gpt4 book ai didi

java - 仅当值不存在时如何将其插入到mysql中?

转载 作者:行者123 更新时间:2023-11-29 22:36:52 26 4
gpt4 key购买 nike

我用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)

最佳答案

使用INSERT IGNORE INTO 表

这不会在 mySql 表中插入重复行。

关于java - 仅当值不存在时如何将其插入到mysql中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29509101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com