1

i'm trying to serialize a prepared statement and store it in a file as object but the object created by my sql driver is of type 'SQLServerPreparedStatement' and is not serializable so i tried to extend it but it's giving me an error when overriding its constructor saying that the constructor is not visible. any ideas on how to solve this? PS: my main goal is to store prepared statement objects in file and retrieve them later as prepared statements. below is my code:

public class WMPreparedStatements extends SQLServerPreparedStatement
{

WMPreparedStatements(SQLServerConnection arg0, String arg1, int arg2,
        int arg3) throws SQLServerException {
    super(arg0, arg1, arg2, arg3);
    // TODO Auto-generated constructor stub
}

}
Gilbert
  • 212
  • 1
  • 13

2 Answers2

2

The JDBC objects (Connection, PreparedStatement, ResultSet etc) are not declared as extending Serializable. The PreparedStatement that you have in your program is the implementation that is provided by your JDBC driver.

Making this PreparedStatement serializable means that you will be able to save it in a file and then restore it from the file. But you should do the same also for the Connection that created it (in your case SQLServerConnection). The JDBC connection is strongly dependent to the underlying network communication. Making a PreparedStatement serializable means that in a scenario you would execute the PreparedStatement inside a transaction, then you would serialize the PreparedStatement without committing and later you would be able to deserialize the PreparedStatement and commit. Such a scenario wouldn't be possible.

Why do you want to serialize the PreparedStatement? Isn't it enough to store the SQL string and the parameters and then read them from the file, open a new connection, create a new PreparedStatement object with the statement that you read, set its parameters and execute it?

nakosspy
  • 3,904
  • 1
  • 26
  • 31
  • i see what you mean and i guess you're probably right, i wish i can easily print them because the toString() implementation in SQLServerPreparedStatement class does not print it (i think it prints the object reference). – Gilbert May 21 '13 at 00:04
  • 1
    The bad news is http://stackoverflow.com/questions/2382532/how-can-i-get-the-sql-of-a-preparedstatement. – nakosspy May 21 '13 at 00:22
  • When I create a PreparedStatement, I keep a map that pairs each PreparedStatement object with its sql statement. Maybe you could do something like that. – nakosspy May 21 '13 at 00:23
  • yes it is bad news.. Well i just need a way to persist or save my prepared statements data on the hard disk not in memory in case i failed from the first time to execute them for any reason in order to reuse it in the next run of my application. i looked around and tried log4jdbc but i prefer not to use third party libraries while i still can in addition to some complicated setup. what in your opinion would be a good design to serve my need? – Gilbert May 21 '13 at 06:51
0

well it was impossible to store prepared statement objects in a file and maintain the data in this way, i had to make a workaround. i stored the statements that i build and that are not set yet (having '?') along with the parameters appended to it separated by a special and unique set of characters, so when i read the file the next time, each query with its parameters are on a separate line, i parse each line to get the values and then store the query in a prepared statement and set its parameters.

Gilbert
  • 212
  • 1
  • 13