码迷,mamicode.com
首页 > 数据库 > 详细

java-JDBC.mysql 工具类 读取本地文件配置

时间:2017-12-14 14:52:33      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:exce   dstat   res   getc   catch   imp   失败   util   post   

引用 mysql-connector-jav 的jar 

配置文件为  database.propertties

 

 1 package JDBCDemo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.sql.DriverManager;
 8 import java.sql.PreparedStatement;
 9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.InputMismatchException;
12 import java.util.Properties;
13 
14 import com.mysql.jdbc.Connection;
15 import com.mysql.jdbc.Statement;
16 
17 public class JDBCUtils {
18     private void JDBCUtils() {};
19     private  static String sqlurl = ""; 
20     private  static String sqluser = "";
21     private  static String sqlpwd = "";
22     private  static String classforName = "";
23     private static  Connection con = null;
24     
25     static {
26         try {
27             initConfig();
28             Class.forName(classforName);
29             con = (Connection) DriverManager.getConnection(sqlurl, sqluser, sqlpwd);
30         } catch (ClassNotFoundException e) {
31             System.out.println("数据驱动失败");
32             e.printStackTrace();
33         } catch (SQLException e) {
34             
35             System.out.println("数据连接失败");
36             e.printStackTrace();
37         } 
38     }
39     
40     private static void initConfig() {
41         InputStream inf = JDBCUtils.class.getClassLoader().getResourceAsStream("database.propertties");
42         Properties properties = new Properties();
43         try {
44             properties.load(inf);
45             classforName = properties.getProperty("driverClass");
46             sqlurl = properties.getProperty("url");
47             sqluser = properties.getProperty("username");
48             sqlpwd = properties.getProperty("password");
49         } catch (IOException e) {
50             System.out.println("读取配置文件失败");
51         }
52     }
53     
54     public static Connection getConnection() {
55         return con;
56     }
57     public static void close(Connection con,Statement preparedStatement,ResultSet rs)   {
58         if(con !=null) {
59             try {
60                 con.close();
61             } catch (SQLException e) {
62                 //e.printStackTrace();
63             }
64             
65         }
66         if (preparedStatement!=null) {
67             try {
68                 preparedStatement.close();
69             } catch (Exception e) {
70                 //e.printStackTrace();
71             }
72         }
73         if (rs!=null) {
74             try {
75                 rs.close();
76             } catch (Exception e) {
77                 //e.printStackTrace();
78             }
79         }
80     }
81 }

 

java-JDBC.mysql 工具类 读取本地文件配置

标签:exce   dstat   res   getc   catch   imp   失败   util   post   

原文地址:http://www.cnblogs.com/wwcherish/p/8037395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!