更新了数据库[update ... set] java

时间:2015-05-11 22:53:53

标签: java database

我的java代码有问题。当我进行数据库更新但他们没有工作时。我使用了我在互联网上找到的三种方法,但仍然无法使用! 帮我 提前谢谢你

package HLR_SERVEUR;
public class model_HLR{

    public void setupdate(String[] information) 
    {
        int imsdn_values,cins,kcs,kis;
        long IMSI_values=0;

        this.IMSI_values = Long.parseLong(information[0]);
        this.imsdn_values = Integer.parseInt(information[1]);
        this.kis = Integer.parseInt(information[2]);
        this.kcs = Integer.parseInt(information[3]);
        this.service = new String(information[4]);
        this.nom = new String(information[5]);
        this.prenom = new String(information[6]);
        this.cins = Integer.parseInt(information[7]);
        System.out.println(this.IMSI_values);
        System.out.println(this.imsdn_values);
        System.out.println(this.kis);
        System.out.println(this.kcs);
        System.out.println(this.service);
        System.out.println(this.nom);
        System.out.println(this.prenom);
        System.out.println(this.cins);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            dbConnect = DriverManager.getConnection("jdbc:mysql://localhost/abonnes", "root","");
            dbStatement = dbConnect.createStatement();

            /*
                test 1: 

                 PreparedStatement prepareStatement = (PreparedStatement)                                                     dbConnect.prepareStatement("Update sim set ISMIDN="+this.imsdn_values +", KI="+this.kis +",KC="+this.kcs+",SERVICE='"+this.service +"',nom='"+this.nom +"',prenom='"+this.prenom +"',cin="+this.cins +" Where IMSI="+this.IMSI_value+"");

            k=prepareStatement.executeUpdate();
                test 2: 
               PreparedStatement checkDB = (PreparedStatement)   dbConnect.prepareStatement( "UPDATE sim set ISMIDN= ? ,KI= ?,KC= ?,SERVICE= ?,nom= ?,prenom= ?,cin= ? Where ISMI= ? ");

            checkDB.setInt(1,this.imsdn_values);
            checkDB.setInt(2,this.kis);
            checkDB.setInt(3,this.kcs);
            checkDB.setString(4,this.service);
            checkDB.setString(5,this.nom);
            checkDB.setString(6,this.prenom); 
            checkDB.setInt(7,this.cins);
            checkDB.setLong(8,this.IMSI_values);
            k=checkDB.executeUpdate();
            */  
                 /*    test 3: */

            dbStatement=dbConnect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

            ResultSet uprs = dbStatement
                    .executeQuery("SELECT * FROM sim where ISMI ='"
                            + this.IMSI_values + "'");


                uprs.moveToInsertRow();
                uprs.updateInt("ISMIDN", this.imsdn_values);
                uprs.updateInt("KI", this.kis);
                uprs.updateInt("KC", this.kcs);
                uprs.updateString("SERVICE", this.service);
                uprs.updateString("nom", this.nom);
                uprs.updateString("prenom", this.prenom);
                uprs.updateInt("cin", this.cins);

                uprs.insertRow();
                uprs.beforeFirst();

            dbStatement.close();
            dbConnect.close();


        } catch (SQLException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

mai 12,2015 12:20:55 AM HLR_SERVEUR.model_HLR setupdate GRAVE:null com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:Column' ISMI'不能为空     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)     在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)     在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)     在com.mysql.jdbc.Connection.execSQL(Connection.java:3249)     在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)     在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)     在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)     在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)     在com.mysql.jdbc.UpdatableResultSet.insertRow(UpdatableResultSet.java:739)     在HLR_SERVEUR.model_HLR.setupdate(model_HLR.java:304)     在HLR_SERVEUR.AbonnePanel_Rechercher.actionPerformed(AbonnePanel_Rechercher.java:401)     在javax.swing.AbstractButton.fireActionPerformed(未知来源)     at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)     在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)     在javax.swing.DefaultButtonModel.setPressed(未知来源)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)     at java.awt.Component.processMouseEvent(Unknown Source)     在javax.swing.JComponent.processMouseEvent(未知来源)     at java.awt.Component.processEvent(Unknown Source)     at java.awt.Container.processEvent(Unknown Source)     at java.awt.Component.dispatchEventImpl(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Window.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)     在java.awt.EventQueue.access $ 500(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     在java.awt.EventQueue $ 4.run(未知来源)     在java.awt.EventQueue $ 4.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at java.awt.EventQueue.dispatchEvent(Unknown Source)     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     在java.awt.EventDispatchThread.run(未知来源)

问题mai 12,2015 1:42:15 AM HLR_SERVEUR.model_HLR setupdate GRAVE:null com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:Duplicate entry! 216011100258963'对于关键' PRIMARY

1 个答案:

答案 0 :(得分:0)

从您发布的答案日志中, ISMI 列是数据库表的必填字段,它似乎是主键

您需要以下内容:

uprs.updateInt("ISMI", someValue);

完成新的行数据。

另请注意,表格中的主键必须是唯一的