解决方案 »

  1.   

    抽象出一个DAO层,操作完数据库立马释放
      

  2.   

    养成良好习惯最重要,就你这个写法也是不规范的。因为你的获取连接并没有放在try中,应该将conn申明放在try之前,并将获取连接的代码放在try中,这样才是规范的写法。
    你的这种写法,如果在第一行获取连接之后直到try之前抛出了异常的话,你的连接将无法关闭。
    如果你基础以及很好了,可以采用一些现成框架,对这块封装的比较方便一些,不过良好习惯才是王道,因为不仅仅数据库连接有这种问题,程序中的很多资源的使用都有这一样的问题。
      

  3.   

    养成良好习惯最重要,就你这个写法也是不规范的。因为你的获取连接并没有放在try中,应该将conn申明放在try之前,并将获取连接的代码放在try中,这样才是规范的写法。
    你的这种写法,如果在第一行获取连接之后直到try之前抛出了异常的话,你的连接将无法关闭。
    如果你基础以及很好了,可以采用一些现成框架,对这块封装的比较方便一些,不过良好习惯才是王道,因为不仅仅数据库连接有这种问题,程序中的很多资源的使用都有这一样的问题。谢谢指正
      

  4.   

    编程习惯问题,根据业务要求来确认是否释放关闭资源。程序员应该能够敏锐的能够观察到这些。写C和C++的人申请了内存,也要根据业务要求来时方内存,不要会内存泄露。Java也是不管用不用框架来实现,这个基本的技能、基础、习惯是必须具备的。
      

  5.   

    public interface Connection
    extends Wrapper, AutoCloseable
    try(Connection connnection = ...){
        ... // do sth
    }catch(SQLException xe){
        ... // process xe
    }
      

  6.   

    从java7开始,可以使用资源自动关闭了,只要实现AutoCloseable接口,就可以使用try(){}catch(){}结构,close方法会被自动调用的。
      

  7.   

    从java7开始,可以使用资源自动关闭了,只要实现AutoCloseable接口,就可以使用try(){}catch(){}结构,close方法会被自动调用的。哇,这是个不错的特性。早就该这么搞了!
      

  8.   

    不会写。。从java7开始,可以使用资源自动关闭了,只要实现AutoCloseable接口,就可以使用try(){}catch(){}结构,close方法会被自动调用的。哇,这是个不错的特性。早就该这么搞了!
    自己抽自己爽不爽?5楼告诉你不用框架怎么办,你装死了。
    人家给你个现成的接口,把你高兴的屁颠屁颠的。这种方法和使用框架有P区别。你不是不想用框架,而是连框架都用不明白,总结就是两个字,第一个是懒,第二个是蠢。
      

  9.   


    高兴,是高兴java的进步。我的问题还是没有解决,我问的是怎么封装,新的特性只是把问题屏蔽掉了。
    我是用不明白框架,现在的需求还不值得我用框架。
    我想知道的只是一个封装的办法,却引来你的侮辱。
    你看问题的片面,以及动不动就骂人的习性,真让我看不起!!
      

  10.   

    用模板模式,自己封装下
    /*
     * 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;
    }

    }