package com.tay.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DaoFactory {
// 建立Connection对象,连接数据库
public static Connection getConnection() {
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://bvoioxwtkxxf.rds.sae.sina.com.cn:10642/test";
String user = "root";
String password = "123";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("数据库连接失败");
}
return conn;
}
// 关闭相关对象,断开数据库连接
public static void closeAll(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}