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:「フィールド リスト」の不明な列「hello」
-- 編集 -- 実際、私のコードは次のとおりです。
s.executeUpdate("insert into "+nameoftab+" (sname,"+""+ddvalue+""+") values ("+cb5.getSelectedItem()+","+cb3.getSelectedItem()+")");
誰もが言ったように引用符を挿入すると、「cb5.getSelectedItem()」という用語がテーブルに入力されます。「+cb3.getSelectedItem()+」の場合は、ガベージ値を入力するだけです。