Java记录没有用黄线保存在mysql数据库中

时间:2017-02-18 23:25:03

标签: java mysql

我正在研究一个简单的java应用程序来保存到mysql数据库中。它工作正常,直到我的一些代码开始出现黄线。然后记录无法再次保存在数据库中,这是先前保存的。

然后" con"和"例外"开始显示黄色错误行。错误代码说我应该使用"尝试"在连接上,我做了,仍然没有工作。请帮忙。感谢

我的代码:

    try {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url, user, pass);
        String sql = "insert into biodatum " + "(firstname, lastname,       address, occupation, phone, email, image, gender, religion)"
        + "values (?,?,?,?,?,?,?,?,?)";
        PreparedStatement pst = con.prepareStatement(sql);
        //InputStream is  = new FileInputStream(new File(s));
        pst.setString(1, firstname.getText());
        pst.setString(2, lastname.getText());
        pst.setString(3, address.getText());
        pst.setString(4, occupation.getText());
        pst.setString(5, phone.getText());
        pst.setString(6, email.getText());
        pst.setBytes(7, image_person);
        pst.setString(8, gender.getSelectedItem().toString());
        pst.setString(9, religion.getSelectedItem().toString());



        JOptionPane.showMessageDialog(this, "Record Saved");
        firstname.setText("");
        lastname.setText("");
        address.setText("");
        occupation.setText("");
        phone.setText("");
        email.setText("");
        image_path.setText("");

        pst.close();
        con.close();

    }
    catch (Exception e){
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
} 

1 个答案:

答案 0 :(得分:0)

执行准备好的声明的内容是什么?

如果您的代码在此之前有效,则无意中删除了一些重要内容,例如执行准备语句的代码行,它应该看起来像:

pst.executeUpdate();

因此,您的代码应如下所示:

..................................
..................................
String sql = "INSERT INTO biodatum " + "(firstname, lastname, address, occupation, phone, email, image, gender, religion) "
        + " VALUES (?,?,?,?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, firstname.getText());
pst.setString(2, lastname.getText());
pst.setString(3, address.getText());
pst.setString(4, occupation.getText());
pst.setString(5, phone.getText());
pst.setString(6, email.getText());
pst.setBytes(7, image_person);
pst.setString(8, gender.getSelectedItem().toString());
pst.setString(9, religion.getSelectedItem().toString());
pst.executeUpdate();  // ************ HERE ***************
..................................
..................................