IDEA学习记录20--数据库工具类自定义DBUtils封装

1、CustomDBUtil

package net.xdclass.web.util;

import java.sql.*;
import java.util.Properties;

public class CustomDBUtil {
    private static String url;
    private static String username;
    private static String password;private static String driver;

    static {//静态代码块作初始化信息
        try {//可能抛出异常,加个try
            Properties properties = new Properties();//读取配置文件
            properties.load(CustomDBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
//加载JDBC驱动程序
            Class.forName(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
//获取连接
    public static Connection getConnection() throws Exception{
        Connection connection =
                DriverManager.getConnection(url,username,password);
        return connection;
    }
//关闭资源
    public static void close(ResultSet resultSet, PreparedStatement ps,
                             Connection connection){
        try{
            if(resultSet!=null){
                resultSet.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(connection!=null){
                connection.close();
            }
        }catch (SQLException e){
            throw new RuntimeException();
        }
    }
}

2、db.properties

配置文件要放在src目录下
在这里插入图片描述

username = root
password = 123456
url = jdbc:mysql://127.0.0.1:3306/xd_class?userUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
driver = com.mysql.cj.jdbc.Driver

3、TestJDBCServlet

package net.xdclass.web.controller;

import net.xdclass.web.util.CustomDBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("/jdbc")
public class TestJDBCServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);

        try{
            Connection connection = CustomDBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement("select * from user where id=?");
            ps.setInt(1,id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()){
                System.out.println("用户名称 name=" +resultSet.getString("username")+
                        "   联系方式 wechat="+resultSet.getString("wechat"));
            }
            CustomDBUtil.close(resultSet,ps,connection);
        }catch(Exception e){
            e.printStackTrace();
        }
    }


}

4、注意

1、在tomcat中的lib文件夹中加入数据库连接用的connector驱动包(jar包)
2、配置文件要放在src目录下
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值