在mysql中插入错误

时间:2013-12-30 11:34:35

标签: java mysql netbeans

我写了下面的代码!我想我在连接数据库时遇到问题!对?我无法将数据插入数据库!问题出在哪儿?我怎么解决呢?

以下是示例:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

    String uname = jTextField1.getText();
    String pass =  jTextField2.getText();
    String fname = jTextField3.getText();
    String lname = jTextField4.getText();
    String gender = (String) COMBOgender.getSelectedItem();
    String address = jTextArea1.getText();
    String email = jTextField5.getText(); 
    try
    {

    //Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
    Statement stat = con.createStatement();

    String insert = "insert into tbl_User (username,password,firstname,surname,gender,address,mail_address) VALUES ('"+uname+"','"+pass+"','"+fname+"','"+lname+"','"+gender+"','"+address+"','"+email+"')";

    stat.executeUpdate(insert);
    //stat.close();
    //con.close();
    JOptionPane.showMessageDialog(null,"Inserted Successfully!");

    }
    catch (SQLException | HeadlessException e)
    {
        JOptionPane.showMessageDialog(null,"Error occurred in inserting data");
    }

    ;// TODO add your handling code here:
}  

1 个答案:

答案 0 :(得分:0)

从这里下载MySQL java驱动程序。 http://dev.mysql.com/downloads/connector/j/5.0.html

提取存档并获取.jar文件。在NetBeans IDE中,转到项目资源管理器窗口,右键单击项目名称下的库,然后选择“添加Jar /文件夹”。选择刚解压缩的.jar文件。

然后,按如下方式修改您的代码。

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();

...

stat.close();
con.close();