发现用con.prepareStatement(sql),感觉代码工整,也方便将sql提到常量类中,可是,对于log的显示很不方便,不能完整显示一个sql,有没有,简单,易用的方法  谢谢,
!!! 斑竹一楼是我的!!! 

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【moodoasis】截止到2008-06-26 12:17:12的历史汇总数据(不包括此帖):
    发帖数:36                 发帖分:1634               
    结贴数:35                 结贴分:1614               
    未结数:1                  未结分:20                 
    结贴率:97.22 %            结分率:98.78 %            
    值得尊敬
      

  2.   

    try{
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/caiwu", "root", "admin");
        PreparedStatement p=con.prepareStatement("select * from ?");
        p.setString(1, "hotleave");
        System.out.println(p.toString());
    }catch(Exception e){
        e.printStackTrace();
    }会打印com.mysql.jdbc.PreparedStatement@7d8483: select * from 'hotleave'
      

  3.   

    protected String asSql(boolean quoteStreamsAndUnknowns) throws SQLException {
    if (this.isClosed) {
    return "statement has been closed, no further internal information available";
    }

    StringBuffer buf = new StringBuffer(); try {
    for (int i = 0; i < this.parameterCount; ++i) {
    if (this.charEncoding != null) {
    buf.append(new String(this.staticSqlStrings[i],
    this.charEncoding));
    } else {
    buf.append(new String(this.staticSqlStrings[i]));
    } if ((this.parameterValues[i] == null) && !this.isStream[i]) {
    if (quoteStreamsAndUnknowns) {
    buf.append("'");
    } buf.append("** NOT SPECIFIED **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) {
    buf.append("'");
    }
    } else if (this.isStream[i]) {
    if (quoteStreamsAndUnknowns) {
    buf.append("'");
    } buf.append("** STREAM DATA **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) {
    buf.append("'");
    }
    } else {
    if (this.charConverter != null) {
    buf.append(this.charConverter
    .toString(this.parameterValues[i]));
    } else {
    if (this.charEncoding != null) {
    buf.append(new String(this.parameterValues[i],
    this.charEncoding));
    } else {
    buf.append(StringUtils
    .toAsciiString(this.parameterValues[i]));
    }
    }
    }
    } if (this.charEncoding != null) {
    buf.append(new String(
    this.staticSqlStrings[this.parameterCount],
    this.charEncoding));
    } else {
    buf
    .append(StringUtils
    .toAsciiString(this.staticSqlStrings[this.parameterCount]));
    }
    } catch (UnsupportedEncodingException uue) {
    throw new RuntimeException(Messages
    .getString("PreparedStatement.32") //$NON-NLS-1$
    + this.charEncoding
    + Messages.getString("PreparedStatement.33")); //$NON-NLS-1$
    } return buf.toString();
    }
    这个是mySql驱动中的toString方法调用的获得sql语句的方法
      

  4.   

    prepareStatement的话,因为有setDate之类的参数格式,JDBC是没有提供直接打印出完整sql的方法
    可以自己写一个Util类或者方法来用instance of解析参数类型,然后拼sql具体实现参考:
    http://www.blogjava.net/crazycy/archive/2006/07/22/59581.html另外一个方法是用P6Spy,不过这个是输出sql到指定log文件,在程序中可能不是很好控制
    这个LZ自己搜索一下吧
      

  5.   


    正解 MySQL 上好使,Ms SQL Server 2000 和其它数据库不知道好不好用。
      

  6.   

    3楼的不好用 只能打印出 
    DelegatingPreparedStatement@1054f93没有后面的,4楼5楼的意思,是没有,简单易用的方法了,是吗
      

  7.   

    用的是ORACLE,,,这个有关吗,connection,只是个接口阿
      

  8.   

    难道 PreparedStatement.toString(),各个驱动,实现的不一样吗
      

  9.   


    PreparedStatement 只是个接口,不同数据库的驱动实现肯定存在差别。