1 | package org.greenstone.gsdl3.sql.derby;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.apache.log4j.*;
|
---|
5 | import org.greenstone.gsdl3.sql.*;
|
---|
6 | import java.sql.Connection;
|
---|
7 | import java.sql.DriverManager;
|
---|
8 | import java.sql.SQLException;
|
---|
9 |
|
---|
10 | public class DerbySQLServer implements SQLServer{
|
---|
11 | static final String PROTOCOL = "jdbc:derby:";
|
---|
12 | static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
|
---|
13 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbySQLServer.class.getName());
|
---|
14 |
|
---|
15 | public DerbySQLServer(){
|
---|
16 | try{
|
---|
17 | Class.forName(DRIVER).newInstance();
|
---|
18 | }
|
---|
19 | catch(Exception e){
|
---|
20 | logger.error("Couldn't find derby driver: "+DRIVER,e);
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public Connection connect(String databasePath){
|
---|
25 | try{
|
---|
26 | String protocol_str = PROTOCOL + databasePath;
|
---|
27 | Connection connection = DriverManager.getConnection(protocol_str);
|
---|
28 | return connection;
|
---|
29 | }
|
---|
30 | catch(Exception e){
|
---|
31 | logger.info("Connect to database "+databasePath + " failed!");
|
---|
32 | }
|
---|
33 |
|
---|
34 | return null;
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | public Connection connectAndCreate(String databasePath){
|
---|
39 | try{
|
---|
40 | String protocol_str = PROTOCOL + databasePath + ";create=true";
|
---|
41 | Connection connection = DriverManager.getConnection(protocol_str);
|
---|
42 | return connection;
|
---|
43 | }
|
---|
44 | catch(Exception e){
|
---|
45 | logger.error("Connect to database "+databasePath + " failed!",e);
|
---|
46 |
|
---|
47 | }
|
---|
48 | return null;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public boolean disconnect(String databasePath){
|
---|
52 | try{
|
---|
53 | String protocol_str = PROTOCOL + databasePath + ";shutdown=true";
|
---|
54 | DriverManager.getConnection(protocol_str);
|
---|
55 | }catch (SQLException se){
|
---|
56 | String theError = (se).getSQLState();
|
---|
57 | if (!theError.equals("08006")){
|
---|
58 | logger.error("Database "+databasePath + " couldn't be shut down properly!",se);
|
---|
59 | }
|
---|
60 | else{
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | }
|
---|