0

我是这种 java 语言的新手,所以我仍然很难弄清楚,虽然我没有错误,但仍然没有运行。帮我弄清楚这件事。我发布了与此相同的程序,尽管问题早在我已经弄清楚了。

public Connection dbConnection() throws Exception
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String filename =
        "C:/Documents and Settings/gebs/Desktop/Database1.mdb";// fileplace
    String database =
        "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";// connection driver
    database += filename.trim() + ";DriverID=22;READONLY=true}";
    return DriverManager.getConnection(database, "", "");

    try
    {
        Connection con = dbConnection();

        String lname = this.jTextField1.getText();
        String fname = this.jTextField2.getText();
        String mname = this.jTextField3.getText();
        String address = this.jTextField4.getText();
        String birthday =
            (String) this.jComboBox1.getSelectedItem()
                + this.jTextField5.getText() + this.jTextField6.getText();
        String gender = (String) this.jComboBox2.getSelectedItem();
        String age = this.jTextField7.getText();
        String contactno = this.jTextField8.getText();
        String contactperson = this.jTextField9.getText();
        String department = this.jTextField11.getText();
        String position = this.jTextField12.getText();

        // to insert to database
        String query =
            "INSERT INTO Personal_Category "
                + "(Last_Name,First_Name,Middle_Initial,Address, "
                + "Birthday,Gender,Age,Contact_No,Contact_Person,"
                + "Department,Position) "
                + "VALUES (?,?,?,?,?,?,?,?,?,?,?,)";

        PreparedStatement pre;
        pre = con.prepareStatement(query);
        pre.setString(1, lname);
        pre.setString(2, fname);
        pre.setString(3, mname);
        pre.setString(4, address);
        pre.setString(5, birthday);
        pre.setString(6, gender);
        pre.setString(7, age);
        pre.setString(8, contactno);
        pre.setString(9, contactperson);
        pre.setString(10, department);
        pre.setString(11, position);
        pre.execute();// execute
        con.close();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}
4

2 回答 2

5

问题是你的return陈述:

return DriverManager.getConnection(database, "", "");

由于该方法在此时终止(即返回),因此无法到达此行之后的任何内容。所以编译器抱怨。

于 2012-10-06T03:16:52.927 回答
0

假设这是您的整个代码,我在您的代码中看不到任何主要方法语句。您需要在某处的某个主要方法中创建此类的对象。

于 2012-10-06T03:23:37.590 回答