Results 1 to 2 of 2
  1. #1
    B13ZT is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default JDBC querys from directory

    I am making a java program that workt with JDBC to contact a database and send querys from a directory to it and get the result back.
    How can i select all the querys that i have in a directory?
    Because now i think i only select test.sql
    Also the output has to be saved in a cvs file. I don't know how i should do that, is it just create_new???


    Btw if you have any remarks on the code please share

    This is the code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.Writer;
    import java.sql.SQLException;
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.Statement;
    
    public class Database {
    
    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    
    static
    {
    try
    {
    Class.forName(DRIVER_NAME).newInstance();
    System.out.println("*** Driver loaded");
    }
    catch(Exception e)
    {
    System.out.println("*** Error : "+e.toString());
    System.out.println("*** ");
    System.out.println("*** Error : ");
    e.printStackTrace();
    }
    
    }
    
    private static final String URL = "jdbc:jtds:sqlserver://;DatabaseName=";
    private static final String USER = "root";
    private static final String PASSWORD = "root";
    private static String INSTRUCTIONS = new String();
    
    public static Connection getConnection() throws SQLException
    {
    return DriverManager.getConnection(URL, USER, PASSWORD);
    }
    
    public static void resetDatabase() throws SQLException
    {
    String s = new String();
    StringBuffer sb = new StringBuffer();
    
    try
    {
    FileReader fr = new FileReader(new File("test.sql"));
    
    
    BufferedReader br = new BufferedReader(fr);
    
    while((s = br.readLine()) != null)
    {
    sb.append(s);
    }
    br.close();
    
    
    String[] inst = sb.toString().split(";");
    
    Connection c = Database.getConnection();
    Statement st = c.createStatement();
    
    for(int i = 0; i<inst.length; i++)
    {
    
    if(!inst[i].trim().equals(""))
    {
    st.executeUpdate(inst[i]);
    System.out.println(">>"+inst[i]);
    }
    }
    
    }
    catch(Exception e)
    {
    System.out.println("*** Error : "+e.toString());
    System.out.println("*** ");
    System.out.println("*** Error : ");
    e.printStackTrace();
    System.out.println("################################################");
    System.out.println(sb.toString());
    }
    
    }
    
    }
    Last edited by B13ZT; 08-01-2011 at 07:06 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    You need to post code in code tags.
    Without that then forum loses any formatting your code may have had, making it very difficult to read.

    For reading multiple files from a directory then reate a File objecvt that points at that directory and use whatever method it is that returns a List of Files. See the API.

    I can't tell with the rest of your stuff due to the lack of formatting, but presumably these files contain single SQL statements that are not dependent on each other (like scripts).

Similar Threads

  1. sqljdbc driver UNION querys
    By sashaxiv in forum JDBC
    Replies: 9
    Last Post: 02-24-2011, 12:37 PM
  2. JDK Directory
    By PhQ in forum Eclipse
    Replies: 1
    Last Post: 03-21-2010, 03:54 AM
  3. phone directory
    By nanna in forum New To Java
    Replies: 3
    Last Post: 02-19-2009, 07:21 AM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •