栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号