import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//URL="jdbc:oracle:thin:@localhost:1521:bookstore"; //Oracle bookstore为数据库名
//URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bookstore"; //SQL Server
private final static String URL="jdbc:mysql://localhost:3306/bookstore"; //MySQL
private final static String USERNAME="root";
private final static String PASSWORD="admin";
//DRIVER="oracle.jdbc.driver.OracleDriver"; //Oracle
//DRiver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //SQL Server
private final static String DRIVER="com.mysql.jdbc.Driver"; //MySQL
public static Connection getConnect(){
Connection conn=null;
try {
//加载数据库驱动类
Class.forName(DRIVER);
//创建数据库连接对象
conn=DriverManager.getConnection(URL, USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("加载数据库驱动类失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("创建数据库连接对象失败");
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,PreparedStatement ps,Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭结果集");
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
System.out.println("关闭预执行语句");
e.printStackTrace();
}
}if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库连接对象");
e.printStackTrace();
}
}
}
}
我是Java新手,希望以在后的学习中请大家赐教,我的QQ:1345383602;MSN:peiqianliu@msn.cn