我无法插入Ms Access数据库

时间:2013-08-26 09:59:52

标签: java sql ms-access

我有以下用户界面 enter image description here

但是,我无法向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成功保存”但是,我在ms访问数据库中找不到任何名为nencor的员工

我的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);
    }

}

2 个答案:

答案 0 :(得分:0)

您必须在程序中添加此项。

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connection = DriverManager.getConnection(databaseURL ,"",""); //databaseURL is url for your db
st=connection.createStatement();

发生错误是因为您没有提及并初始化数据库交互所需的变量。在execQuery方法中写了三行并完成了它。

答案 1 :(得分:0)

private statement st;
public void executeQuery(String query)
    {
        try {
            st.executeUpdate(query);
        } catch (SQLException ex) {
            System.out.println("Error: " +ex);
        }
    }

您是否初始化变量'st'