KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > postinstall > DBAccess


1 /*
2  * DBAccess.java
3  *
4  * Created on July 4, 2003, 5:36 AM
5  */

6
7 package com.quikj.application.utilities.postinstall;
8
9 import java.sql.*;
10 import java.io.*;
11 import java.util.*;
12
13 /**
14  *
15  * @author amit
16  */

17 public class DBAccess
18 {
19     private Connection connection;
20     
21     /** Creates a new instance of DBAccess */
22     public DBAccess(String JavaDoc driver,
23     String JavaDoc host, String JavaDoc user, String JavaDoc password, String JavaDoc db)
24     throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, SQLException
25     {
26         Class.forName(driver).newInstance();
27         login(host, user, password, db);
28     }
29     
30     public void login(String JavaDoc host,
31     String JavaDoc user,
32     String JavaDoc password,
33     String JavaDoc database) throws SQLException
34     {
35         // Connect to the database
36
String JavaDoc url_str;
37         
38         if (host.equals("localhost") == true)
39         {
40             host = "127.0.0.1";
41         }
42         
43         url_str = new String JavaDoc("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 JavaDoc 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 JavaDoc executeSQLStatements(String JavaDoc path)
72     {
73         File file = new File(path);
74         try
75         {
76             FileReader freader = new FileReader(file);
77             
78             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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             // and execute the commands
93
return processQuery(buffer.toString());
94         }
95         catch (IOException ex1)
96         {
97             return "IO error occured while reading file";
98         }
99     }
100     
101     private String JavaDoc processQuery(String JavaDoc 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 JavaDoc 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