tags:

views:

135

answers:

5

I'm trying to generate some sql files in my java application. The application will not execute any sql statements, just generate a file with sql statements and save it.

I'd like to use the java.sql.PreparedStatement to create my statements so that i don't have to validate every string etc. with my own methods.

Is there a way to use the PreparedStatement without the calling java.sql.Connection.prepareStatement(String) function, because I don't have a java.sql.Connection?

+1  A: 

Not really. Preparing a statement in most cases means that it will be compiled by DBMS which is "hard" without connection.

http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

Anton
If it wasn't hard he wouldn't be asking ;)
aioobe
+1  A: 

I'm guessing that until you've got a sql connection, the parser won't know what rules to apply. I'm guessing that it's actually the SQL driver or even server that's compiling the sql statement.

Assuming your sql is simple enough, then how about using a cheap connection, like, say a sqlite connection.

SQLite will create a new database on the fly if the database you're attempting to connect to does not exist.

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;

}
blissapp
So, how can I create a connection without something to connect to?
r3zn1k
sqlite will create a new database for you if it doesn't exist - added a proof of concept code snippet
blissapp
Update, just learned that SQLite databases can be created in memory only with connection string "jdbc:sqlite::memory:"
blissapp
A: 

Try implementing PreparedStatement.

Example : class YourOwnClass implements PreparedStatement {

// 1. Do implement all the methods , 2. Get the minimal logic to implement from OraclePreparedStatement(classes12.jar) or sun.jdbc.odbc.JdbcOdbcCallableStatement

}

srinannapa
Just using regex or something like that would be much easier...
r3zn1k
@r3zn1k: The advantage of implementing this is that the code is then re-usable: it can either generate text SQL or directly execute the SQL. However I wouldn't be surprised if you didn't need that flexibility. :)
Mr. Shiny and New
A: 

The thread at http://forums.sun.com/thread.jspa?threadID=5434756 indicates that the answer is, no. Would be nice with a better reference though.

aioobe
+2  A: 

Take a look at this Java library: http://openhms.sourceforge.net/sqlbuilder/

PeterMmm
This library imports the package "com.healthmarketscience.common.util" which isn't included...
r3zn1k
You have to go around a little bit: http://openhms.sourceforge.net/common-util/
PeterMmm
Best solution. Thank a lot!
r3zn1k