我的java没有错误,但没有运行

时间:2012-10-06 03:11:06

标签: java jdbc

我是这种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);
    }
}

2 个答案:

答案 0 :(得分:5)

问题是你的return声明:

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

此行之后无法到达,因为此方法终止(即返回)。所以编译器抱怨。

答案 1 :(得分:0)

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