线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException”

时间:2019-01-14 10:35:59

标签: java

尝试将数据插入mysql数据库,但继续收到此错误“线程“ AWT-EventQueue-0”中的异常” java.lang.NullPointerException”

我已经在youtube上观看并尝试了多个视频

连接

import java.sql.*;
public class Myconnection {

    public static Connection getConnection(){

      Connection conn = null;  
      try {
        Class.forName("com.mysql.jbdc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/report?zeroDateTimeBehavior=convertToNull","root","Rex123Rex");
      } catch(Exception ex) {
       System.out.println(ex.getMessage());
      }

      return conn;
    }
}

插入数据

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

        String fname = firstname_txt.getText();
        String lname = lastname_txt.getText();
        String dept = Department_txt.getText();
        String uname = username_txt.getText();
        String pass = String.valueOf(password_txt.getPassword());
        String con_pass = String.valueOf(passconfirm_txt.getPassword());

        PreparedStatement ps;
        String query = "insert into 'users'('firstname','lastname','department','username','password')VALUES(?,?,?,?,?)";

        try {
            ps = Myconnection.getConnection().prepareStatement(query);

            ps.setString(1,fname);
            ps.setString(2,lname);
            ps.setString(3,dept);
            ps.setString(4,uname);
            ps.setString(5,pass);

           if(ps.executeUpdate() > 0) 
           {
             JOptionPane.showMessageDialog(null,"New User Added");
           }
        } catch (SQLException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }

0 个答案:

没有答案