一、插件实现功能情况
本工具类已经实现MySQL数据的连接和数据库的关闭,并通过线程池进行数据限制。
二、文件组织结构
下图为本工具类的文件预览图
三、maven配置文件pom.xml
下面是本工程文件的依赖库文件
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
四、conn.xml
本文件主要存储连接配置的信息
<connection>
<database id="mysql">
<dependencies>
<name>driver</name>
<value>com.mysql.jdbc.Driver</value>
</dependencies>
<dependencies>
<name>url</name>
<value>jdbc:mysql://192.168.130.100:3306/zuoye?useSSL=true&useUnicode=true&characterEncoding=utf-8</value>
</dependencies>
<dependencies>
<name>user</name>
<value>root</value>
</dependencies>
<dependencies>
<name>password</name>
<value>ok</value>
</dependencies>
</database>
</connection>
五、Conn类
本类主要实现单个数据的连接和释放
Map<String,String>m=null;
Connection conn = null;//定义连接
Statement stmt = null;//初始化statement对象
public Connection con(String database_name) throws DocumentException, ClassNotFoundException, SQLException {
ConSourse c=new ConSourse();
Map<String, String> m = c.getConInfo_Xml("mysql");
Class.forName(m.get("driver"));
conn = DriverManager.getConnection(m.get("url"),m.get("user"),m.get("password"));//连接数据库
stmt = conn.createStatement();//创建一个 Statement 对象
return conn;
}
public void close(Connection database_connection) throws SQLException {
if (database_connection!=null)
database_connection.close();
}
六、ConSourse类
本类主要实现数据库的连接数据从conn.xml中读出
//定义XML的读者
static SAXReader reader;
//定义文件路径
static Document document;
//XML根
static Element root;
//连接XML数据库
private static void conXml() throws DocumentException {
//实现读者
reader = new SAXReader();
//打开文件路径
document = reader.read(new File("src/main/java/conf/conn.xml"));
//获取根
root = document.getRootElement();
}
static Map<String, String> getConInfo_Xml(String dataSourceId) throws DocumentException {
//实现连接
conXml();
//获取配置信息的根
Element datasource =(Element) root.selectSingleNode("database[@id='" + dataSourceId + "']");
//用图来存储配置信息
Map<String, String> map = new HashMap<>();
//获得配置信息的列表
List<Element> list = datasource.elements("dependencies");
//遍历获取配置信息
for (Element element : list) {
//获取配置信息名称
String name = element.elementText("name");
//获取配置信息的值
String value = element.elementText("value");
//将配置信息压入图中
map.put(name, value);
}
//返回配置信息的图
return map;
}
七、mysql_conPool
数据库连接池
private static int POOL_SIZE =5;
Connection con=null;
public synchronized Connection coonpool(String database_name) throws InterruptedException {
if(POOL_SIZE<0){
Thread.sleep(1000);
coonpool(database_name);
}
POOL_SIZE--;
Conn c=new Conn();
try {
con= c.con(database_name);
System.out.println("连接成功");
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return con;
}
public synchronized void closepool(Connection con) throws SQLException {
POOL_SIZE++;
Conn c=new Conn();
c.close(con);
System.out.println("数据库已关闭");
}
八、主方法main()
public static void main(String[] args) throws InterruptedException, SQLException {
Connection con=null;
mysql_conPool p=new mysql_conPool();
con=p.coonpool("mysql");
p.closepool(con);
}