hyy9512

导航

连接MySQL常用工具

database.properties  

如下:url中coursesystem为将要连接的数据库名;username为该数据库设置权限时的用户名;如果设置了密码,再添一项password=你的密码

1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/coursesystem
3 username=root

工具util层,创建properties文件的解析工具ConfigerManager.java

 1 package util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Properties;
 6 /*
 7  * 解析配置文件,大前提是:
 8  * 1、 你要有个配置文件
 9  * 2、你要解析它的工具
10  * 过程:1.创建配置文件的输出流
11  *        2.用工具去解析配置文件输出流
12  *     3.创建利用key值获取value值的方法
13  */
14 public class ConfigerManager {
15 //    创建配置文件解析工具
16     private static Properties param=new Properties();
17     static {
18         String configFile="database.properties";
19         InputStream is = ConfigerManager.class.getClassLoader().getResourceAsStream(configFile);
20             try {
21                 param.load(is);
22             } catch (IOException e) {
23                 e.printStackTrace();
24             }
25     }
26     public static String getValue(String key) {
27         return param.getProperty(key);
28     }
29 }

在dao层创建进行连接数据库以及其他基本操作的BaseDao.java

 1 package dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 import util.ConfigerManager;
10 /*
11  * 普适性的增删改操作,其他的增删改操作需要继承它
12  */
13 public class BaseDao {
14     private static String driver=ConfigerManager.getValue("driver");
15     private String url=ConfigerManager.getValue("url");
16     private String username=ConfigerManager.getValue("username");
17     Connection conn = null;
18     PreparedStatement pds=null;
19     ResultSet rs=null;
20     int result=0;
21     static {
22         try {
23             Class.forName(driver);
24         } catch (ClassNotFoundException e) {
25             e.printStackTrace();
26         }
27     }
28 //    建立连接
29     public Connection getConnection() {
30         try {
31             conn=DriverManager.getConnection(url, username, null);
32             System.out.println("连接已建立!");
33         } catch (SQLException e) {
34             e.printStackTrace();
35         }
36         return conn;
37     }
38 //    断开连接
39     public void closeAll(ResultSet rs,PreparedStatement pds,Connection conn) {
40         if(rs!=null) {
41             try {
42                 rs.close();
43             } catch (SQLException e) {
44                 e.printStackTrace();
45             }
46         }
47         if(pds!=null) {
48             try {
49                 pds.close();
50             } catch (SQLException e) {
51                 e.printStackTrace();
52             }
53         }
54         if(conn!=null) {
55             try {
56                 conn.close();
57             } catch (SQLException e) {
58                 e.printStackTrace();
59             }
60         }
61     }
62 //    增、删、改
63     public int updateData(String sql,Object[] params) {
64         conn = this.getConnection();
65         try {
66             pds=conn.prepareStatement(sql);
67             if(params!=null) {
68                 for(int i=0;i<params.length;i++) {
69                     pds.setObject(i+1, params[i]);    //MySQL语句中下标从1开始
70                 }
71             }
72             result=pds.executeUpdate();
73         } catch (SQLException e) {
74             e.printStackTrace();
75         } finally {
76             this.closeAll(null, pds, conn);
77         }
78         return result;
79     }
80 //
81     public ResultSet queryData(String sql,Object[] params) {
82         conn=this.getConnection();
83         try {
84             pds=conn.prepareStatement(sql);
85             if(params!=null) {
86                 for(int i=0;i<params.length;i++) {
87                     pds.setObject(i+1, params[i]);    //MySQL语句中下标从1开始
88                 }
89             }
90             rs=pds.executeQuery();
91         } catch (SQLException e) {
92             e.printStackTrace();
93         } 
94         return rs;
95     }
96 }

简单的使用Junit对以上代码进行测试

package test;

import org.junit.Test;

import dao.BaseDao;

public class DatabaseTest {
    
        @Test
        public void linkDatabase() {
            BaseDao dao=new BaseDao();
            dao.getConnection();
        }

}

 

posted on 2017-12-17 15:54  hyy9512  阅读(7283)  评论(0编辑  收藏  举报