zoukankan      html  css  js  c++  java
  • 数据库的工具类

    package jdbcutils;

    import java.sql.*;

    /*
    * 访问数据库的工具类
    * */
    public class JdbcUtils {
    //1 可以把几个字符串定义成常量
    private static final String USER ="root";
    private static final String PWD="root";
    private static final String URL="jdbc:mysql://localhost:3306/user";
    private static final String DRIVER="com.mysql.jdbc.Driver";
    //注册驱动
    static{
    try {
    Class.forName(DRIVER);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    //得到数据库连接
    public static Connection getConnection() throws SQLException {
    return DriverManager.getConnection(URL,USER,PWD);
    }
    //关闭连接 执行 的打开资源
    public static void close(Connection conn, Statement stmt){
    if(stmt!=null){
    try {
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    //关闭所有的打开资源
    public static void close(Connection conn, Statement stmt, ResultSet rs){
    if(stmt!=null){
    try {
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    }

  • 相关阅读:
    springMVC(注解版笔记)
    关于在Eclipse里面启动了服务,但是localhost:8080无法访问的问题:
    springmvc基础知识
    private
    hashtable,hashMap,vector和ArrayList
    使用IDEA创建java项目(hello word)
    设计模式
    设计模式(总纲)
    spring-boot集成PageHelper和通用Mapper
    spring-boot集成mybatis
  • 原文地址:https://www.cnblogs.com/dongzhenkun/p/10638861.html
Copyright © 2011-2022 走看看