我已经在java中编写了这个sql查询,以便在数据库中插入记录,如果它不存在以避免重复输入......
PreparedStatement pst = connection.prepareStatement("INSERT INTO tbl_review (?,?,?,?,?,?,?) SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 WHERE NOT EXISTS (SELECT t2.product_name,t2.product_model_no,t2.user_username,t2.user_contact FROM tbl_review t2 WHERE t2.product_name = t1.product_name AND t2.product_model_no = t1.product_model_no AND t2.user_username = t1.user_username AND t2.user_contact = t1.user_contact)");
pst.setString(1,null); //This field is autoincrement
pst.setString(2,prodname);
pst.setString(3,prodmodel);
pst.setString(4,user);
pst.setLong(5,usercon);
pst.setString(6,review);
pst.setString(7,rating);
int numRowsChanged = pst.executeUpdate();
我使用过此查询...此查询无效....我不知道它是否正确..
INSERT INTO tbl_review (?,?,?,?,?,?,?)
SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1
WHERE NOT EXISTS
(SELECT t2.product_name,t2.product_model_no,
t2.user_username,t2.user_contact
FROM tbl_review t2
WHERE t2.product_name = t1.product_name AND
t2.product_model_no = t1.product_model_no AND
t2.user_username = t1.user_username AND
t2.user_contact = t1.user_contact)
我收到此错误
INFO: Server startup in 1027 ms
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 'null,'Some Product','S657','username',9944558675,'sdfdsfdf','3') SELECT t1.pro' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
at com.stc.AddReview.doPost(AddReview.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
使用INSERT IGNORE INTO table
这不会在mySql表中插入重复的行。
答案 1 :(得分:0)
您可以选择该行。如果没有结果,请插入。
SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 WHERE t1.product_name = :paramName AND t1.product_model_no = :paramModNo AND ...
答案 2 :(得分:0)
插入Stmt包含自动增量字段?您尝试使用
进行设置pst.setString(1,null); //This field is autoincrement
由于该字段为AutoIncrement,因此不应在insert stmt 中使用。 此外,您可以改善穿孔的存在感
SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1
WHERE NOT EXISTS
(SELECT 1
FROM tbl_review t2
WHERE t2.product_name = t1.product_name AND
t2.product_model_no = t1.product_model_no AND
t2.user_username = t1.user_username AND
t2.user_contact = t1.user_contact)