我有一个按钮在我的应用程序应该保存到我的数据库。谁能告诉我,如果你看到我的代码有什么问题吗?它根本没有保存任何东西。
saveButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//gets text from texfields and saves to instance variables
String fname = fNameTextBox.getText();
String lname = lNameTextBox.getText();
String email = eMailTextBox.getText();
String signUpDate = signUpTextBox.getText();
try
{
//moves cursor to new row
//rs.moveToInsertRow();
//statement that checks if user enters all letters
if(fname.matches("[a-zA-Z]+"))
{
//statement that checks if user enters all letters
if(lname.matches("[a-zA-Z]+"))
{
//statement and actions if user enters a '.'
if(email.contains("."))
{
//gets last period in email
int emailDotCheck = email.lastIndexOf(".");
//substring to period in variable "emailDotCheck"
String extensionCheck = email.substring(emailDotCheck);
//statement and actions if user doesn't enter email correctly
if(!email.contains("@") || !extensionCheck.matches("\.[a-z]{3}"))
{
JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
eMailTextBox.setText("");
}
else
{
//instance variables
int month = 100;
int day = 100;
int year = 10000;
//statement and actions if user enters 'signUpDate' in correct format
if(signUpDate.matches("\d{2}/\d{2}/\d{4}"))
{
//gets substring of instance variables
String monthStr = signUpDate.substring(0,2);
String dayStr = signUpDate.substring(3,5);
String yearStr = signUpDate.substring(6);
//parsing intstance variables to Integers
month = Integer.parseInt(monthStr);
day = Integer.parseInt(dayStr);
year = Integer.parseInt(yearStr);
//statements and actions if user doesn't enter date in correct format
if(month > 12 || day > 31 || year > 2100)
{
JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
signUpTextBox.setText("");
}
else
{
//String sql4 = "INSERT INTO Table1 (Fname, Lname, [E_mail], [Sign_up_date]) VALUES (fname, lname, email, signUpDate)";
//execute query, assigning specified record in db to 'rs4'
//rs4 = st.executeQuery(sql4);
rs.moveToInsertRow();
//inserts record into db
rs.updateString("Fname", fname);
rs.updateString("Lname", lname);
rs.updateString("E-mail", email);
rs.updateString("Sign_up_date", signUpDate);
//inserts data into db
rs.insertRow();
//closes statement variable so there won't be a gap in db
st.close();
//closes result set variable so there won't be a gap in db
rs.close();
//create new statement to help us gain access to table in db
st = con.createStatement(rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_UPDATABLE);
//statement that selects everything from our table
String sql = "SELECT * FROM Table1";
//execute query, assigning all records in db to 'rs'
rs = st.executeQuery(sql);
//gets next row in db
rs.next();
//sets text in text fields to specified fields in db
fNameTextBox.setText(rs.getString("Fname"));
lNameTextBox.setText(rs.getString("Lname"));
eMailTextBox.setText(rs.getString("E_mail"));
fNameTextBox.setText(rs.getString("Sign_up_date"));
}
}
//statement and actions if user does enter date in correct format
else
{
JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
signUpTextBox.setText("");
}
}
}
//statement and actions if user doesn't enter email in correct format
else
{
JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
eMailTextBox.setText("");
}
}
//statement and actions if user doesnt enter last name in correct format
else
{
JOptionPane.showMessageDialog(null, "Please enter last name in correct format!");
lNameTextBox.setText("");
}
}
//statement and actions if user doesn't enter first name in correct format
else
{
JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
fNameTextBox.setText("");
}
}
catch(Exception ex)
{
}
}
});
从报告遇到的任何错误开始
catch(Exception ex)
{
}
用catch块静默地处理错误通常是个坏主意。
第二,使用调试器,逐步检查代码,看看发生了什么。试图通过检查办公桌来解决这样的问题。仅仅阅读代码通常是非常低效的。这里我们没有关键信息,比如我们是否有信心
rs.moveToInsertRow();
可以被期望工作——我们不知道rs是如何得到它的值的。许多数据库问题只发生在运行时,例如数据库连接配置错误或试图插入与现有数据冲突。因此,只有理解你的代码在测试运行中做了什么,问题才能得到解决。
在这里,我猜在catch中打印出一些统计信息将非常有帮助,否则进入调试器(或添加跟踪语句)将使您达到目的。