zoukankan      html  css  js  c++  java
  • MySQL建立一个连接工具类

    public class DBUtil {
    public static Connection getConn() {
    Connection conn = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root", "root");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    }

    public static PreparedStatement prepareStmt(Connection conn, String sql) {
    PreparedStatement pstmt = null;
    try {
    pstmt = conn.prepareStatement(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return pstmt;
    }

    public static ResultSet executeQuery(Statement stmt, String sql) {
    ResultSet rs = null;
    try {
    rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return rs;
    }


    public static void close(Connection conn, Statement stmt,
    PreparedStatement preStatement, ResultSet rs) {
    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    conn = null;
    }
    if (stmt != null) {
    try {
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    stmt = null;
    }
    if (preStatement != null) {
    try {
    preStatement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    preStatement = null;
    }

    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    rs = null;
    }
    }
    }

  • 相关阅读:
    Pthon3各平台的安装
    scrapy爬虫 简单入门
    自动定时打卡7.13
    centos7+python3+selenium+chrome
    在Ubuntu安装kubernetes
    在Ubuntu下安装Jenkins
    在Ubuntu安装Docker
    猫眼100 爬虫
    python 招聘数据分析
    mysql8.0.19忘记密码
  • 原文地址:https://www.cnblogs.com/lxmyhappy/p/6908147.html
Copyright © 2011-2022 走看看