我有以下用户界面
但是,我无法将任何数据插入到ms access database
. 第一行是通过手动生成的ms access database
我不知道我的代码有什么问题,看来他们都做得很好
private void doSimpan(String idnya, String namanya,String alamatnya,String teleponnya,String emailnya,String passwordnya,String rolesnya) {
try {
String query = "INSERT INTO MsEmployee (EmployeeID, EmployeeName, EmployeeAddress, EmployeePhone, EmployeeEmail, EmployeePassword, EmployeeRole)VALUES ('"+idnya+"','"+namanya+"','"+alamatnya+"','"+teleponnya+"','"+emailnya+"','"+passwordnya+"','"+rolesnya+"')";
connect.executeQuery(query);
fillTable();
cmd.printSuccess("Master Employee", namanya+" saved successfully");
} catch (Exception e) {
}
}
这是execQuery
方法
private statement st;
public void executeQuery(String query)
{
try {
st.executeUpdate(query);
} catch (SQLException ex) {
System.out.println("Error: " +ex);
}
}
这是我的printAsk()
方法
public Boolean printAsk(String title, String text)
{
if(JOptionPane.showConfirmDialog(null, text, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
return true;
}
else
{
return false;
}
}
这是我的isValid()
方法
private Boolean isValid(String name,String address,String phone,String email,String password) {
if(name.equals("") || address.equals("") || phone.equals("") || email.equals("") || password.equals(""))
{
return false;
}
else if(val.isNumeric(phone) || !email.contains("@") && !email.endsWith(".com"))
{
return false;
}
else
{
return true;
}
}
这是我的代码save button
//save
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String idnya = jTextField1.getText();
String namanya = jTextField2.getText();
String alamatnya = jTextArea1.getText();
String teleponnya = jTextField3.getText();
String emailnya = jTextField4.getText();
String passwordnya = new String(jPasswordField1.getPassword());
String rolesnya = jComboBox2.getSelectedItem().toString();
if(status==1) //to insert
{
if(isValid(namanya, idnya, teleponnya, emailnya, passwordnya))
{
if(cmd.printAsk("Master Employee", namanya+" will be entered, continue ?"))
{
doSimpan(idnya, namanya, alamatnya, teleponnya, emailnya, passwordnya, rolesnya);
fillTable();
defaultControl(false,false);
crudButton(true);
}
}
else
{
cmd.printFail("Master Employee", "all field must be filled");
}
}
else if (status==2)//to update
{
}
}
我哪里错了?nencor
我得到“nencor 保存成功”,但随后,我在 ms 访问数据库中找不到任何员工
我的Connect
构造函数
public Connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=TIFProject.mdb");
st = con.createStatement(1004, 1008);
} catch (Exception ex) {
System.out.println("Database Error" + ex);
}
}