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

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

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目录下

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708629.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存