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(); } } } }
  • 相关阅读:
    2021-5-14 日报博客
    2021-5-13 日报博客
    2021-5-11 日报博客
    2021-5-10 日报博客
    2021-5-8 周报博客
    团队介绍——北部大队
    周总结4
    梦断代码阅读笔记02
    周总结3
    周总结2
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12403667.html
Copyright © 2011-2022 走看看