插入数据库后准备语句停止工作
preparedstatement stopped working after inserting into database
我想从用户输入插入到 table 调用的帐户,但这是错误。
它曾经工作但突然我不知道发生了什么,它不再工作了
错误代码:
Exception in thread "main" java.lang.NullPointerException
at pl.marcinek.sql.sql.register(sql.java:28)
at pl.marcinek.accounts.Register.Register(Register.java:32)
at pl.marcinek.Main.main(Main.java:19)
Sql class
public class sql {
private static Connection conn = null;
private static Staenter code heretement st = null;
private static PreparedStatement preparedStmt = null;
public static void sql() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "");
st = conn.createStatement();
}
public static void register() throws SQLException, ClassNotFoundException {
sql.sql();
String query = " insert into accounts (username, password, name, surname, balance, authcode)" + " values (?, ?, ?, ?, ?, ?)";
conn.prepareStatement(query);
preparedStmt.setString(1, Register.username); //error
preparedStmt.setString(2, Register.password);
preparedStmt.setString(3, Register.name);
preparedStmt.setString(4, Register.surname);
preparedStmt.setString(5, Register.balance);
preparedStmt.setString(6, Authenticator.authenticator());
preparedStmt.executeUpdate();
}
注册class
public class Register {
public static String username;
public static String password;
public static String name;
public static String surname;
public static String balance;
public static void Register() throws InterruptedException, SQLException, ClassNotFoundException {
Scanner reg = new Scanner(System.in);
System.out.println("register in this order...");
TimeUnit.SECONDS.sleep(2);
System.out.println("username, password, name, surname, balance");
username = reg.nextLine();
password = reg.nextLine();
name = reg.nextLine();
surname = reg.nextLine();
balance = reg.nextLine();
System.out.println("you were correctly registered");
System.out.println("your authenticating code is: ");
sql.register(); //error
TimeUnit.SECONDS.sleep(1);
sql.id();
System.out.println(" ");
System.out.println("dont show your to anyone");
}
}
主要class
public class Main {
public static void main(String[] args) throws InterruptedException, SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("choose one");
System.out.println("1:log in");
System.out.println("2:register");
String opcja = scanner.nextLine();
if(opcja.equals("1")){
AccountManager.accoutmanager();
}
if(opcja.equals("2")){
Register.Register(); // Error
}
}
}
您忘记将 Connection#prepareStatement()
的调用结果分配给变量:
conn.prepareStatement(query);
preparedStmt.setString(1, Register.username); //error
应该是
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, Register.username);
我想从用户输入插入到 table 调用的帐户,但这是错误。 它曾经工作但突然我不知道发生了什么,它不再工作了
错误代码:
Exception in thread "main" java.lang.NullPointerException
at pl.marcinek.sql.sql.register(sql.java:28)
at pl.marcinek.accounts.Register.Register(Register.java:32)
at pl.marcinek.Main.main(Main.java:19)
Sql class
public class sql {
private static Connection conn = null;
private static Staenter code heretement st = null;
private static PreparedStatement preparedStmt = null;
public static void sql() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "");
st = conn.createStatement();
}
public static void register() throws SQLException, ClassNotFoundException {
sql.sql();
String query = " insert into accounts (username, password, name, surname, balance, authcode)" + " values (?, ?, ?, ?, ?, ?)";
conn.prepareStatement(query);
preparedStmt.setString(1, Register.username); //error
preparedStmt.setString(2, Register.password);
preparedStmt.setString(3, Register.name);
preparedStmt.setString(4, Register.surname);
preparedStmt.setString(5, Register.balance);
preparedStmt.setString(6, Authenticator.authenticator());
preparedStmt.executeUpdate();
}
注册class
public class Register {
public static String username;
public static String password;
public static String name;
public static String surname;
public static String balance;
public static void Register() throws InterruptedException, SQLException, ClassNotFoundException {
Scanner reg = new Scanner(System.in);
System.out.println("register in this order...");
TimeUnit.SECONDS.sleep(2);
System.out.println("username, password, name, surname, balance");
username = reg.nextLine();
password = reg.nextLine();
name = reg.nextLine();
surname = reg.nextLine();
balance = reg.nextLine();
System.out.println("you were correctly registered");
System.out.println("your authenticating code is: ");
sql.register(); //error
TimeUnit.SECONDS.sleep(1);
sql.id();
System.out.println(" ");
System.out.println("dont show your to anyone");
}
}
主要class
public class Main {
public static void main(String[] args) throws InterruptedException, SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.println("choose one");
System.out.println("1:log in");
System.out.println("2:register");
String opcja = scanner.nextLine();
if(opcja.equals("1")){
AccountManager.accoutmanager();
}
if(opcja.equals("2")){
Register.Register(); // Error
}
}
}
您忘记将 Connection#prepareStatement()
的调用结果分配给变量:
conn.prepareStatement(query);
preparedStmt.setString(1, Register.username); //error
应该是
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, Register.username);