You are on page 1of 4

package connector; import import import import import import import import java.io.BufferedReader; java.io.File; java.io.FileReader; java.io.IOException; java.sql.

DriverManager; java.sql.ResultSet; java.sql.SQLException; java.util.Vector;

import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class SqlConnector { private private private private private private String hostname = ""; String port = ""; String dbname = ""; String user = ""; String password = ""; Connection conn = null;

public SqlConnector() { super(); System.out.println("Standardconstuctor: " + this.getClass().toSt ring()); } public SqlConnector(String hostname, String port, String dbname, String user, String password) { super(); this.hostname = hostname; this.port = port; this.dbname = dbname; this.user = user; this.password = password; try { System.out.println("* Try to load driver"); Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (Exception e) { System.err.println("* Unable to load driver."); e.printStackTrace(); } System.out.println("* Driver loaded successfully"); } public void connect(){ try { System.out.println("* Trying to connect"); String url = "jdbc:mysql://"+hostname+":"+port+"/"+dbname; conn = (Connection) DriverManager.getConnection(url, user, p assword); } catch (SQLException sqle) { System.out.println("* Can't connect"); System.out.println("* SQLException: " + sqle.getMessage()); System.out.println("* SQLState: " + sqle.getSQLState());

System.out.println("* VendorError: " + sqle.getErrorCode()); sqle.printStackTrace(); } System.out.println("* Connection established"); } public void disconnect() { try { this.conn.close(); System.out.println("* Connection closed"); } catch (SQLException sqle) { System.out.println("SQLException: " + sqle.getMessage()); System.out.println("SQLState: " + sqle.getSQLState()); System.out.println("VendorError: " + sqle.getErrorCode()); sqle.printStackTrace(); } } public void openBatch(String filepath) { try { Process p = Runtime.getRuntime().exec("cmd /c " + filepa th); p.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Vector<String[]> readFile(String filename) throws Exception { int zaehler = 0; int i=0,t=0; int begin=0,end=0; int z = 0; String line2, line; String[] create; Vector<String[]> finish= new Vector<String[]>(); try { File file = new File(filename); BufferedReader in = new BufferedReader(new FileReader(fi le)); if (!file.exists()){ throw new Exception("Die angegbene Datei exsisti ert nicht"); } if (!file.isFile()){ throw new Exception("Der angegebene Pfad ist kei ne Datei"); }

if(!file.canRead()) { throw new Exception("Die angegebene Datei kann n icht gelesen werden"); } while ((line = in.readLine()) != null) { z++; } BufferedReader in2 = new BufferedReader( new FileReader( file)); String[] feld = new String[z]; while ((line2 = in2.readLine()) != null) { feld[zaehler] = line2; //System.out.println(feld[zaehler]); if(feld[zaehler].contains("CREATE")) { i++; System.out.println(i); begin=zaehler; } else if(i!=0 && !(feld[zaehler].contains(";"))) { i++; System.out.println(i); } else if(feld[zaehler].contains(";")) { i++; end=zaehler; create=new String[i]; System.out.println(i); i=0; for(int j=begin; j<=end; j++ ) { create[i++]=feld[j]; } finish.add(create); for(int j=0; j<create.length; j++) System.out.println(create[j] + " ::: " + i); create=null; i=0; } zaehler++; } in2.close(); in.close(); return finish; } catch (Exception e) { e.printStackTrace(); throw new Exception(); } } public void execSqlFile(String filename) throws Exception { Statement stmt = (Statement) conn.createStatement(); String sqlC=""; Vector<String[]> vs=readFile(filename);

for(int i=0; i<vs.size(); i++) { String[] s=vs.elementAt(i); for(int j=0; j<s.length;j++) sqlC+=s[j]; stmt.execute(sqlC); sqlC=""; } } public static void main(String[] args) throws Exception { SqlConnector test= new SqlConnector("db4free.net","3306","nickpa tt","nickpatt","*********"); test.connect(); test.execSqlFile("create.sql"); test.disconnect(); } }

You might also like