1 6 7 package com.quikj.application.utilities.postinstall; 8 9 import java.sql.*; 10 import java.io.*; 11 import java.util.*; 12 13 17 public class DBAccess 18 { 19 private Connection connection; 20 21 22 public DBAccess(String driver, 23 String host, String user, String password, String db) 24 throws ClassNotFoundException , InstantiationException , IllegalAccessException , SQLException 25 { 26 Class.forName(driver).newInstance(); 27 login(host, user, password, db); 28 } 29 30 public void login(String host, 31 String user, 32 String password, 33 String database) throws SQLException 34 { 35 String url_str; 37 38 if (host.equals("localhost") == true) 39 { 40 host = "127.0.0.1"; 41 } 42 43 url_str = new String ("jdbc:mysql://" + host + "/" + database); 44 connection = DriverManager.getConnection(url_str, user, password); 45 } 46 47 public boolean databaseAlreadyExists() 48 { 49 try 50 { 51 Statement stmt = connection.createStatement(); 52 ResultSet rslt = stmt.executeQuery("show databases"); 53 54 while (rslt.next() == true) 55 { 56 String db = rslt.getString(1); 57 if (db.equals("ace") == true) 58 { 59 return true; 60 } 61 } 62 63 return false; 64 } 65 catch (SQLException ex) 66 { 67 return false; 68 } 69 } 70 71 public String executeSQLStatements(String path) 72 { 73 File file = new File(path); 74 try 75 { 76 FileReader freader = new FileReader(file); 77 78 StringBuffer buffer = new StringBuffer (); 79 int count = 0; 80 char[] read_buffer = new char[100]; 81 while(count >= 0) 82 { 83 count = freader.read(read_buffer); 84 if (count >= 0) 85 { 86 buffer.append(read_buffer, 0, count); 87 } 88 } 89 90 freader.close(); 91 92 return processQuery(buffer.toString()); 94 } 95 catch (IOException ex1) 96 { 97 return "IO error occured while reading file"; 98 } 99 } 100 101 private String processQuery(String command) 102 { 103 StringTokenizer strtok = new StringTokenizer(command, ";"); 104 int num_tokens = strtok.countTokens(); 105 for (int k = 0; k < num_tokens; k++) 106 { 107 String token = strtok.nextToken().trim(); 108 if (token.length() > 0) 109 { 110 try 111 { 112 Statement s = connection.createStatement(); 113 int num = s.executeUpdate(token); 114 s.close(); 115 } 116 catch (SQLException ex) 117 { 118 return "SQL error: " + ex.getMessage(); 119 } 120 } 121 } 122 123 return null; 124 } 125 126 } 127 | Popular Tags |