JDBC模板
一、准备工作
1.1、创建Maven工程
工程ID
Maven工作目录
保存路径
进入工程,选择Enable Auto-Import
,允许自动导包
1.2、修改pom文件
1.修改properties里的编译版本,此处用的是jdk1.8,所以改为1.8
1.3、修改Project Structure
Language level改成版本8
1.4、修改Settings
编译版本改成8
1.5、资源文件夹
1.创建文件夹
src->main下创建resources文件夹
2.设置为资源文件夹
右击resources文件夹,Mark Directory as->Resources Root
二、Java连接mysql
2.1、添加依赖
在<dependencies></dependencies>中添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
pom.xml文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.template.jdbc</groupId>
<artifactId>java2mhh</artifactId>
<version>1.0-SNAPSHOT</version>
<name>java2mhh</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.2、JDBC配置文件
resources文件夹下新建文件datasource.properties
文件内容如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.247.130:3306/mysqltest?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=javakb10
2.3、代码编写
(1)BaseConfig类,初始化连接
package cn.template.jdbc;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BaseConfig {
//定义内部类Config,使用其对象作为BaseConfig的成员变量,加载驱动等信息
private class Config {
String driver;
String url;
String username;
String password;
}
private Config config;
//正则匹配,检查url格式是否有效
private boolean valid(String url) {
Pattern p = Pattern.compile("jdbc:\\w+://((\\d{1,3}\\.){3}\\d{1,3}|\\w+):\\d{1,5}/\\w+");
Matcher m = p.matcher(url);
return m.matches();
}
//用代码块调用init方法,加载数据库配置信息并加载驱动driver
{
//静态代码块 异常无法抛出
try {
init();
//驱动装载
Class.forName(config.driver);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
//init方法将配置信息加载到BaseConfig的成员变量config上
private void init() throws Exception {
//用线程读取读取资源文件datasource.properties的路径
String path = Thread.currentThread().getContextClassLoader()
.getResource("datasource.properties").getPath();
//使用Properties包装类解析properties文件
Properties pro = new Properties();
pro.load(new FileReader(path));
String url = pro.getProperty