zoukankan      html  css  js  c++  java
  • 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()));
  • 相关阅读:
    构建自己的C/C++插件开发框架(四)——核心层设计和实现
    构建自己的C/C++插件开发框架(二)——总体功能
    对企业来说,要放在第一位的是什么
    深入理解C++的动态绑定和静态绑定
    构建自己的C/C++插件开发框架(三)——总体结构
    管道和过滤器
    层模式——面向模式的体系结构学习笔记
    使用信元流(TLVStream)规范、简化模块(C/C++)间交互
    推荐博客
    Android 操作系统的内存回收机制
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400598.html
Copyright © 2011-2022 走看看