简体   繁体   中英

how to insert value in mysql only if it already doesn't exist?

I have written this sql query in java to insert record in database if it doesn't exists to avoid duplicate entry...

    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();

I have used this query...This query is not working....I don't know if it's right..

    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)

I am getting this error

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)

Use INSERT IGNORE INTO table

This will not insert a duplicate row in the mySql table.

You can make a select of that row. If you do not have results do the insert.

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 ...

The insert Stmt includes the autoincrement field? You try to set it with the

pst.setString(1,null);  //This field is autoincrement

Since the field is AutoIncrement it should not be used in the insert stmt . Also you can improve your exists for perfmance to

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM