からIDを渡すと、コードです:JavaのJDBCは、 - ここで、テーブル
public static Connection getConnection() throws Exception {
String name1 = "Danny";
String city1 = "Wien";
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/supermarket??verifyServerCertificate=false&useSSL=true";
String username = "myuser";
String password = "mypass";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "insert into marketinfo "
+ " (name, country)" + " values (" + name1 + ", " + city1 + ")";
Statement insertie = conn.prepareStatement(sql);
insertie.executeUpdate(sql);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
私の誤差が"Unknown column 'Danny' in 'field list'"
です。
Sqlデータベースでは、myテーブルにID、名前、都市が含まれています。そのidは自動的に増分されるので、idフィールドを渡したいと思います。
あなたはメソッドスコープのConnectionまたはPreparedStatementを閉じているわけではありません。悪い考えです。これはJDK 8の試用には適しているかもしれません。https://stackoverflow.com/questions/8066501/how-should-i-use-try-with-resources-with-jdbc – duffymo
どうすれば閉じられますか?彼らはtry-with-resourcesブロックに入っています。もちろん、私は 'stmt.set()'呼び出しを、あなたがリンクした答えのような別のメソッドに入れていません。しかし、私はResultSetオブジェクトを扱っていないので、必要ではないようです。私が見逃した別の面がありますか? – gridDragon
そこにはConnectionがあります。私はそれがPreparedStatementに発生していることを知らない。私の失敗かもしれない、あなたのものではありません。だから私はdownvoteしなかった。 – duffymo