DBUtil
2015-06-20 15:16
447 查看
/*需要先加包:mysql-connector-java-5.1.7-bin.jar*/
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/gale";
private static String user = "root";
private static String password = "root";
/**
* 连接数据库
* @return Connection
* @throws Exception
*/
public static Connection getConnection() throws Exception{
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw e;
}
}
public static void closeConnection(Connection conn){
try {
if(conn==null){
return;
}
if(conn.isClosed()){
return;
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/gale";
private static String user = "root";
private static String password = "root";
/**
* 连接数据库
* @return Connection
* @throws Exception
*/
public static Connection getConnection() throws Exception{
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw e;
}
}
public static void closeConnection(Connection conn){
try {
if(conn==null){
return;
}
if(conn.isClosed()){
return;
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- MySQL中的integer 数据类型
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android之获取手机上的图片和视频缩略图thumbnails
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- PropertyChangeListener简单理解