用模板模式,自己封装下 /* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.my.behaviourtype.template.jdbctemplate;import java.sql.Connection; import java.sql.SQLException;public interface ConnectionCallback<T> { T doInConnection(Connection conn) throws SQLException;}package com.my.behaviourtype.template.jdbctemplate;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;public class MyJdbcTemplate { private String className; private String url; private String user; private String password;
public Connection getConnection() { return getConnection(className, url, user, password); }
你的这种写法,如果在第一行获取连接之后直到try之前抛出了异常的话,你的连接将无法关闭。
如果你基础以及很好了,可以采用一些现成框架,对这块封装的比较方便一些,不过良好习惯才是王道,因为不仅仅数据库连接有这种问题,程序中的很多资源的使用都有这一样的问题。
你的这种写法,如果在第一行获取连接之后直到try之前抛出了异常的话,你的连接将无法关闭。
如果你基础以及很好了,可以采用一些现成框架,对这块封装的比较方便一些,不过良好习惯才是王道,因为不仅仅数据库连接有这种问题,程序中的很多资源的使用都有这一样的问题。谢谢指正
extends Wrapper, AutoCloseable
try(Connection connnection = ...){
... // do sth
}catch(SQLException xe){
... // process xe
}
自己抽自己爽不爽?5楼告诉你不用框架怎么办,你装死了。
人家给你个现成的接口,把你高兴的屁颠屁颠的。这种方法和使用框架有P区别。你不是不想用框架,而是连框架都用不明白,总结就是两个字,第一个是懒,第二个是蠢。
高兴,是高兴java的进步。我的问题还是没有解决,我问的是怎么封装,新的特性只是把问题屏蔽掉了。
我是用不明白框架,现在的需求还不值得我用框架。
我想知道的只是一个封装的办法,却引来你的侮辱。
你看问题的片面,以及动不动就骂人的习性,真让我看不起!!
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.my.behaviourtype.template.jdbctemplate;import java.sql.Connection;
import java.sql.SQLException;public interface ConnectionCallback<T> { T doInConnection(Connection conn) throws SQLException;}package com.my.behaviourtype.template.jdbctemplate;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;public class MyJdbcTemplate {
private String className;
private String url;
private String user;
private String password;
public Connection getConnection() {
return getConnection(className, url, user, password);
}
public Connection getConnection(String className, String url, String user, String password) {
try {
Class.forName(className);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public <T> T execute(ConnectionCallback<T> action) throws SQLException {
Connection conn = getConnection();
try {
return action.doInConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// egnore
e.printStackTrace();
}
conn = null;
}
}
} /**
* @return the className
*/
public String getClassName() {
return className;
} /**
* @param className the className to set
*/
public void setClassName(String className) {
this.className = className;
} /**
* @return the url
*/
public String getUrl() {
return url;
} /**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
} /**
* @return the user
*/
public String getUser() {
return user;
} /**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
} /**
* @return the password
*/
public String getPassword() {
return password;
} /**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}