"시간대가없는 시간"데이터 유형이있는 테이블이 postgres 데이터베이스에 있으며 준비 문을 사용하여 삽입하려고 할 때 구문 오류가 발생합니다. 웹 페이지에서 입력을 사용하는 대신 새 java.sql.Time 객체를 만들려고했지만 여전히 동일한 오류가 발생합니다. 내가 도대체 뭘 잘못하고있는 겁니까?JDBC를 사용하여 PostgreSQL 시간 유형에 삽입하는 중 오류가 발생했습니다.
오류 :
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type time: "1970-01-01 +01:00:00"
테이블 :
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)
예약 자바 :
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 액세스 자바 :
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);
}
}
, 난 아무것도 볼 수 없습니다 :이 문제를 해결하는 좋은 방법은 내부적으로 PARAM 값으로
?
을 대체하기 위해 JDBC에서 사용하는toString()
방법을Time
에서 파생 된 클래스를 생성하고 무시하는 것입니다 코드가 잘못되었습니다. 'java.sql.Time'을 가져오고 다른 클래스는 가져 오지 않도록하십시오. – Bohemian