DBUtil工具类

发布于:2023-01-04 ⋅ 阅读:(192) ⋅ 点赞:(0)

package com.mhys.utils;

import java.sql.*;

/**
 * 数据库工具类
 * @author Administrator
 *    4321
 */
public class DBUtil {
    private static final String DriverName="com.mysql.jdbc.Driver";
    private static final String Url="jdbc:mysql://127.0.0.1:3306/book1";
    private static final String User="root";
    private static final String Pwd="123456";
    static{
        try {
            Class.forName(DriverName);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static Connection getConn() throws SQLException{
        return DriverManager.getConnection(Url, User, Pwd);
    }
    public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
 


网站公告

今日签到

点亮在社区的每一天
去签到