我对 SQL INSERT QUERY 有疑问。每当我在下面的代码中执行 INSERT QUERY 时,会发生什么,查询理解要作为列名输入的值。我的代码是:
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 语句未执行!com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“你好”
-- 编辑 -- 实际上,我的代码是:
s.executeUpdate("insert into "+nameoftab+" (sname,"+""+ddvalue+""+") values ("+cb5.getSelectedItem()+","+cb3.getSelectedItem()+")");
当我像每个人所说的那样插入引号时,会发生什么是术语“cb5.getSelectedItem()”被输入到表中。"+cb3.getSelectedItem()+" 的情况是,它只是输入了一些垃圾值。