db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel John Debrunner <...@apache.org>
Subject Re: Executing command from a file via JDBC
Date Tue, 15 May 2007 03:41:38 GMT
Jim Newsham wrote:
>> -----Original Message-----
>> From: Bryan Pendleton [mailto:bpendleton@amberpoint.com]
>> Sent: Thursday, May 10, 2007 9:58 AM
>> To: Derby Discussion
>> Subject: Re: Executing command from a file via JDBC
>>> I didn't want to distribute derbytools.jar with my application.  I guess
>>> was looking for something that came with the core jdbc driver.
>> You could put something together yourself pretty easily:
>>    BufferedReader rdr = ...;
>>    String sql;
>>    Statement stmt = conn.createStatement();
>>    while ( (sql = rdr.readLine()) != null)
>>       stmt.executeUpdate(sql);
>> Would that serve your purposes?
>> thanks,
>> bryan
> I had a need to run sql scripts into our database at runtime as well.  I
> didn't find anything in the Derby API, so I whipped the following up.  We
> use it to create the database structure from a .sql resource, the first time
> our application runs (and also within unit tests, since we want a newly
> initialized copy of the database for each test).
> It's not bullet-proof (read the comments), but it was sufficient for our
> purposes.  If something like this doesn't already exist in Derby, it sure
> would be useful... with the documented deficiencies remedied, of course.

How about the runscript() method in ij?


public static int runScript(java.sql.Connection conn,
                             java.io.InputStream sqlIn,
                             java.lang.String inputEncoding,
                             java.io.OutputStream sqlOut,
                             java.lang.String outputEncoding)
                      throws java.io.UnsupportedEncodingException

     Run a SQL script from an InputStream and write the resulting output 
to the provided PrintStream. SQL commands are separated by a semi-colon 
';' character.

         conn - Connection to be used as the script's default connection.
         sqlIn - InputStream for the script.
         inputEncoding - Encoding of the script.
         sqlOut - OutputStream for the script's output
         outputEncoding - Output encoding to use.
         Number of SQLExceptions thrown during the execution, -1 if not 


View raw message