0

i am having the script of tables in normal txt files also i have scripts for procedures and functions. Now, i want to just read that file from java and i want op fire that string (script) on DB.. is it possible.. i have written statements for all DML queries, but here i want to use DDL queries from Java.. can any one help me..

Wim Coenen
  • 64,994
  • 13
  • 151
  • 241
M.J.
  • 15,586
  • 27
  • 72
  • 97

2 Answers2

1

This is question is very similar to what you are asking.

Have also a look at iBatis ScriptRunner.

Community
  • 1
  • 1
kgiannakakis
  • 100,996
  • 27
  • 157
  • 193
0

Read the script with a BufferedReader (see some examples) appending each line to a StringBuilder. Then use JDBC to create Statement and call execute on it, with your stringbuilder object as a string argument.

//create StringBuilder "myProc" here, reading the script
//get Connection conn
//...
Statement stmt = conn.createStatement();
stmt.executeUpdate(myProc);

Check the Java Tutorial lesson on SQL Statements for Creating a Stored Procedure.

You need to handle delimiters and iterate over the script with this idea if your file contains more than one script.

JuanZe
  • 7,813
  • 42
  • 58