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语句的方法
楼主【moodoasis】截止到2008-06-26 12:17:12的历史汇总数据(不包括此帖):
发帖数:36 发帖分:1634
结贴数:35 结贴分:1614
未结数:1 未结分:20
结贴率:97.22 % 结分率:98.78 %
值得尊敬
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'
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语句的方法
可以自己写一个Util类或者方法来用instance of解析参数类型,然后拼sql具体实现参考:
http://www.blogjava.net/crazycy/archive/2006/07/22/59581.html另外一个方法是用P6Spy,不过这个是输出sql到指定log文件,在程序中可能不是很好控制
这个LZ自己搜索一下吧
正解 MySQL 上好使,Ms SQL Server 2000 和其它数据库不知道好不好用。
DelegatingPreparedStatement@1054f93没有后面的,4楼5楼的意思,是没有,简单易用的方法了,是吗
PreparedStatement 只是个接口,不同数据库的驱动实现肯定存在差别。