一、方法一: java原生jdbc连接数据库
类中封装数据库连接方法,业务操作时调用方法连接数据库,再通过结果集和预处理对象进行增删查改操作
/**
* 数据库基本连接方式——类中封装数据库连接方法,业务操作时调用方法连接数据库,再通过结果集和预处理对象进行增删查改操作
* @author monna
* @version 03/04/2019
*/
public class DbConnected {
/**
* 需连接的数据库URL,记住数据库乱码的处理方式
*/
private static final String URL = "jdbc:mysql://localhost:3306/medical?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
private static final String USER = "root";
private static final String PASSWORD = "root";
/**
* 数据库连接
* @return 数据库连接
*/
public Connection conn(){
Connection conn = null;
try {
//加载驱动程序,需要数据库驱动jar包
Class.forName("com.mysql.cj.jdbc.Driver");
//获取数据库链接
conn= DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 数据库的增删查改操作
*/
public void crudDb(){
Connection con = conn();
//通过数据库的连接操作数据库,实现增删改查(使用Statement类)
Statement st= null;
try {
st = con.createStatement();
ResultSet rs=st.executeQuery("select * from user_info");
/**
* 4.处理数据库的返回结果(使用ResultSet类)
* 了解rs.next(),rs.next()返回的是true 和false 如果集合中有数据返回的就是true,否,则false
* 先判断下后遍历,会导致查询结果中的数据变少,且从第二条数据开始遍历。
* 如果当前查询的只是一条数据,那遍历的结果始终都是null。
* while(rs.next()) //就是 将rs全部进行读取
* if(rs.next()) //rs进行读取一次 判断是否有数据
*/
while(rs.next()){
}
//关闭资源
rs.close();
st.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
/**
* 测试数据库连接是否成功,成功则输出字符串,失败则报错
*/
System.out.println(new DbConnected().conn());
}
}
二、方法二:利用properties.properties配置文件
jdbc.properties
#驱动名称
jdbcDriver=com.mysql.cj.jdbc.Driver
#连接URL
jdbcUrl=jdbc:mysql://localhost:3306/medical?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
#数据库账户名
userName=root
#数据库密码
password=root
DbConnect.java
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.medical.util.ParsingProperties;
/**
* 连接池接口实现类
* @author monna
* @version 02/28/2019
*/
public class DbConnect{
/**
* 驱动名称
*/
private String jdbcDriver;
/**
* 连接地址
*/
private String jdbcUrl;
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String password;
/**
* 构造函数,执行初始化方法
*/
public DbConnect() {
init();
}
/**
* 该方法用于解析wechat.properties文件
* @return
*/
public Properties readProperties() {
//实例化properties类
Properties prop = new Properties();
/**
*使用类的反射来加载配置文件,并写入配置文件正确得到路径
*/
try {
prop.load(ParsingProperties.class.getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
/**
* 初始化连接池
*/
private void init(){
Properties pro = readProperties();
jdbcDriver = pro.getProperty("jdbcDriver");
jdbcUrl = pro.getProperty("jdbcUrl");
userName = pro.getProperty("userName");
password = pro.getProperty("password");
try {
Driver driver = (Driver)Class.forName(jdbcDriver).newInstance();
//将driver注册
DriverManager.registerDriver(driver);
} catch (Exception e) {
e.printStackTrace();
}
createConnection();
}
/**
* 创建连接
* @param count
*/
public Connection createConnection() {
Connection connection=null;
try {
//创建连接
connection = DriverManager.getConnection(jdbcUrl, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 获取连接
* @return
*/
public Connection getConnection() {
Connection connection = createConnection();
//判断是否为空
while(connection == null){
//创建connection,步进数
createConnection();
/**
* 防止其他线程过来拿连接
*/
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return connection;
}
/**
* 关闭数据库连接
* @param resultSet 结果集
* @param preparedStatement 预处理指令
* @param connection 数据库连接
*/
public void releaseDB(ResultSet resultSet, PreparedStatement preparedStatement,
Connection connection) {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public String getJdbcDriver() {
return jdbcDriver;
}
public void setJdbcDriver(String jdbcDriver) {
this.jdbcDriver = jdbcDriver;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 测试数据库是否连接成功
* @param args
*/
public static void main(String [] args) {
DbConnect dbConnect = new DbConnect();
System.out.println(dbConnect.getConnection());
}
}