英文:
How do I add the auto increment values with java?
问题
我在MySQL中创建了一个表:
CREATE TABLE login
(
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声明
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 (?, ?)";
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.setString(1, u);
preparedStmt.setString(2, hashpass);
preparedStmt.execute();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
一切运行正常
但是id是自动增长的,我在添加其他列值时不需要添加id的值。
当通过Java添加时,我不能像这样添加,只需添加us、ps列,id将自动递增。
是否有不传递参数的方法来添加数据?
英文:
I created a table in Mysql using
Create table
(
id int auto_increment,
us varchar(100),
ps varchar(1000)
);
And used java for adding values thru my GUI application:
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.
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
列:
String query = "insert into login (us, ps) values (?, ?)";
在预处理语句中不设置任何值给它,所以删除这行:
// preparedStmt.setInt(1, id++);
id
列是自增的,因此其值将由 MySQL 设置。
当然,将其他行的索引更改为 1 和 2:
preparedStmt.setString(1, u);
preparedStmt.setString(2, hashpass);
英文:
Remove the column id
from the sql statement:
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.<br/>
Of course change the indices of the other lines to 1 and 2:
preparedStmt.setString(1,u);
preparedStmt.setString(2,hashpass);
答案2
得分: 1
可能会插入没有ID的数据,因为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 (?, ?)"; // 在这里检查
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);
}
}
英文:
You might insert data without ID as it will be auto-generated from 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);}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论