如何在Java中添加自增值?

huangapple go评论100阅读模式
英文:

How do I add the auto increment values with java?

问题

我在MySQL中创建了一个表:

  1. CREATE TABLE login
  2. (
  3. id INT AUTO_INCREMENT,
  4. us VARCHAR(100),
  5. ps VARCHAR(1000)
  6. );

然后我使用Java通过GUI应用程序添加值到数据库:

我使用了以下方法向数据库中添加值:

  1. public static void Mysql(String u, String p) throws NoSuchAlgorithmException, InvalidKeySpecException
  2. {
  3. String hashpass = passhash(p); // 对此语句进行了throws声明
  4. try {
  5. Class.forName("com.mysql.cj.jdbc.Driver");
  6. Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bs", "root", "root");
  7. String query = "INSERT INTO login (us, ps) VALUES (?, ?)";
  8. Statement stmt = con.createStatement();
  9. ResultSet rs = stmt.executeQuery("SELECT * FROM login");
  10. int id = 0;
  11. while (rs.next()) {
  12. id = rs.getInt(1);
  13. }
  14. PreparedStatement preparedStmt = con.prepareStatement(query);
  15. preparedStmt.setString(1, u);
  16. preparedStmt.setString(2, hashpass);
  17. preparedStmt.execute();
  18. con.close();
  19. } catch (Exception e) {
  20. System.out.println(e);
  21. }
  22. }

一切运行正常

但是id是自动增长的,我在添加其他列值时不需要添加id的值。

当通过Java添加时,我不能像这样添加,只需添加us、ps列,id将自动递增。

是否有不传递参数的方法来添加数据?

英文:

I created a table in Mysql using

  1. Create table
  2. (
  3. id int auto_increment,
  4. us varchar(100),
  5. ps varchar(1000)
  6. );

And used java for adding values thru my GUI application:

I used the following method to add values into my database:

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

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

I can't add like that while adding thru java like only adding us, ps columns and the id will be automatically incremented.

> Are there any methods to add data without passing the parameters?

答案1

得分: 5

从 SQL 语句中删除 id 列:

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

在预处理语句中不设置任何值给它,所以删除这行:

  1. // preparedStmt.setInt(1, id++);

id 列是自增的,因此其值将由 MySQL 设置。

当然,将其他行的索引更改为 1 和 2:

  1. preparedStmt.setString(1, u);
  2. preparedStmt.setString(2, hashpass);
英文:

Remove the column id from the sql statement:

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

and don't set any value for it in the prepared statement, so remove this line:

  1. preparedStmt.setInt(1, id++);

The column id is auto_inrement so its value will be set by MySql.<br/>
Of course change the indices of the other lines to 1 and 2:

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

答案2

得分: 1

可能会插入没有ID的数据,因为ID将从SQL中自动生成。

  1. public static void Mysql(String u, String p) throws NoSuchAlgorithmException, InvalidKeySpecException {
  2. String hashpass = passhash(p);
  3. try {
  4. Class.forName("com.mysql.cj.jdbc.Driver");
  5. Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bs", "root", "root");
  6. String query = " insert into login (us, ps) values (?, ?)"; // 在这里检查
  7. Statement stmt = con.createStatement();
  8. ResultSet rs = stmt.executeQuery("select * from login");
  9. PreparedStatement preparedStmt = con.prepareStatement(query);
  10. preparedStmt.setString(1, u);
  11. preparedStmt.setString(2, hashpass);
  12. preparedStmt.execute();
  13. con.close();
  14. } catch (Exception e) {
  15. System.out.println(e);
  16. }
  17. }
英文:

You might insert data without ID as it will be auto-generated from SQL

  1. public static void Mysql(String u,String p) throws NoSuchAlgorithmException, InvalidKeySpecException {
  2. String hashpass=passhash(p);
  3. try{
  4. Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
  5. Connection con=DriverManager.getConnection(&quot;jdbc:mysql://127.0.0.1:3306/bs&quot;,&quot;root&quot;,&quot;root&quot;);
  6. String query = &quot; insert into login (us,ps) values (?, ?)&quot;; // CHECK HERE
  7. Statement stmt=con.createStatement();
  8. ResultSet rs=stmt.executeQuery(&quot;select * from login&quot;);
  9. PreparedStatement preparedStmt = con.prepareStatement(query);
  10. preparedStmt.setString(1,u);
  11. preparedStmt.setString(2,hashpass);
  12. preparedStmt.execute();
  13. con.close();
  14. }catch(Exception e){
  15. System.out.println(e);}
  16. }
  17. }

huangapple
  • 本文由 发表于 2020年9月24日 23:31:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定