MySQL连接

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>

在这里插入图片描述

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望北i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值