1. 程式人生 > >錯誤筆記:JDBC中Statement和PreparedStatement對於Date型別寫入資料庫問題

錯誤筆記:JDBC中Statement和PreparedStatement對於Date型別寫入資料庫問題

今天寫JDBC使用Statement執行sql語句向oracle資料庫中插入Date型別資料時,遇到了一些問題:

首先Date類在java.util下和java.sql下都有,他們在控制檯上的答應分別是:

java.util.date:


java.sql.date:


接下來  我們需要跟資料庫建立連線,把我們寫的POJO類中的資料插入oracle資料庫中

POJO類:

package com.briup.day2;

import java.util.Date;

public class Student {
   private  long id;
   private   String name;
   private Date date;
public long getId() {
	return id;
}
public void setId(long id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Date getDate() {
	return date;
}
public void setDate(Date date) {
	this.date = date;
}
@Override
public String toString() {
	return "Studnet [id=" + id + ", name=" + name + ", date=" + date + "]";
}
   

}
接著要把POJO類物件中的資料拿出來插入資料庫,需要對應資料庫中的表

建student表:

create table student(
	id number primary key,
	name varchar2(100),
	birthday date
);

對應的把物件中的資料插入資料庫的操作:

首先要想日期型別要怎麼往資料庫中插入,我的oracle時Session是中文格式,必須要插入xx-xx月-xx型別的日期,而我的POJO類中Date時java.util.date類,所對應的日期不符合規則.因此需要先寫一個方法把java.util.date類轉換成java.sql.date型別. java.sql.date型別建立物件時,需要傳一個long型別的日期,因此呼叫java.util.date.getTime()方法來獲得,傳入:

   public   java.sql.Date   getDate(long time){
    	 return   new java.sql.Date(time);
    	
    }
但是這樣轉換後的日期格式時"xx-xx-xx",因此我一直報一個數據型別不符合的錯.那怎麼辦呢?首先用toString()將Date型別轉化成字串,在使用SQL語句to_date('date','yyyy-mm-dd')方法,將"xx-xx-xx"轉換成"xx-xx月-xx"的字串方可插入資料庫:
public class StatementTest {
	private   String driver ="oracle.jdbc.driver.OracleDriver";
	private   String url ="jdbc:oracle:thin:@127.0.0.1:1521:XE";
	private  String user=name
	private  String pass=password;
	
   public  void  StTestInsert(Student  s){
	   Connection  conn=null;
	   Statement  st =null;
	   try {
		     Class.forName(driver);
		     conn  =
		    		 DriverManager.getConnection(url, user, pass);
		     System.out.println(conn);
	         st=conn.createStatement();
	         long id  = s.getId();
	         String name  = s.getName();
	         java.sql.Date date  = getDate(s.getDate().getTime());
	         String   date1 = date.toString();
	         String  sql="insert into student  values("+id+",'"+name+"',to_date('"+date1+"','yyyy-mm-dd'))";
	         System.out.println(sql);
	         st.execute(sql);
	          System.out.println("執行成功");
	   } catch (Exception e) {
		e.printStackTrace();
	}finally{
		 if(st!=null){
			 try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 if(conn!=null){
			 try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		
	}
	   
   }
    public   java.sql.Date   getDate(long time){
    	 return   new java.sql.Date(time);
    	
    }

這樣可以將date正確的插入資料庫中

接著時prepareStatement:

prepareStatement中有對應的方法,getDate()  裡面需要傳的就是java.sql.date型別的資料,因此不需要麻煩的轉換:

  public  void preTestInsert(Student s){
	   Connection  conn=null;
	   PreparedStatement  pst =null;
	   try {
		     Class.forName(driver);
		     conn  =
		    		 DriverManager.getConnection(url, user, pass);
		     System.out.println(conn);
	         long id  = s.getId();
	         String name  = s.getName();
	         java.sql.Date date  = getDate(s.getDate().getTime());
	      
	       //構建PreparedStatement 物件  ,這個物件提供預編譯功能
	         //可以幫我們把sql語句傳送到資料庫
	         String sql  ="insert into  student values(?,?,?)";
	         pst=conn.prepareStatement(sql);
	         pst.setLong(1, id);
	         pst.setString(2, name);
	         pst.setDate(3, date);
	         pst.execute();
	         System.out.println("執行成功");
	   } catch (Exception e) {
		e.printStackTrace();
	}finally{
		 if(pst!=null){
			 try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 if(conn!=null){
			 try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		
	}
   }

總結下來,statement插入資料時比較麻煩,如果資料型別太多的話,字串的拼接就會很長,稍不留神就會出錯,因此覺得還是preparedStatement比較好用.

用statement時需要特別注意日期的轉換問題.