-2

我有以下用户界面 在此处输入图像描述

但是,我无法将任何数据插入到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);
    }

}
4

2 回答 2

0

你必须在你的程序中添加它。

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

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

于 2013-08-26T10:11:35.083 回答
0
private statement st;
public void executeQuery(String query)
    {
        try {
            st.executeUpdate(query);
        } catch (SQLException ex) {
            System.out.println("Error: " +ex);
        }
    }

你有没有初始化变量'st'

于 2013-08-26T10:17:11.840 回答