2

テーブルに INSERT を実行したい IF NOT EXISTS

だから私は2つJTextFieldのsを持っていて、i stの変数をs intテーブルに追加したいのですJTextfieldが、それらがテーブルに存在しないという条件で

これはコードであり、クエリに関するエラーが表示されます。

try{
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db, "root", "123456");
       stmt = conn.createStatement();
       stmt.executeUpdate(
           "insert into router (edge01,edge02)\n" +
           "Select edge01, edge02 " +
           "Where not exists(select * from router " +
                            "where edge01='" + jTextField1.getText() +
                              "' and edge02='" + jTextField2.getText() + "')");
}
catch (SQLException ex) {
    Logger.getLogger(bdd.class.getName()).log(Level.SEVERE, null, ex);
}

エラー:

SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Where not exists(select * from router where edge01='fffffffff' and edge02='fffff' at line 2

4

3 に答える 3

0

代わりに次のクエリを使用してみてください。

insert into router (edge01,edge02)
 select edge01,edge02 from router where edge01 not in(select edge01 from router where    
       edge01='"+jTextField1.getText()+"' and edge02='"+jTextField2.getText()+"')"
于 2013-06-21T10:54:37.050 に答える