I have a table with a “time without time zone” data type in a postgres database and I’m getting a syntax error when trying to insert using prepared statements. I’ve tried creating a new java.sql.Time object rather than using inputs from a web page and I am still getting the same error. What am I doing wrong?
Error:
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type time: "1970-01-01 +01:00:00"
Table:
Table "public.reservation"
Column | Type | Modifiers
------------+------------------------+--------------------------------------------------------------
res_id | integer | not null default nextval('reservation_res_id_seq'::regclass)
cust_id | character varying(50) | not null
date | date | not null
time | time without time zone | not null
num_people | integer | not null
Indexes:
"reservation_pkey" PRIMARY KEY, btree (res_id)
Foreign-key constraints:
"reservation_cust_id_fkey" FOREIGN KEY (cust_id) REFERENCES customer(cust_id)
Referenced by:
TABLE "reservation_table" CONSTRAINT "reservation_table_res_id_fkey" FOREIGN KEY (res_id) REFERENCES reservation(res_id)
Reservation Java:
public static boolean createReservation(String custID, Date date, Time time, int numPeople) throws ServletException {
//TODO add checking
//TODO need to add determing table and if reservation time is free
boolean succeeded = false;
//checks if reservation is possible to be booked
if(checkReservationPossible()){
//convet date to correct format for database
//SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd");
//String formattedDate = dateFormatter.format(date);
//convert time to correct format for database
//SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
//String formattedTime = timeFormatter.format(time);
DatabaseAccess db = new DatabaseAccess();
String sql = "INSERT INTO reservation (cust_id, date, time, num_people)"
+ "VALUES (?,?,?,?)";
LinkedList<Object> params = new LinkedList<Object>();
params.add(custID);
params.add(date);
params.add(time);
params.add(numPeople);
succeeded = db.runUpdateQuery(sql,params);
db.close();
}
return succeeded;
}
DB Access Java:
public boolean runUpdateQuery(String sql, LinkedList<Object> params) throws ServletException
{
// Using try {...} catch {...} for error control
try{
// Create a statement variable to be used for the sql query
//Statement statement = this.connection.createStatement();
// Perform the update
PreparedStatement pst = this.connection.prepareStatement(sql);
Iterator iter = params.iterator();
for(int i = 1;iter.hasNext(); i++){
Object curr = iter.next();
//TODO may need to add more for different types
if(curr instanceof String){
pst.setString(i, curr.toString());
}else if (curr instanceof Integer){
pst.setInt(i, (Integer)curr);
}else if (curr instanceof java.util.Date){
//convert to sql date
java.util.Date tempDate = (java.util.Date)curr;
java.sql.Date sqlDate = new java.sql.Date(tempDate.getTime());
pst.setDate(i, sqlDate);
}else if (curr instanceof Time){
pst.setTime(i, Time.valueOf("11:30:00"));
}
}
int numRows = pst.executeUpdate();
pst.close();
if(numRows > 0){
return true;
}else{
return false;
}
} catch (SQLException e){
// Deal with the error if it happens
throw new ServletException(String.format("Error: Problem running query... "), e);
}
}
You don’t need all that checking. I recommend you change you code to simply this:
JDBC knows what to do with all the various usual java types – no need to convert.
The only time you need to check the type is if you want to use some custom class and you want to get some JDBC compliant type from it. This is not the case here.