MYSQL连接因netbeans而失败

时间:2012-10-03 12:55:59

标签: mysql connection database-connection

  

可能重复:
  Unable to load com.mysql.jdbc.Driver

您好,我正在尝试更新我的表,但连接仍然失败。以下是我的代码:

try {
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver).newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull","root","P@typd79");
            Statement stmt = con.createStatement();
            String query = "INSERT INTO project_one.personal_info(ssn,name,street,city,state,zip,class,degree,s,imm) VALUES"
                    + "(" + social + "," + Name + "," + Address + "," + City + "," + State + "," + Zip
                    + "," + Year + "," + Degree + "," + HS + "," + IMM + ")";

            stmt.executeUpdate(query);
            JOptionPane.showMessageDialog(null, "New student added successfully");

        } catch (Exception e) {

            JOptionPane.showMessageDialog(null,"Data not saved: " + e,"Error",JOptionPane.ERROR_MESSAGE);

        } 

    }

连接名称和驱动程序是正确的,但我收到以下错误java.lang.classnotfound.exeption:com.mysql.jdbc.Driver.

我错过了什么?

3 个答案:

答案 0 :(得分:0)

看起来你缺少jdbc jar。

答案 1 :(得分:0)

您应该将mysql驱动程序jar添加到构建路径中。

答案 2 :(得分:0)

http://dev.mysql.com/downloads/connector/j/

添加jdbc mysql驱动程序