I'm very confused working with dates could someone point me in the right direction on for the code below, it throws the following exception:
org.h2.jdbc.JdbcSQLException:Invalid value '11' for parameter "parameterIndex" [90008-193]
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.ZoneId;
public class Database {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement st = null;
String URL = "jdbc:h2:~/registDB";
String USER = "admin";
String PASSWORD = "password";
ZoneId z = ZoneId.systemDefault() ;
LocalDate currentDate = LocalDate.now(z);
LocalDate expiration = currentDate.plusDays(inputFld.getText()); //JTextField.getText()
java.sql.Date expirationDate = java.sql.Date.valueOf(expiration);
try {
Class.forName("org.h2.Driver").newInstance();
conn = DriverManager.getConnection(URL,USER,PASS);
String sql = "INSERT INTO data
(fullName,regNum,itemName,note,zHemjee,fee,
time,date,totalPay,expirationDate)"
+ "VALUES"
+ "(?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
// 1st index left not being modified on purpose which is ID auto_increment-ed
pst.setString(2, getFullName()); // Get methods are
// JTextField.getText() casted into proper data types
// except the 11th row which is throwing SQLException
pst.setString(3, getRegNum());
pst.setString(4, getItemName());
pst.setString(5, getNote());
pst.setInt(6, getzHemjee());
pst.setInt(7, getFee());
pst.setInt(8, getTime());
pst.setDate(9, java.sql.Date.valueOf(LocalDate.now()));
pst.setDouble(10, getTotalPay());
pst.setDate(11, expirationDate);
pst.executeUpdate();
pst.close();
conn.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Here are my SQL statement creating a table with it's column types:
CREATE TABLE IF NOT EXISTS data "
+"(id INT NOT NULL AUTO_INCREMENT," //int
+ " fullname varchar(30)," //String
+ " regNum varchar(10)," //String
+ " itemName varchar(30)," //String
+ " note varchar(30)," //String
+ " zHemjee int," //int
+ " fee number," //int
+ " time INT," //int
+ " date DATE," //Date
+ " totalPay BIGINT," //int
+ " expirationDate DATE);"); //Date