db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Raymond Kroeker" <raym...@raykroeker.com>
Subject Re: Executing command from a file via JDBC
Date Tue, 15 May 2007 15:51:21 GMT
Hi Daniel,
  I'm just throwing around ideas at this point, but it would be awesome if
the code from runscript could be made into a system stored procedure.  Then
it's always there and runscript from ij could re-use the same functionality.

  Just a thought.

Raymond

On 5/14/07, Daniel John Debrunner <djd@apache.org> wrote:
>
> 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?
>
> runScript
>
> 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.
>
>      Parameters:
>          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.
>      Returns:
>          Number of SQLExceptions thrown during the execution, -1 if not
> known.
>      Throws:
>          java.io.UnsupportedEncodingException
>
> Dan.
>
>


-- 
---------------------------------------------------------
Raymond Kroeker

Mime
View raw message