1.工具类应包含
(1)获取con对象
public static Connection getCon() throws SQLException {
return DriverManager.getConnection(url,name,pass);
}
(2)关闭资源
public static void close(Statement statement,Connection connection){
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection, ResultSet resultSet){
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.使用到的技术
(1)路径查找
(2)资源配置
//创造配置集
Properties properties = new Properties();
//获取配置路径
//获取类加载器
ClassLoader classLoader = JDBCutil.class.getClassLoader();
URL resource = classLoader.getResource("JDBC.properties");
String path = resource.getPath();
System.out.println(path);
//将配置文件加载进结果集使用
properties.load(new FileReader(path));
//开始取配置
url= properties.getProperty("url");
name=properties.getProperty("name");
pass=properties.getProperty("pass");
driver=properties.getProperty("driver");
3.总的代码
public class JDBCutil {
private static String url;
private static String name;
private static String pass;
private static String driver;
static {
try {
//创造配置集
Properties properties = new Properties();
//获取配置路径
//获取类加载器
ClassLoader classLoader = JDBCutil.class.getClassLoader();
URL resource = classLoader.getResource("JDBC.properties");
String path = resource.getPath();
System.out.println(path);
//将配置文件加载进结果集使用
properties.load(new FileReader(path));
//开始取配置
url= properties.getProperty("url");
name=properties.getProperty("name");
pass=properties.getProperty("pass");
driver=properties.getProperty("driver");
//开始使用
//先注册一个
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon() throws SQLException {
return DriverManager.getConnection(url,name,pass);
}
public static void close(Statement statement,Connection connection){
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection, ResultSet resultSet){
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
6.实际使用时的替代
(1)
connection= JDBCutil.getCon();
(2)
finally {
JDBCutil.close(statement,connection,resultSet);
}
5.结果
编号: 2 姓名: jun
编号: 3 姓名: feng
编号: 4 姓名: niu
编号: 1 姓名: jiang
编号: 5 姓名: jiang1
编号: 6 姓名: jiang2
编号: 7 姓名: jiang3
编号: 8 姓名: jiang4
编号: 9 姓名: jiang5
编号: 10 姓名: jiang5
Process finished with exit code 0