数据库读取工具类
1 package com.db;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import oracle.jdbc.driver.OracleDriver;
7
8 import javax.naming.Context;
9 import javax.naming.InitialContext;
10 import javax.naming.NamingException;
11 import javax.sql.DataSource;
12
13 /**
14 * 数据库连接工具类
15 * @version 1.0
16 */
17 public class JdbcConnection {
18
19 // 定义线程本地变量,每个线程访问它都会获得不同的对象
20 // 使用ThreadLocal使一个连接绑定到一个线程上
21 private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
22 private static String username=null; //用户名
23 private static String password=null; //密码
24 private static String dbName=null; //数据库名称
25 private static String ip=null; //数据库服务器IP地址
26 private static String resourceName=null; //为null时不使用连接池, jdbc/mysql或jdbc/oracle或jdbc/derby
27 private static String databaseType = "oracle";
28
29 private static void initParams(){
30 username=DbConfig.getInstance().getDb_username();
31 password=DbConfig.getInstance().getDb_password();
32 dbName=DbConfig.getInstance().getDb_name();
33 ip=DbConfig.getInstance().getIp();
34 }
35
36 /**
37 *
38 * @return 得到一个数据库连接
39 * @throws SQLException
40 */
41 public static Connection getConnection() throws SQLException {
42 Connection conn = currentConnection.get();
43 if (conn == null) {
44 if(null==resourceName){
45 if("mysql".equals(databaseType.toLowerCase())){
46 conn = getMySqlConnection();
47 }else if("oracle".equals(databaseType.toLowerCase())){
48 conn = getOracleConnection();
49 }else if("derby".equals(databaseType.toLowerCase())){
50 conn = getDerbyConnection();
51 }else{
52 System.out.println("在 JdbcConnection.java 中数据库类型没有设置");
53 throw new SQLException("数据库类型未设置");
54 }
55 }else{
56 conn = getConnectionByPool();
57 }
58 currentConnection.set(conn);
59 }
60 return conn;
61 }
62 /**
63 * 关闭Oracle数据库连接
64 * @throws SQLException
65 */
66 public static void closeConnection() throws SQLException{
67 Connection conn = currentConnection.get();
68 conn.close();
69 currentConnection.set(null);
70 }
71 //获得Oracle数据库连接
72 private static Connection getOracleConnection(){
73 initParams();
74 Connection conn = null;
75 try {
76 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); //加载驱动
77 conn= DriverManager.getConnection("jdbc:oracle:thin:@"+ip+":1521:"+dbName,username,password);
78 } catch (ClassNotFoundException e) {
79 e.printStackTrace();
80 System.out.println("Oracle驱动没找到");
81 } catch (InstantiationException e) {
82 e.printStackTrace();
83 } catch (IllegalAccessException e) {
84 e.printStackTrace();
85 } catch (SQLException e) {
86 e.printStackTrace();
87 }
88 return conn;
89 }
90 //获得MySql数据库连接
91 private static Connection getMySqlConnection(){
92 initParams();
93 Connection conn = null;
94 try {
95 Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动
96 String url = "jdbc:mysql://"+ip+":3306/"+dbName+"?useUnicode=true&characterEncoding=utf8";
97 conn = DriverManager.getConnection(url, username, password);
98 } catch (ClassNotFoundException e) {
99 e.printStackTrace();
100 System.out.println("MySql驱动没找到");
101 } catch (SQLException e) {
102 e.printStackTrace();
103 } catch (InstantiationException e) {
104 e.printStackTrace();
105 } catch (IllegalAccessException e) {
106 e.printStackTrace();
107 }
108 return conn;
109 }
110 //获取Derby数据库连接
111 private static Connection getDerbyConnection(){
112 initParams();
113 Connection conn = null;
114 try {
115 Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); //加载驱动
116 String url = "jdbc:derby://"+ip+":1527/"+dbName+";create=true";
117 conn = DriverManager.getConnection(url, username, password);
118 } catch (ClassNotFoundException e) {
119 e.printStackTrace();
120 System.out.println("Derby驱动没找到");
121 } catch (SQLException e) {
122 e.printStackTrace();
123 } catch (InstantiationException e) {
124 e.printStackTrace();
125 } catch (IllegalAccessException e) {
126 e.printStackTrace();
127 }
128 return conn;
129 }
130 //获取连接池连接
131 private static Connection getConnectionByPool(){
132 try {
133 Context ctx = new InitialContext();
134 Context subContext = (Context)ctx.lookup("java:comp/env");
135 String dsName="";
136 dsName = resourceName;
137
138 DataSource dataSource = (DataSource)subContext.lookup(dsName);
139 //上面两句可以合写成下边这句
140 //ctx.lookup("java:comp/env/jdbc/oracle");// java:comp/env/ 规定:加前缀指定资源
141 return dataSource.getConnection();
142 }
143 catch (NamingException e) {e.printStackTrace();}
144 catch (SQLException e) {e.printStackTrace();}
145 return null;
146 }
147 }
读取.properties文件
1 package com.db;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.net.URISyntaxException;
8 import java.util.Properties;
9
10 /**
11 * 数据库访问配置文件各参数的获取
12 * @author lzx
13 *
14 */
15 public class DbConfig {
16 //数据库及server配置文件路径
17 private static final String ACTIONPATH = "config.properties";
18 private static DbConfig instance=null;
19
20 private String db_username=null;
21 private String db_password=null;
22 private String db_name=null;
23 private String ip=null;
24 private String ags_user=null;
25 private String ags_password=null;
26 private String ags_domain=null;
27 private String ags_host=null;
28 private String ags_servicename=null;
29
30 private DbConfig(){}
31
32 public String getDb_username() {
33 return db_username;
34 }
35 public String getDb_password() {
36 return db_password;
37 }
38 public String getDb_name() {
39 return db_name;
40 }
41 public String getIp() {
42 return ip;
43 }
44 public String getAgs_user() {
45 return ags_user;
46 }
47 public String getAgs_password() {
48 return ags_password;
49 }
50 public String getAgs_domain() {
51 return ags_domain;
52 }
53 public String getAgs_host() {
54 return ags_host;
55 }
56 public String getAgs_servicename() {
57 return ags_servicename;
58 }
59
60 public static DbConfig getInstance(){
61 if(instance==null){
62 instance= new DbConfig().getNewDbConfig();
63 }
64 return instance;
65 }
66
67 private DbConfig getNewDbConfig(){
68
69 DbConfig dc=new DbConfig();
70 Properties prop = new Properties();
71 String path=null;
72 FileInputStream fis=null;
73
74 try {
75 path = DbConfig.class.getClassLoader().getResource("").toURI().getPath();
76 fis = new FileInputStream(new File(path + ACTIONPATH));
77 prop.load(fis);
78 dc.db_username=prop.getProperty("db_username");
79 dc.db_password=prop.getProperty("db_password");
80 dc.db_name=prop.getProperty("db_name");
81 dc.ip=prop.getProperty("ip");
82 dc.ags_user=prop.getProperty("ags_user");
83 dc.ags_password=prop.getProperty("ags_password");
84 dc.ags_domain=prop.getProperty("ags_domain");
85 dc.ags_host=prop.getProperty("ags_host");
86 dc.ags_servicename=prop.getProperty("ags_servicename");
87 } catch (URISyntaxException e) {
88 e.printStackTrace();
89 } catch (FileNotFoundException e) {
90 e.printStackTrace();
91 } catch (IOException e) {
92 e.printStackTrace();
93 }
94
95 return dc;
96 }
97 }
对应的配置文件如下:
config.properties
#database username
db_username=sde
#database password
db_password=sde
#database server name
db_name=RASA
#database server ip
ip=localhost
#arcgis server username
ags_user=zj
#arcgis server password
ags_password=0.
#arcgis server user domain
ags_domain=zj
#arcgis server host address
ags_host=localhost
#arcgis server feature service name
ags_servicename=map