zoukankan      html  css  js  c++  java
  • 【mysql jdbc DbUtil()】连接、关闭数据库

    package com.java1234.jdbc.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    import com.mysql.jdbc.PreparedStatement;
    
    public class DbUtil {
        
        private static String dbUrl="jdbc:mysql://localhost:3306/db_book";    //"db_book"是数据库名字
        
        private static String dbUserName="root";    //数据库用户名
        
        private static String dbPassword="123456";   //数据库密码
        
        private static String jdbcName="com.mysql.jdbc.Driver";  //驱动
        
    //获取连接
    public Connection getCon()throws Exception{ Class.forName(jdbcName); Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return con; }
    //关闭连接
    public void close(Statement stmt,Connection con)throws Exception{ if(stmt!=null){ stmt.close(); if(con!=null){ con.close(); } } }
    //关闭连接
    public void close(PreparedStatement pstmt,Connection con)throws Exception{ if(pstmt!=null){ pstmt.close(); if(con!=null){ con.close(); } } } }
  • 相关阅读:
    176. Second Highest Salary
    175. Combine Two Tables
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    169. Majority Element
    168. Excel Sheet Column Title
    167. Two Sum II
    160. Intersection of Two Linked Lists
    个人博客记录
    <meta>标签
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12403667.html
Copyright © 2011-2022 走看看