我想在我的Java应用程序中生成一些sql文件。 应用程序将而不是执行任何sql语句,只需用sql语句生成一个文件并保存它。Java:准备没有连接的语句
我想使用java.sql.PreparedStatement
来创建我的语句,这样我就不必使用自己的方法来验证每个字符串等。
有没有办法使用PreparedStatement
没有调用java.sql.Connection.prepareStatement(String)
函数,因为我没有java.sql.Connection
?
我想在我的Java应用程序中生成一些sql文件。 应用程序将而不是执行任何sql语句,只需用sql语句生成一个文件并保存它。Java:准备没有连接的语句
我想使用java.sql.PreparedStatement
来创建我的语句,这样我就不必使用自己的方法来验证每个字符串等。
有没有办法使用PreparedStatement
没有调用java.sql.Connection.prepareStatement(String)
函数,因为我没有java.sql.Connection
?
不是。在大多数情况下准备一个声明意味着它将由DBMS编译,这是“硬”而没有连接。
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
如果不难,他不会问;) – aioobe 2010-05-20 12:40:05
我猜测,直到你已经有了一个SQL连接,解析器会不知道如何应用规则。我猜测它实际上是SQL驱动程序,甚至是编译sql语句的服务器。
假设你的sql很简单,那么如何使用廉价的连接,比如说一个sqlite连接。
如果您试图连接的数据库不存在,SQLite将立即创建一个新的数据库。
public Connection connectToDatabase() {
// connect to the database (creates new if not found)
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:mydatabase.db");
// initialise the tables if necessary
this.createDatabase(conn);
}
catch (java.lang.ClassNotFoundException e) {
System.out.println(e.getMessage());
}
catch (java.sql.SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
尝试实施PreparedStatement。
实施例:类YourOwnClass实现的PreparedStatement {
// 1.不要执行所有方法, 2.获取最小逻辑从OraclePreparedStatement(classes12.jar)或 sun.jdbc.odbc.JdbcOdbcCallableStatement实施
}
使用正则表达式或类似的东西会容易得多... – r3zn1k 2010-05-20 12:44:11
@ r3zn1k:实现这一点的优点是代码可以重用:它可以生成文本SQL或直接执行SQL。不过,如果你不需要这种灵活性,我不会感到惊讶。 :) – 2010-05-20 12:58:03
这是一个卑鄙狡猾的问题,谢谢完全它很容易应付:
public class PreparedStatementBuilder
{
private String sql; // the sql to be executed
public PreparedStatementBuilder(final String sql) { this.sql = sql; }
protected void preparePrepared(final PreparedStatement preparedStatement)
throws SQLException
{
// this virtual method lets us declare how, when we do generate our
// PreparedStatement, we want it to be setup.
// note that at the time this method is overridden, the
// PreparedStatement has not yet been created.
}
public PreparedStatement build(final Connection conn)
throws SQLException
{
// fetch the PreparedStatement
final PreparedStatement returnable = conn.prepareStatement(sql);
// perform our setup directives
preparePrepared(returnable);
return returnable;
}
}
要使用,只写一个匿名类中的覆盖空洞preparePrepared(PreparedStatement的):
final String sql = "SELECT * FROM FOO WHERE USER = ?";
PreparedStatementBuilder psBuilder = new PreparedStatementBuilder(sql){
@Override
protected void preparePrepared(PreparedStatement preparedStatement)
throws SQLException
{
preparedStatement.setString(1, "randal");
}};
return obtainResultSet(psBuilder);
的Presto!您现在可以使用PreparedStatement进行工作,但尚未构建它。下面是显示的最小样板,否则你不得不复制粘贴到国降临为例,每次你需要时间来写不同的说法:
public ResultSet obtainResultSet(final PreparedStatementBuilder builder)
throws SQLException {
final Connection conn = this.connectionSource.getConnection();
try
{
// your "virtual" preparePrepared is called here, doing the work
// you've laid out for your PreparedStatement now that it's time
// to actually build it.
return builder.build(conn).executeQuery();
}
finally
{
try { conn.close(); }
catch (SQLException e) { log.error("f7u12!", e); }
}
}
你真的真的不希望复制粘贴到处都有,你做?
这个库导入不包括在内的包“com.healthmarketscience.common.util”... – r3zn1k 2010-05-20 14:02:25
你必须有一点点:http://openhms.sourceforge.net/common-util/ – PeterMmm 2010-05-20 14:32:41
最佳解决方案非常感谢! – r3zn1k 2010-05-25 07:10:13