简体   繁体   English

如何使用 java 添加自动增量值?

[英]How do I add the auto increment values with java?

I created a table in Mysql using我在 Mysql 使用创建了一个表

Create table 
(
 id int auto_increment,
 us varchar(100),
 ps varchar(1000)
); 

And used java for adding values thru my GUI application:并使用 java 通过我的 GUI 应用程序添加值:

I used the following method to add values into my database:我使用以下方法将值添加到我的数据库中:

public static void Mysql(String u, String p) throws NoSuchAlgorithmException, InvalidKeySpecException
    {
        String hashpass=passhash(p);//throws declaration for this statement
        try{  
            Class.forName("com.mysql.cj.jdbc.Driver");  
            Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bs","root","root");   
            String query = " insert into login (id,us,ps)"
                    + " values (?,?, ?)";  
            Statement stmt=con.createStatement();  
            ResultSet rs=stmt.executeQuery("select * from login"); 
            int id=0;
            while(rs.next())
            {
                id= rs.getInt(1);
            }
            PreparedStatement preparedStmt = con.prepareStatement(query);
            preparedStmt.setInt(1, id++); //I don't want this method because id is auto increment
            preparedStmt.setString(2,u);
            preparedStmt.setString(3,hashpass);
            preparedStmt.execute();
            con.close();  
            }catch(Exception e){ System.out.println(e);}  
              
    }

Everything works fine一切正常

But the id is the auto_increment and I don't need to add value to id while adding other column values.但是 id 是 auto_increment 并且我不需要在添加其他列值时向 id 添加值。

I can't add like that while adding thru java like only adding us, ps columns and the id will be automatically incremented.我不能像那样添加,同时通过 java 添加,就像只添加我们一样,ps 列和 id 将自动递增。

Are there any methods to add data without passing the parameters?有没有不传递参数就可以添加数据的方法?

Remove the column id from the sql statement:从 sql 语句中删除列id

String query = "insert into login (us, ps) values (?, ?)";

and don't set any value for it in the prepared statement, so remove this line:并且不要在准备好的语句中为它设置任何值,所以删除这一行:

preparedStmt.setInt(1, id++); 

The column id is auto_inrement so its value will be set by MySql.idauto_inrement ,因此其值将由 MySql 设置。
Of course change the indices of the other lines to 1 and 2:当然将其他行的索引更改为 1 和 2:

preparedStmt.setString(1,u);
preparedStmt.setString(2,hashpass);

You might insert data without ID as it will be auto-generated from SQL您可能会插入没有 ID 的数据,因为它将从 SQL 自动生成

public static void Mysql(String u,String p) throws NoSuchAlgorithmException, InvalidKeySpecException {
    String hashpass=passhash(p);
    try{  
        Class.forName("com.mysql.cj.jdbc.Driver");  
        Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bs","root","root");   
        String query = " insert into login (us,ps) values (?, ?)"; // CHECK HERE  
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("select * from login"); 
        PreparedStatement preparedStmt = con.prepareStatement(query);
        preparedStmt.setString(1,u);
        preparedStmt.setString(2,hashpass);
        preparedStmt.execute();
        con.close();  
    }catch(Exception e){
        System.out.println(e);}           
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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