【问题标题】:How do I add the auto increment values with java?如何使用 java 添加自动增量值?
【发布时间】:2020-09-24 15:31:40
【问题描述】:

我在 Mysql 中使用创建了一个表

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

并使用 java 通过我的 GUI 应用程序添加值:

我使用以下方法将值添加到我的数据库中:

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

一切正常

但是 id 是 auto_increment 并且我不需要在添加其他列值的同时向 id 添加值。

我不能这样添加,而通过 java 添加就像只添加我们,ps 列和 id 将自动递增。

有什么方法可以不传参数就添加数据吗?

【问题讨论】:

    标签: java mysql sql database prepared-statement


    【解决方案1】:

    从sql语句中删除列id

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

    并且不要在准备好的语句中为其设置任何值,因此删除此行:

    preparedStmt.setInt(1, id++); 
    

    id 列是auto_inrement,所以它的值将由 MySql 设置。
    当然将其他行的索引更改为 1 和 2:

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

    【讨论】:

    • 我错过了要更改的索引值
    【解决方案2】:

    您可以插入没有 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);}           
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多