package com.daqing.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 此方法是没有new实例的
* @author Administrator
*
*/
public final class A0101JdbcUtils {
private static final String url = "jdbc:mysql://127.0.0.1:3306/jdbc";
private static final String user = "root";
private static final String password = "root";
private A0101JdbcUtils(){
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
public static void free(ResultSet rs, PreparedStatement ps, Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
System.out.println("ResultSet关闭发生异常...");
} finally {
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
System.out.println("PreparedStatement关闭发生异常...");
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("Connection关闭发生异常...");
}
}
}
}
}
}
}
jdbc连接数据库普通写法
最新推荐文章于 2024-04-09 23:17:20 发布