source: greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java@ 15081

Last change on this file since 15081 was 15081, checked in by xiao, 16 years ago

add the derby library and also some classes that use the library

File size: 1.7 KB
Line 
1package org.greenstone.gsdl3.sql.derby;
2
3
4import org.apache.log4j.*;
5import org.greenstone.gsdl3.sql.*;
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.SQLException;
9
10public 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}
Note: See TracBrowser for help on using the repository browser.