MySQL工具包
MySQL实现简单链接
一 引入工具包
JBDCUtils,无需更改,直接使用即可。
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtil {
private static String URL;
private static String USERNAME;
private static String PASSWORD;
// 静态代码块,类加载时执行
static {
try {
Properties properties = new Properties();
InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
String DRIVER = properties.getProperty("driver");
URL = properties.getProperty("url");
USERNAME = properties.getProperty("username");
PASSWORD = properties.getProperty("password");
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
static ThreadLocal<Connection> local = new ThreadLocal<>();
public static Connection getConnection() {
try {
//从ThreadLocal中取对象
Connection connection = local.get();
//判断ThreadLocal中是否为空
if (connection == null) {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
local.set(connection);
return connection;
} else
return local.get();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
// 关闭资源
public static void closeAll(Connection conn, Statement stat, ResultSet rs) {
try {
if (conn != null) {
conn.close();
local.remove();
}
if (stat != null) stat.close();
if (rs != null) rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//开启事务
public static void begin() {
Connection connection;
try {
connection = getConnection();
connection.setAutoCommit(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
// 提交事务
public static void commit() {
Connection connection = null;
try {
connection = getConnection();
connection.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
closeAll(connection, null, null);
}
}
// 回滚
public static void rollback() {
Connection connection = null;
try {
connection = getConnection();
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
closeAll(connection, null, null);
}
}
}
二 资源文件
db.properties 后缀名必须以 .properties结尾,上述工具类才能自动扫描到这个文件
#8.0版本数据库
driver = com.mysql.cj.jdbc.Driver #加载驱动 5.6驱动: driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/store #具体的表
username = root #账户
password = root #密码
三 加载jar包
必须放在web->WEB-INF->lib中 否则就会存在加载不成功的问题
放入过后必须右键
测试
测试是否连接成功
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
try{
Connection conn = JDBCUtil.getConnection();Connection connection = JDBCUtil.getConnection();
if(conn!=null){
out.println("mysql数据库连接成功!!!");
}else{
out.println("数据库连接失败!!!");
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
%>
</body>
</html>