1 votes

JDBC Error: "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

I am trying to create a simple program in Java that can record, query and modify data from a database in MySQL. The program if I compile well, but when you submit the Query, I skip this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Code of the connection:

import java.sql.*;

public class ConexionBD {
Connection con;

public Connection conexion(){
   try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysqldb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC://localhost/notas","root","Marioloco456");
        System.out.println("Conexion exitosa");
   } catch (Exception e){
       System.out.println(e.getMessage());
   }return con;


}

Statement createStatement(){
    throw new UnsupportedOperationException("No soportado");
}

}

Code of the button:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try{
        PreparedStatement pps = cn.prepareStatement("INSERT INTO periodo1(corte1,corte2,bimestral) VALUE(?,?,?,?)");
        pps.setString(1, corte1.getText());
        pps.setString(2, corte2.getText());
        pps.setString(3, bimestral.getText());
        pps.executeUpdate();
        JOptionPane.showMessageDialog(null,"Datos guardados");
   } catch (SQLException ex) {
        Logger.getLogger(PantallaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    }

}                         

0voto

You have 4 parameters in the query "(?,?,?,?)" when you are only inserting 3 elements "corte1.corte2,bimestral"

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X