java 用PreparedStatement来setDate,用问号的形式给日期问号赋值

java 用PreparedStatement来setDate,用问号的形式给日期问号赋值
给下边这个语句的问号赋值:

Java代码
  1. String sql="SELECT * FROM jizhang.bookuse_book WHERE datecreated between ? and ? group by DATECREATED   ";  
String sql="SELECT * FROM jizhang.bookuse_book WHERE datecreated between ? and ? group by DATECREATED ";

Java代码
  1. String startYear = "";  
  2. String stopYear = "";  
  3. String str1 = "2005-01-01";  
  4. String str2 = "2005-01-31";  
  5. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
  6. Date date1 = new Date();  
  7. Date date2 = new Date();  
  8. try {  
  9.      date1 = sdf.parse(str1);  
  10.      date2 = sdf.parse(str2);  
  11.                   
  12.      startYear = sdf.format(date1);  
  13.      stopYear = sdf.format(date2);  
  14.                       
  15. } catch (ParseException e1) {  
  16.     // TODO Auto-generated catch block  
  17.      e1.printStackTrace();  
  18. }  
  19. pstmt.setDate(1, new java.sql.Date(date1.getTime()));  
  20. pstmt.setDate(2, new java.sql.Date(date2.getTime()));




用下面的方法可以将JAVA的STRING 转化成CLOB类型,不过好像仅限于ORACLE,其他的数据库上我没有试过。
private CLOB getCLOB( String clobData,Connection conn )
throws Exception {
CLOB tempClob = null; try {
// create a new temporary CLOB

tempClob = CLOB.createTemporary(getNativeConnection(conn) , false,
CLOB.DURATION_SESSION ); // Open the temporary CLOB in readwrite mode to enable writing
tempClob.open( CLOB.MODE_READWRITE );
// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream( ); // Write the data into the temporary CLOB
tempClobWriter.write( clobData ); // Flush and close the stream
tempClobWriter.flush( );
tempClobWriter.close( ); // Close the temporary CLOB
tempClob.close( ); } catch ( Exception exp ) {
// Free CLOB object
throw exp;
//do something
}
return tempClob;
}

如果使用连接池来获得数据库连接,有可能需要将数据库连接进行一下转化,使用以下代码:
private static Connection getNativeConnection(Connection con) throws SQLException { if (con instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate(); // For some reason, the innermost delegate can be null: not for a
// Statement''''s Connection but for the Connection handle returned by the pool.
// We''''ll fall back to the MetaData''''s Connection in this case, which is
// a native unwrapped Connection with Commons DBCP 1.1. return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
}
return con;
}String startYear = ""; String stopYear = ""; String str1 = "2005-01-01"; String str2 = "2005-01-31"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = new Date(); Date date2 = new Date(); try { date1 = sdf.parse(str1); date2 = sdf.parse(str2); startYear = sdf.format(date1); stopYear = sdf.format(date2); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } pstmt.setDate(1, new java.sql.Date(date1.getTime())); pstmt.setDate(2, new java.sql.Date(date2.getTime()));

posted on 2009-05-21 15:48  dhj  阅读(628)  评论(0编辑  收藏  举报

导航