source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnectionFactory.java@ 10294

Last change on this file since 10294 was 10294, checked in by kjdon, 19 years ago

made the connection a bit more dynamic - gets server and port and password info from GlobalProperties

  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1package org.greenstone.gsdl3.gs3build.database;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6import java.io.InputStream;
7import java.util.Properties;
8import org.greenstone.gsdl3.util.GlobalProperties;
9
10public class GS3SQLConnectionFactory
11{
12
13 public static Connection getConnection(String database)
14 {
15 try {
16 String server = GlobalProperties.getProperty("mysql.server", "localhost");
17 String port = GlobalProperties.getProperty("mysql.tcp.port", "3306");
18 String password = GlobalProperties.getProperty("mysql.gsdl3admin.password");
19
20 Class.forName("com.mysql.jdbc.Driver").newInstance();
21 String connection_address = "jdbc:mysql://"+server+":"+port+"/"+database+"?user=gsdl3admin";
22 if (password != null) {
23 connection_address += "&password="+password;
24 }
25 Connection connection = DriverManager.getConnection(connection_address);
26 return connection;
27 }
28 catch (SQLException sqlEx) {
29 System.out.println(sqlEx.toString());
30 }
31 catch (Exception ex) {
32 System.out.println(ex.toString());
33 }
34 return null;
35 }
36
37 public static SQLConnection getSQLConnection(String database)
38 {
39 Connection c = getConnection(database);
40 if (c== null) {
41 return null;
42 }
43 return new SQLConnection(c, database);
44 }
45
46
47 public static GS3SQLConnection getGS3SQLConnection(String database)
48 {
49 Connection c = getConnection(database);
50 if (c== null) {
51 return null;
52 }
53 return new GS3SQLConnection(c, database);
54 }
55
56 /* returns a Connection to the server, */
57 public static Connection getConnection()
58 {
59 return getConnection("test");
60 }
61
62 /* returns a Connection to the server, */
63 public static SQLConnection getSQLConnection()
64 {
65 return getSQLConnection("test");
66 }
67
68 public static GS3SQLConnection getGS3SQLConnection()
69 {
70 return getGS3SQLConnection("test");
71 }
72
73 public static void main(String args[])
74 {
75 GS3SQLConnection connection = getGS3SQLConnection();
76 connection.initCollection("maya");
77 }
78}
Note: See TracBrowser for help on using the repository browser.