我最近开始编码,我正试图在Java上创建一个注册表,但我无法获得我的"寄存器";按钮从TextField获取数据并将其保存在我的数据库中。这就是我所拥有的,我完全迷失了。
Register.addActionListener(new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{ if(fn.getText().equals("")||fn.getText()==null||ln.getText().equals("")||
ln.getText()==null ||id.getText().equals("")||id.getText()==null ||gpa.getText().equals("")||gpa.getText()==null)
JOptionPane.showMessageDialog(null, "Please enter all Fields","Error",0);
else{
String First_Name=fn.getText();
String Last_Name=ln.getText();
String ID=id.getText();
String GPA =gpa.getText();
try{
Connection connection=null;
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/MYDB","root","");
PreparedStatement pstmt = null;
String query = "INSERT INTO CourseDemo(First_Name, Last_Name, ID, GPA)" + "VALUES (?, ?, ?, ?)";
pstmt = conn.prepareCall(query);
int status = pstmt.executeUpdate();
if(status > 0) {
System.out.println("Record is inserted successfully !!!");
}
}
catch(ClassNotFoundException ex){
}
catch(Exception f){
f.printStackTrace();
}
}
}
});
尝试:
pstmt = conn.prepareCall(query);
pstmt.setString(1, First_Name);
pstmt.setString(2, Last_Name);
pstmt.setString(3, ID); // consider setInt() might be more appropriate
pstmt.setString(4, GPA);
int status = pstmt.executeUpdate();
文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
为PreparedStatement参数提供值
您必须提供值来代替问号占位符(如果有)然后才能执行PreparedStatement对象。请致电PreparedStatement类中定义的setter方法之一。
这里需要解决的一些问题:
-
a)查询参数没有添加到语句中
b) 要么处理异常,要么抛出
稍微重构代码:
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
{
if (invalidInput()) {
JOptionPane.showMessageDialog(null, "Please enter all Fields", "Error", 0);
} else {
String First_Name = fn.getText();
String Last_Name = ln.getText();
String ID = id.getText();
String GPA = gpa.getText();
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MYDB", "root", "");
String query = "INSERT INTO CourseDemo(First_Name, Last_Name, ID, GPA)" + "VALUES (?, ?, ?, ?)";
pstmt = conn.prepareCall(query);
pstmt.setString(1, First_Name);
pstmt.setString(2, Last_Name);
pstmt.setString(3, ID);
pstmt.setString(4, GPA);
int status = pstmt.executeUpdate();
if (status > 0) {
System.out.println("Record is inserted successfully !!!");
}
} catch (ClassNotFoundException ex) {
// todo show error message or throw
} catch (Exception f) {
// todo show error message or throw
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException s) {
// todo show error message
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException s2) {
// todo show error message
}
}
}
}
}
}
};
在那里,您还有一个用于验证的辅助方法
boolean invalidInput() {
return fn.getText() == null || fn.getText().equals("") || ln.getText() == null || ln.getText().equals("") || id.getText() == null || id.getText().equals("") || gpa.getText() == null || gpa.getText().equals("");
}
- 必须重构验证(对输入的检查),因为在其他需要非null值的检查(会抛出null指针)之后,还有null检查。请注意,在这里,用OR(
||
)中的所有这些东西来测试一个方法并不简单,将检查拆分为不同的语句会更好、更易于维护
另一条评论:您正在使用哪个jdk?如果您使用的是java7或更高版本,请考虑使用try-with-resources。
在todo
注释中,您必须处理可能引发的异常或抛出异常。