I have a problem with the SQL INSERT QUERY. Whenever I execute the INSERT QUERY in the below code, what happens, is the query understands the values to be entered as the column names. My code is :
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db","root","123456");
Statement s = con.createStatement();
int start = 8, end = 10;
char buf[] = new char[end-start]; // for extracting day alone.
dvalue = new String();
ddvalue = new String(); // for adding the extracted day to the table.
dvalue = cb3.getSelectedItem().toString();
dvalue.getChars(start, end, buf, 0);System.out.println(buf);
ddvalue = String.copyValueOf(buf);
s.executeUpdate("insert into "+nameoftab+" (sname,"+"_"+ddvalue+"_"+") values (hello,"+cb3.getSelectedItem()+")");
}
catch(SQLException s)
{
System.out.println("SQL statemnet is not executed!");
System.out.println(s);
}
The error that I get after executing the query is :-
SQL statemnet is not executed! com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'hello' in 'field list'
-- EDIT-- Actually, my code was :
s.executeUpdate("insert into "+nameoftab+" (sname,"+""+ddvalue+""+") values ("+cb5.getSelectedItem()+","+cb3.getSelectedItem()+")");
When I insert the quotes like everyone said, what happens is that the term "cb5.getSelectedItem()" is being entered into the table. The case with "+cb3.getSelectedItem()+" is that, it just enteres some garbage value.
You need to quote the string hello
in your query.
s.executeUpdate("insert into "+nameoftab+" (sname,"+"_"+ddvalue+"_"+") values ('hello',"+cb3.getSelectedItem()+")");