【发布时间】:2014-03-19 21:26:53
【问题描述】:
我的应用程序中有一个按钮,应该保存到我的数据库中。如果您发现我的代码有任何问题,谁能告诉我?它根本没有保存任何东西。
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){}. -
@Shankar 我已经调试过了,找不到错误...
-
将
ex.printStackTrace()放入您的catch 块中并尝试再次运行您的代码。您遇到任何异常吗?