-1

I have this code below for connecting to the database named judicial but it's not working.

package Project;
import java.sql.*;

public class ConnectionProvider {
    public static Connection getCon()
    {
       try
       {
          Class.forName("com.mysql.jdbc.Drver");
          Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/judicial","root", "");
          return con;
       } 
      
    catch(Exception e)
    {
        return null;
    }
    }
}

this is the code for the button

import Project.ConnectionProvider;
import java.sql.*;
import javax.swing.JOptionPane;
public class addNewCriminalRecord extends javax.swing.JFrame {

                            

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String criminalID=jTextField1.getText();
        String name=jTextField2.getText();
        String contactNumber=jTextField3.getText();
        String age=jTextField4.getText();
        String gender=(String)jComboBox1.getSelectedItem();
        String address=jTextField5.getText();
        String crimeType=jTextField6.getText();
        String anyMajorCrime=jTextField7.getText();
        try{
            Connection con=ConnectionProvider.getCon();
            Statement st=con.createStatement();
            st.executeUpdate("insert into criminal values('"+criminalID+"','"+name+"','"+contactNumber+"','"+age+"','"+gender+"','"+address+"','"+crimeType+"','"+anyMajorCrime+"')");
            JOptionPane.showMessageDialog(null, "Successfully Updated!");
            setVisible(false);
            new addNewCriminalRecord().setVisible(true);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Please enter Judicial data in the correct format!");
        }
    }                                        

    

Please help on how or where I went wrong. Because whenever I run the pade and input some details, it gives me the caught error. I have xampp up and running.

Dan
  • 3,647
  • 5
  • 20
  • 26
Kudash
  • 3
  • 2
  • 1
    Please post the exception and read up on sql injection. – jurez May 28 '22 at 09:16
  • There can be any number of problems between the code that you are executing and the database. But you are throwing the exception away, instead of letting it inform you of the error that it is encountering. Use `System.err.println(e); e.printStackTrace();` in your catch blocks to learn more. – tucuxi May 28 '22 at 09:37
  • The exception caught is Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at addNewCriminalRecord.jButton2ActionPerformed(addNewCriminalRecord.java:159) at addNewCriminalRecord.access$100(addNewCriminalRecord.java:15) at addNewCriminalRecord$2.actionPerformed(addNewCriminalRecord.java:130) – Kudash May 28 '22 at 09:54
  • Which one is line 159? – dunni May 28 '22 at 10:05

1 Answers1

0

There is no need to add the Class.forName("com.mysql.jdbc.Drver"); maybe that is causing the problem, and use System.println(e); to know what is causing the problem instead of return; in catch

Iyad
  • 73
  • 5