与我的上一个问题相关:Java SQL (JDBC) : how to move to the next column?
我有一个包含少量列的SQL表,其中一列是" UserID"这是PK和AI
代码是做什么的? : 1.从用户那里获取用户名,电子邮件和密码。 2.检查表中是否已存在用户名或电子邮件。 3.如果表中没有用户和电子邮件,我会在表格中插入用户名,电子邮件和密码。
然而,当我进行插入时,我仍然需要插入UserID
因为我现在无法运行我的代码,因为我遇到了Workbench MySQL的问题,我只是想知道这段代码是否有效:
/* checks that last UserID in the Table */
String command = "SELECT UserID FROM users";
resultSet = statement.executeQuery(command);
resultSet.last();
id = resultSet.getInt("UserID") + 1;
然后当我使用 INSERT 查询时:
command = "INSERT users SET UserID = '" + id + "', UserName = '" + usr.toString()
+ "',Email = '" + eml.toString()
+ "',Password = '" + pwd.toString()
+ "',Status = '0' ,Connected = '1';";
statement.executeUpdate(command);
你觉得怎么样?
这是我的整个班级:
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
公共类DB {
private String usr;
private String eml;
private String pwd;
private int id = 0;
private ResultSet resultSet;
private Statement statement;
private Connection connection;
String jdbcUrl = "jdbc:mysql://localhost:3306/registered";
String jdbcUser = "...";
String jdbcPassword = "...";
int flag = 0;
public boolean checkSignUp(String usr, String eml, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(jdbcUrl, jdbcUser,
jdbcPassword);
statement = connection.createStatement();
/* checks that last UserID in the Table */
String command = "SELECT UserID FROM users";
resultSet = statement.executeQuery(command);
resultSet.last();
id = resultSet.getInt("UserID") + 1;
/* runs over the UserName column */
command = "SELECT UserName FROM users WHERE UserName LIKE '"
+ usr.toString() + "';";
resultSet = statement.executeQuery(command);
while (resultSet.next()) {
if (usr.equalsIgnoreCase(resultSet.getString("UserName"))) {
System.out.println("UserName : " + usr.toString() + " is taken!");
flag++;
}
}
/* runs over the Email column */
command = "SELECT Email FROM users WHERE UserName LIKE '"
+ usr.toString() + "';";
resultSet = statement.executeQuery(command);
while (resultSet.next()) {
if (eml.equalsIgnoreCase(resultSet.getString("Email"))) {
System.out.println("Email : " + this.eml + " is taken!");
flag +=2;
}
}
/* if user\email exists or not */
if (flag == 0) {
System.out.println("Email : " + this.eml + " and UserName : "
+ usr + " are AVAILABLE!");
command = "INSERT users SET UserID = '" + id + "', UserName = '" + usr.toString()
+ "',Email = '" + eml.toString()
+ "',Password = '" + pwd.toString()
+ "',Status = '0' ,Connected = '1';";
statement.executeUpdate(command);
return true;
}
else if (flag == 1) {
//show error message
}
else if (flag == 2) {
//show error message
}
else if (flag == 3) {
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("Vendor error: " + e.getErrorCode());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
flag = 0;
return false;
}
}
答案 0 :(得分:1)
此代码存在许多问题:
答案 1 :(得分:1)
您可以更改表格结构以自动增加UserID。
否则,您可以将ID值设为 SELECT max(UserID)+1 FROM users
由于它是用户凭据,而不是以纯文本形式建议在DB中加密它们