我有一个SQL INSERT查询的问题。每当我在下面的代码中执行INSERT查询时,所发生的情况是,查询理解要作为列名输入的值。我的代码是:
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);
}执行查询后,我得到的错误是:-
SQL statemnet未执行!字段列表中的com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'hello‘
-- EDIT--实际上,我的代码是:
s.executeUpdate("insert into "+nameoftab+" (sname,"+""+ddvalue+""+") values ("+cb5.getSelectedItem()+","+cb3.getSelectedItem()+")"); 当我像每个人说的那样插入引号时,发生的情况是术语"cb5.getSelectedItem()“被输入到表中。"+cb3.getSelectedItem()+“的情况是,它只是输入了一些无用的值。
发布于 2013-02-19 12:51:34
您需要在查询中引用字符串hello。
s.executeUpdate("insert into "+nameoftab+" (sname,"+"_"+ddvalue+"_"+") values ('hello',"+cb3.getSelectedItem()+")");发布于 2013-02-19 12:53:48
试着用单引号将hello括起来-例如'hello‘
发布于 2013-02-19 12:55:35
尝尝这个
s.executeUpdate("insert into "+nameoftab+" (sname,"+"_"+ddvalue+"_"+") values ('hello',"+cb3.getSelectedItem()+")");https://stackoverflow.com/questions/14950046
复制相似问题