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.io.InputStream;
|
---|
7 | import java.util.Properties;
|
---|
8 | import org.greenstone.gsdl3.util.GlobalProperties;
|
---|
9 |
|
---|
10 | public 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 && !password.equals("")) {
|
---|
23 | connection_address += "&password="+password;
|
---|
24 | }
|
---|
25 | Connection connection = DriverManager.getConnection(connection_address);
|
---|
26 | return connection;
|
---|
27 | }
|
---|
28 | catch (Exception ex) {
|
---|
29 | System.out.println("GS3SQLConnectionFactory.getConnection() Error:");
|
---|
30 | System.out.println(ex.getMessage());
|
---|
31 | }
|
---|
32 | return null;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static SQLConnection getSQLConnection(String database)
|
---|
36 | {
|
---|
37 | Connection c = getConnection(database);
|
---|
38 | if (c== null) {
|
---|
39 | return null;
|
---|
40 | }
|
---|
41 | return new SQLConnection(c, database);
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | public static GS3SQLConnection getGS3SQLConnection(String database)
|
---|
46 | {
|
---|
47 | Connection c = getConnection(database);
|
---|
48 | if (c== null) {
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 | return new GS3SQLConnection(c, database);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* returns a Connection to the server, */
|
---|
55 | public static Connection getConnection()
|
---|
56 | {
|
---|
57 | return getConnection("test");
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* returns a Connection to the server, */
|
---|
61 | public static SQLConnection getSQLConnection()
|
---|
62 | {
|
---|
63 | return getSQLConnection("test");
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static GS3SQLConnection getGS3SQLConnection()
|
---|
67 | {
|
---|
68 | return getGS3SQLConnection("test");
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static void main(String args[])
|
---|
72 | {
|
---|
73 | GS3SQLConnection connection = getGS3SQLConnection();
|
---|
74 | connection.initCollection("maya");
|
---|
75 | }
|
---|
76 | }
|
---|