0

I am having problems passing data from my GUI to MySQL database. I am collecting information from JTextFields and JDateChooser, into the database. Everything else works, except the date. I tried multiple methods that I found online, and none of them have worked. I also checked the table, to ensure that the "DATE" data type is enabled in my "patientinfo" Table. If I remove the JDateChooser, my query works. Otherwise, I will get this error message:

Java.lang.NullPointerException

I am including my source code with this message.

//Event handler adds records
//to the database

JButton subInfoBtn = new JButton("SUBMIT AND CONTINUE");

subInfoBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

            try {
                //Invokes "javaconnect" class
                //to link to MySQL database
                conn = javaconnect.ConnecrDb();

                //Invokes SQL Statement from "Connection" Object
                pst = conn.createStatement();

                //SQL query commands event handler to access
                //"patientinfo" table, to insert data to columns.
                pst.executeUpdate("INSERT into patientinfo(firstName, lastName, DOB, age, SSN, "
                                  + "address, phone,email, emergencycontact, emergencyphone) "
                                  + "VALUES" + "('"+firstTxt.getText() + "', '" + lastTxt.getText()
                                  + "', '" + DOBTxt.getDate() + "' ,'" + ageTxt.getText() + "', '"
                                  + ssnTxt.getText() + "', "+ " '" + addressTxt.getText() +"',
                                  '"+phoneTxt.getText()+"'  , '"+emailTxt.getText()+"'  ,
                                  '"+emergencyTxt.getText()+"'  , '"+emergPhoneTxt.getText()+"'  )");


                //Closes Database Connection
                conn.close();
                JOptionPane.showMessageDialog(null, "Pantient Information Saved Succesfully");

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    });

    subInfoBtn.setFont(new Font("Tahoma", Font.BOLD, 14));
    subInfoBtn.setBounds(305, 286, 220, 23);
    contentPane.add(subInfoBtn);

//Also, I have tried to use SimpleDateFormat and
//((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
//Without any luck.  I am also aware of using Prepare Statement to avoid SQL injections, but
//I would like to solve the JDateChooser bonding data dilema into MySQL database.
Tiny
  • 27,221
  • 105
  • 339
  • 599
Federico Chin
  • 3
  • 1
  • 1
  • 3
  • When posting a question, please reduce your code the smallest possible example that shows your problem/question, preferably a [SSCCE](http://sscce.org). – Basil Bourque Sep 06 '14 at 05:06

2 Answers2

1
java.sql.Date date = new java.sql.Date(dateChooser.getDate().getTime());

Convert Date to sql object then store in database. Hope it will work.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • Hi Ishwar, Thank you for your help. I already used the java.sql.Date class, and I'm still getting theJava.lang.NullPointerException. I don't know what else to do. I also tried different other methods and classes, and still, no joy. The same message. I have also passed parameters and if... else statements to the classes, in case the JDateChooser component is empty, and I still get the same outcome. Do you have any other ideas? Thank you again for helping me. – Federico Chin Sep 07 '14 at 16:35
1

try something like this Convert the java.util.Date you are taking input to java.sql.Date for storing it to Mysql database as mysql stores Date in the format yyyy-MM-dd

public static java.sql.Date convertUtilDateToSqlDate(java.util.Date date){
    if(date != null) {
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        return sqlDate;
    }
    return null;
}

Then do it his way

PreparedStatement statement = conn.prepareStatement("INSERT INTO patientinfo"+
                                     "(firstName, lastName, DOB, age, SSN, address," +
                                     "phone,email, emergencycontact, emergencyphone)"+
                                     "VALUES(?,?,?,?,?,?,?,?,?,?)");
statement.setTimestamp(1,sqlDate);
statement.setString(1,firstTxt.getText());
statement.setString(2,lastTxt.getText());
statement.setDate(3,convertUtilDateToSqlDate(DOBTxt.getDate()));
statement.setString(4,ageTxt.getText());
statement.setString(5,ssnTxt.getText());
statement.setString(6,addressTxt.getText());
statement.setString(7,phoneTxt.getText());
statement.setString(8,emailTxt.getText());
statement.setString(9,emergencyTxt.getText());
statement.setString(10,emergPhoneTxt.getText());
int i = statement.executeUpdate();
if(i>0){
    System.out.println("Successfull");
 }

I hope you will do the necessary Exception handling and connection closing

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • SparkOn, thank you for taking the time to help me with my jdatechooser dilemma. I tried to implement the h ava.sql.Date class and I still get the sameJava.lang.NullPointerException error. Do you have any other ideas of to solve this issue? I also made sure that my table has the "DATE" data type, and the columns order matches the same sequence as my source code, and nothing. – Federico Chin Sep 07 '14 at 17:10
  • In addition, I tried to implement PreparedStatement, and it does not work for me. I have another column in my table called "patientID", which generates an auto integer. Since I'm not declaring the "patientID" column in my query, dies it matter which index I declare on my query? For example, patientID is column 1 on the patientinfo table, but I just want the user to enter the rest of the data. Therefore, using thestatement.setString(1,firstTxt.getText()); would still be valid? Thank you again for your help. – Federico Chin Sep 07 '14 at 17:11
  • Hi again SparkOn. I managed to implement the PrepareStatement, but I am still having trouble to enter the date using the JDateChooser from the JCalendar library. I even tried the java.sql.Date class conversion that you mentioned before, and nothing. Thanks for your help again. – Federico Chin Sep 08 '14 at 04:16
  • having trouble like what? – SparkOn Sep 08 '14 at 05:21
  • I am still getting the same Java.lang.NullPointerException when I try to use the JDateChooser. If I remove the JDateChooser and the DOB column from my query, everything else works. – Federico Chin Sep 08 '14 at 05:41
  • that may be because your date is coming as null – SparkOn Sep 08 '14 at 05:44