package com.java1234.util; |
import java.sql.Connection; |
import java.sql.DriverManager; |
public class DbUtil { |
private String dbUrl= "jdbc:mysql://localhost:3306/db_studentInfo?characterEncoding=utf8" ; |
private String dbUserName= "root" ; |
private String dbPassword= "root" ; |
private String jdbcName= "com.mysql.jdbc.Driver" ; |
|
/** |
* 获取数据库连接 |
* @return |
* @throws Exception |
*/ |
public Connection getCon() throws Exception{ |
Class.forName(jdbcName); |
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword); |
return con; |
} |
|
/** |
* 关闭数据库连接 |
* @param con |
* @throws Exception |
*/ |
public void closeCon(Connection con) throws Exception{ |
if (con!= null ){ |
con.close(); |
} |
} |
|
public static void main(String[] args) { |
DbUtil dbUtil= new DbUtil(); |
try { |
dbUtil.getCon(); |
System.out.println( "数据库连接成功" ); |
} catch (Exception e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
} |
初级程序员
by: 云代码会员 发表于:2019-07-15 14:26:52 顶(0) | 踩(0) 回复
好
回复评论