Results 1 to 2 of 2
Thread: JDBC querys from directory
- 08-01-2011, 06:09 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
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.
- 08-01-2011, 06:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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
-
sqljdbc driver UNION querys
By sashaxiv in forum JDBCReplies: 9Last Post: 02-24-2011, 12:37 PM -
JDK Directory
By PhQ in forum EclipseReplies: 1Last Post: 03-21-2010, 03:54 AM -
phone directory
By nanna in forum New To JavaReplies: 3Last Post: 02-19-2009, 07:21 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks