Question

I want to retrieve only data that have same date_Add and output it to the table_patients

Here is the code

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

     java.util.Date chooser= choose.getDate();
     java.sql.Date sqlchooser=new java.sql.Date(chooser.getDate());

     try{
     String sql="select * from Patients_Details where Date_Add='"+sqlchooser+"'";
     pst=conn.prepareStatement(sql);
     pst.setDate(1,sqlchooser);
     rs=pst.executeQuery();
     table_patients.setModel(DbUtils.resultSetToTableModel(rs));

     }
 catch(SQLException sql)
{   sql.printStackTrace(); }
} 

but i get this error

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at org.sqlite.PrepStmt.batch(PrepStmt.java:192)
at org.sqlite.PrepStmt.setObject(PrepStmt.java:245)
at org.sqlite.PrepStmt.setDate(PrepStmt.java:290)
at employeeJFrame.btn_GoActionPerformed(employeeJFrame.java:981)
at employeeJFrame.access$1300(employeeJFrame.java:18)
at employeeJFrame$13.actionPerformed(employeeJFrame.java:391)

error points to this line

    pst.setDate(1,sqlchooser);

Could someone please help? NB: I am a java begginer

Was it helpful?

Solution

There are a number of issues here.

  1. You're passing the day of month to a constructor that expects a milliseconds time value. This will result in your sqlchooser date being 1 to 31 milliseconds after midnight on 1970-01-01. If you look at the documentation, you'll see that java.util.Date.getDate()

    Returns the day of the month represented by this Date object...

    And the constructor java.sql.Date(long)

    Constructs a Date object using the given milliseconds time value...

    Also be aware that java.util.Date.getDate() is deprecated in favor of Calendar.get(Calendar.DAY_OF_MONTH).

  2. You're setting the date condition explicitly by constructing the SQL string with sqlchooser and are then attempting to set the date as parameter of the prepared statement (but the statement has no parameters). You only need to do one of these. Read to Using Prepared Statements to understand how parameters work.

The first issue will likely result in you receiving no results. The second issue should be causing an SQLException. Neither of these should cause any problem on the line you indicated, but I'd suggesting fixing these problems and moving on from there.

For work with dates going forward, you should consider using Joda Time rather than the base Java date/time API, which many regard as poorly designed.

UPDATE (post provision of exception details)

Post your edit, it's clear that you are receiving this error because you are trying to assign a parameter to a prepared statement that has no parameters (point 2 above). Parameters are represented by the '?' character in statements. See the linked documentation above for details.

If you look at the exception, you'll see it occurs in the org.sqlite.PrepStmt.batch method. The source of this method is:

private void batch(int pos, Object value) throws SQLException {
    checkOpen();
    if (batch == null) batch = new Object[paramCount];
    batch[batchPos + pos - 1] = value;
}

For your statement, paramCount==0, so the batch Object array is created with length 0. The last line then attempts to set the first element (0 + 1 - 1 = 0) to your passed Date object, which is why you get an ArrayIndexOutOfBoundsException.

Bottom line: Read and understand about prepared statement parameters at the link provided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top