[Java] 类 →→→→→进入此内容的聊天室

来自 , 2020-02-24, 写在 Java, 查看 103 次.
URL http://www.code666.cn/view/6ba3af5d
  1. package com.student.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class DbUtil {
  8.         private String dbUrl="jdbc:mysql://localhost:3306_student";
  9.         private String user="root";
  10.         private String password="123456";
  11.         private String jdbcName="com.mysql.jdbc.Driver";
  12.         public Connection getCon() throws Exception{
  13.                 Class.forName(jdbcName);
  14.                 Connection con=DriverManager.getConnection(dbUrl, user, password);
  15.                 return con;
  16.         }
  17.         public void close(Connection con)throws Exception{
  18.                 if(con!=null){
  19.                         con.close();
  20.                 }
  21.         }
  22.         public static void main(String[] args){
  23.                 Connection con=null;
  24.                 try {
  25.                         con = new DbUtil().getCon();
  26.                         System.out.println(con);
  27.                 } catch (Exception e) {
  28.                         e.printStackTrace();
  29.                 }finally{
  30.                         if(con!=null){
  31.                                 try {
  32.                                         con.close();
  33.                                 } catch (SQLException e) {
  34.                                         e.printStackTrace();
  35.                                 }
  36.                         }
  37.                 }
  38.         }
  39. }
  40.  
  41.  

回复 "类"

这儿你可以回复上面这条便签

captcha