Invoking .SQL file from JAVA

Hi All,
Anyway of calling .SQL files from JAVA ??
thanks in advance..

What do you mean by calling?
Are you talking about a stored procedure? Then yes.typo !!
i meant invoking .SQL script only...
No not stored procedure..i mean running or invoking a sql file containing sql statements(inserts etc)

Similar Messages

  • How to run SQL files from Java?

    Hi,
    Can someone point me towards a link on how to run sql files in Java? Thanks.
    P.S...if I've been completely blind please go easy on me!

    Sorry forgot the formating code thingy
    public static boolean executeScript(File script, Connection conn){
        boolean success = true;
        success = script.exists();
        if(success){
          DEBUG.print("ES::READING SCRIPT:" + script.getAbsolutePath());
          StringBuffer buffer = new StringBuffer();
          success=readScript(script,buffer);
          if(success){
            try{
              String creationScript = buffer.toString();
              Statement st = conn.createStatement();
              int start = 0;
              int end = 0;
              while (end != -1 && start < creationScript.length()) {
                end = creationScript.indexOf("GO", start);
                if (end != -1) {
                  DEBUG.print(creationScript.substring(start, end));
                  st.executeUpdate(creationScript.substring(start, end));
                  start = end + 2; //2 is the length of "GO"
              st.close();
            }catch(Exception e){
              success=false;
              DEBUG.printStackTrace(e);
        }else{
          DEBUG.print("ES::SCRIPT FILE DOES NOT EXISTS");
          success=false;
        return success;
      public static boolean readScript(File script, StringBuffer buffer){
        boolean success = true;
        DEBUG.print("RS:: reading file :" + script.getAbsolutePath());
        try{
          InputStreamReader isr = new InputStreamReader(new FileInputStream(script),"UTF16");
          int ch;
          while ( (ch = isr.read()) > -1) {
            buffer.append( (char) ch);
          if (isr != null)
            isr.close();
        }catch(Exception e){
          success=false;
          DEBUG.printStackTrace(e);
        return success;
      }

  • Run a sql file from java

    I have a SQL file named myfile.sql which contains something as follows:-Declare
    Begin
    insert into alphaweb values (1);
    end;
    .What I am looking for is a way to execute this through a java code. e.g. something like reading the String from the the file and executing it.
    public static boolean executeScript(File script, Connection conn) {
              boolean success = true;
              success = script.exists();
              if (success) {
                   System.out.println("ES::READING SCRIPT:" + script.getAbsolutePath());
                   StringBuffer buffer = null;
                   try {
                        buffer = readFileAsString("myfile.sql");
                   } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                   }//new StringBuffer();
                   //success = readScript(script, buffer);
                   if (null!=buffer) {
                        try {
                             String creationScript = buffer.toString();
                             Statement st = conn.createStatement();
                             int start = 0;
                             int end = 0;
                             while (end != -1 && start < creationScript.length()) {
                                  end = creationScript.lastIndexOf ('/');
                                  if (end != -1) {
                                       System.out.println(creationScript.substring(start, end));
                                       st.executeUpdate(creationScript.substring(start, end));
                                       start = end + 2; // 2 is the length of "GO"
                             st.close();
                        } catch (Exception e) {
                             success = false;
                             System.out.println(e);
              } else {
                   System.out.println("ES::SCRIPT FILE DOES NOT EXISTS");
                   success = false;
              return success;
         }But it is failing any ideas if it is possible and what am I doing wrong?

    I believe the sample that you provided only has a single statement in it.
    Do the files only contain one block like that?
    If yes then you should be able to process them as a single statement. Just drop the terminator at the end (maybe wrap it in another block statement...)
    If not then your solutions are.
    1. Parse the file yourself.
    2. Use the oracle command line tool and feed it to that.

  • How to execute sql file from servlet

    Hi,
    I am using JSP, Servlets and Oracle 8i in my application. I want to execute .sql file from java code. Is it possible to do that,
    as we execute .sql file from sql plus prompt.
    Suppose I have abc.sql file and I want to execute it from java code.
    If any body have the solution then pl. reply with sample code.
    Thanks,
    Rajesh

    If any body have the solution then pl. reply with
    sample code.No, no, dec - s/he doesn't want the actual solution, but the full code!
    /k1

  • Calling SQL LOADER From Java Program

    Hi
    I need to invoke sql loader from inside a Java Class.
    Any Pointers in this regards will be really appreciated.
    Thanks
    Vishal

    You can create two different files which has sqlldr command defined....
    1. loader.sh
    2. loader.bat
    and in the depending on the OS execute one...
             String command = "";
             if(System.getProperty("os.name").equals("Windows 2000")){
                    //if os is windows 2000  then execute bat file
                     command =  <<bat file path>>
              }else{
                      command = <<shell path>>
              Runtime rt = Runtime.getRuntime();
              Process p = rt.exec(command);
              int i = p.exitValue();
             

  • Calling a PL/SQL function from java

    I would like to call a pl/sql function from java. My pl/sql function is taking arrays of records as parameters. How do i proceed? Which specific oracle packages do I have to import?
    Please send an example.
    TIA,
    Darko Guberina

    Documentation here: http://download-uk.oracle.com/docs/cd/B14117_01/java.101/b10983/datamap.htm#sthref185
    says JPublisher can publish records too.
    But when I change the example given at http://download-uk.oracle.com/docs/cd/B14117_01/java.101/b10983/datamap.htm#sthref190 as following:
    PACKAGE "COMPANY" AS
    type emp_rec is record (empno number, ename varchar2(10));
    type emp_list is varray(5) of emp_rec;
    type factory is record (
    name varchar(10),
    emps emp_list
    function get_factory(p_name varchar) return factory;
    END;
    then I see <unknown type or type not found> at sql or java files generated. Any ideas?

  • Calling oracle SQL Loader from java program

    Hi,
    I want to insert data into oracle tables from a text files containing fixed length fields. I am using SQLLoader to achieve this. The requirement is to call the SQLLoader within the java program.
    Does anyone know how to call SQL Loader from java program?
    Thanks,
    Varsha

    Simply "exec" the sqlldr program with the parameters you need to pass to it.
    Runtime.exec (........)
    Try to pass all parameters needed including the username and password, and use the option "silent" to avoid output, and the option "log" to have a log file of the modifications that sqlldr has done to the database.

  • Invoking Microsoft Word from Java code

    I want to invoke Microsoft word from Java code. How can i do this plz ............... Help

    do something along these lines.... here is an example with notepad
    create a .bat file with the commmand in to run notepad for example
    notpad c:\test.txtthis command calls notepad and passes it the argument C:\test.txt which is the file and it's location, that I want to open.
    The Java program
    import java.util.*;
    import java.io.*;
    public class Run
    public static void main(String args[])
        try
           //the name of the batch file with your commands in
           String command ="runprog.bat";
           //get the runtime 
           Runtime rt  = Runtime.getRuntime();
           //run the bat file  
           Process proc = rt.exec(command);
          //wait for the program to exit i.e notepad then get the return code
          int exitVal = proc.waitFor();
          //print out the exit value
          System.out.println(2THe Process exit value is: " + exitVal);
      catch(Throwable t)
         t.printStackTrace();
    }have a look at this
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
    and this
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html

  • Invoking batch file through java pgm

    hi
    i want to invoke run.bat file from java pgm, i have written following code
    String[] cmd = new String[4];
              cmd[0] = "cmd.exe";
              cmd[1] = "/C";
              cmd[2] = "start";
              cmd[3] = "run.bat";
              Process p=Runtime.getRuntime().exec(cmd);
              p.waitFor();
    It's working without error but it is not giving any output.
    plz guide
    Also give me syntax to run commands of cmd through java pgm.
    Thanks

    Hi,
    What type of output you expected. If you want to cmd output from process then use following code
    String[] cmd = new String[4];
    cmd[0] = "cmd.exe";
    cmd[1] = "/C";
    cmd[2] = "start";
    cmd[3] = "run.bat";
    Process p=Runtime.getRuntime().exec(cmd);
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
         pingResult += inputLine;
    in.close();
    p.waitFor();Edited by: sagar_birari on Mar 26, 2008 4:47 AM

  • How can i call forpro prg file from java

    Hai friends,
    I have a doubt,clear it.
    how can i call forpro prg file from java file
    by,
    N.Vijay

    Thanks to your reply,
    I have some print statements in my foxpro program file.
    Then i like to invoke that foxpro file from my java file
    This want i want..,
    by,
    N.Vijay

  • Invoke beanshell methods from java

    Hello
    I'm learning beanshell and using it to write scripts that are ran in JDK 6.
    My question is how to invok beanshell methods from java source codes.
    My codes is:
    import javax.script.*;
    public class InvokeFunctions {
    public static void main (String[] args)throws ScriptException, NoSuchMethodException
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine bshEngine = sem.getEngineByName("beanshell");
    String script = "public void sayHello()"+"{print (\"sayHello() is a method in bsh script\");}";
    bshEngine.eval(script);
    Invocable inbshEngine = (Invocable)bshEngine;
    inbshEngine.invokeFunction("sayHello");
    I defined a method "sayHello()" using beanshell, but i just can't invoke it in Java.
    I got an error msg said:
    Exception in thread "main" java.lang.IllegalAccessError: tried to access method bsh.NameSpace.getThis(Lbsh/Interpreter;)Lbsh/This; from class bsh.engine.BshScriptEngine
    Any one has any idea about it?
    Thanks

    Look at the Javadoc documentation of IllegalAccessError. It says:
    Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.
    Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
    Maybe recompiling all your sources (make sure you delete all existing *.class files) will help?

  • Invoke a CFC from Java

    Can you give me an example of where or why you would invoke a CFC from Java?  I have used CF to call Java and then having Java return "data" back to the CFC but not sure I know why I would want my Java to invoke a CFC.
    Thanks

    Hi,
    how to invoke a exe file from a java program. (
    operating system: windows )
    is it possible ?Yep.
    Navigate yourself around pitfalls related to the Runtime.exec() method

  • Deleting files from Java app in XP

    Greetings --
    I'm currently developing a standalone app that, among other things, needs to read an image in from a remote server and somehow translate it back into an image file. Right now, I'm reading the information in and saving it to file so that I can manipulate it -- but when my program is done, I want to "clean up" by deleting this image file. I haven't been able to find a good way to delete a file from Java - is there a way, even by somehow invoking the old DOS "del" command? Thanks in advance!
    Yours,
    Cheryl/muppetaphrodite

    Simplest way for a file:
    // Assuming String fileName;
    new java.io.File(fileName).delete();
    If you want to delete a directory recursively, you'll have to write a recursive method to open a directory using File.listFiles() and checking File.isDirectory() as your guides. Remember to walk a directory completely first, deleting everything under it, before deleting the directory itself.

  • Invoking python program from java

    I want to execute a parser program(which is in python) that parses log file from java interface.The design of Java user interface has to be such that the log file is obtained from the user into a text field.The file name has to be read from the text field and this has to be taken by the python log parser program.How can this be implemented?

    You can invoke an external program using:
    Runtime.getRuntime().exec("bla.exe");
    See: http://java.sun.com/j2se/1.4.1/docs/api/

  • Generation of xml file from java code

    hi,
    I want to manipulate data in a xml file with java code.I have read data from xml file and also changed it. But i am unable to covert it again in xml file from java code. Can you please tell me how i can do this?

    Let me know which parser are you using currently for reading xml files so that i assist you. For now, you can refer to STAX Parser API under this link
    http://java.sun.com/webservices/docs/1.6/tutorial/doc/SJSXP3.html

Maybe you are looking for

  • Mac min wont start

    gray screen comes up with the apple and the loader symbol spins but just does not come in, few days before i was having issues with the drive and graphics, is this a goner? it is a g4 1.5 512mb superdrive model

  • Shared variable engine network published source issue

    Hello, I am attempting to use the shared variable engine to share some simple stings over a network between a Windows 7 computer and a Windows XP computer.  I got through the shared variable example just fine and can share variables between VIs on a

  • Label component doesn't show required icon

    I have some JSP like this: <ui:label for="uiMiddleInitial" text="Middle Initial"/> <ui:textField binding="#{SomeForm.uiMiddleInitial}" columns="2" id="uiMiddleInitial" tabIndex="2" required="true"/> When the page renders the first time, the label is

  • CORBA Bug?

    Hi all, I got the following error.... Can't understand what happen? I can get the interface from the server. However, I cannot invoke any method. Any thought? Thank you, edu.cmu.nat.project6.server._SupervisorInterfaceStub:IOR:000000000000003849444c3

  • How do i sync my contacts from iCloud onto my mac

    I've installed iMessage into my macbook air, but i don't have any contacts.. i was wondering how i can sync my iCloud contacts (online) so they show up in the contacts (on my mac).