1 | package org.greenstone.gsdl3.gs3build.database;
|
---|
2 |
|
---|
3 | import java.sql.Connection;
|
---|
4 | import java.sql.DriverManager;
|
---|
5 | import java.sql.SQLException;
|
---|
6 | import java.sql.Statement;
|
---|
7 | import java.sql.ResultSet;
|
---|
8 |
|
---|
9 | public class SQLConnection
|
---|
10 | {
|
---|
11 | protected Connection connection;
|
---|
12 | protected String database;
|
---|
13 |
|
---|
14 | public SQLConnection(java.sql.Connection connection, String database)
|
---|
15 | {
|
---|
16 | this.connection = connection;
|
---|
17 | this.database = database;
|
---|
18 | }
|
---|
19 |
|
---|
20 | protected void finalize() {
|
---|
21 | if (this.connection!=null) {
|
---|
22 | try {
|
---|
23 | this.connection.close();
|
---|
24 | } catch (Exception e) {}
|
---|
25 | }
|
---|
26 | this.connection = null;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void close() {
|
---|
30 | finalize();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public Statement createStatement() throws SQLException
|
---|
34 | {
|
---|
35 | return this.connection.createStatement();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public boolean connectToDatabase(String database) {
|
---|
39 | if (this.connection != null) {
|
---|
40 | try {
|
---|
41 | this.connection.close();
|
---|
42 | } catch (Exception e) {}
|
---|
43 | }
|
---|
44 | this.connection = GS3SQLConnectionFactory.getConnection(database);
|
---|
45 | if (this.connection == null) {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | this.database = database;
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public boolean dropDatabase(String database) {
|
---|
53 | try {
|
---|
54 | Statement statement = this.connection.createStatement();
|
---|
55 | statement.execute("DROP DATABASE "+database+";");
|
---|
56 | statement.close();
|
---|
57 | }
|
---|
58 | catch (SQLException ex){
|
---|
59 | System.err.println(ex);
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | return true;
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | public boolean createDatabase(String database) {
|
---|
67 | try {
|
---|
68 | String command = "CREATE DATABASE " + database;
|
---|
69 | Statement statement = this.connection.createStatement();
|
---|
70 | statement.execute(command);
|
---|
71 | statement.close();
|
---|
72 | } catch (Exception e) {
|
---|
73 | System.err.println(e);
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|