我想在表IF NOT EXISTS中执行INSERT
所以我有2个JTextField
,我希望将变量添加到JTextfield
的int表中,但条件是它们不存在于表中
这是代码,它给出了有关查询的错误:
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
答案 0 :(得分:0)
您的查询错误
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 <Some unique Field> not exists in(select "<Some unique Field>" from router where edge01='"+jTextField1.getText()+"' and edge02='"+jTextField2.getText()+"')");
} catch (SQLException ex) {
Logger.getLogger(bdd.class.getName()).log(Level.SEVERE, null, ex);
}
答案 1 :(得分:0)
首先选择数据以检查数据是否存在:'使用select * from router'。如果没有返回数据,则插入。
答案 2 :(得分: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()+"')"