sqlCon.close ();//sqlCon是数据库连接,关闭了就行了,不过他会抛出异常,你可以定义一个函数
//***************************************************
//名称:closeDb
//功能:关闭数据库
//输入:
//输出:
//返回:
//***************************************************
public void closeDb () throws Exception
{
        sqlCon.close ();
}或者用try catch块来处理异常

解决方案 »

  1.   

    try {---
    }
    catch(exception e){}
    finally{
     sqlcon.close();
    }
      

  2.   

    用try...catch来处理异常,

    try{
      //connection 连接代码
    }catch(SQLException e){
      connection.close;
    }
      

  3.   

    操作完就close,最好在finally中关闭
      

  4.   

    我还是将代码贴出来,大家帮看一下吧,是不是我关闭有的问题.
    import java.util.Vector;
    import java.sql.*;public class OperatorSql implements OperatorInterface
    {
    private Connection connection;
    private PreparedStatement sqlAdd;
    //构造
    public OperatorSql() throws Exception
    {
    connect();
    sqlAdd=connection.prepareStatement("INSERT INTO Operator " +
    "(userName,password,power) VALUES (?,?,?)");
    }
    //连接数据库
    public void connect() throws Exception
    {
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";
    Class.forName(driver);
    connection=DriverManager.getConnection(url);
    connection.setAutoCommit(false);
    }
    //添加
    public boolean add(Operator operator)
    {
    try
    {
    int result;
    sqlAdd.setString(1,operator.getUserName());
    sqlAdd.setString(2,operator.getPassword());
    sqlAdd.setString(3,operator.getPower());
    result=sqlAdd.executeUpdate();
    connection.commit();
    if (result==0)
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    catch(Exception e)
    {
    System.out.println("OperatorSql类中的add方法异常 : " + e);
    return false;
    }
    }
    //关闭
    public void close()
    {
    try
    {
    sqlAdd.close();
    sqlModify.close();
    sqlDelete.close();
    sqlFind.close();
    sqlFindAll.close();
    connection.close();
    }
    catch(Exception e)
    {
    System.out.println("OperatorSql类中的close方法异常 : " + e);
    }
    }
    //结束
    protected void finalize()
    {
    close();
    }
    }
      

  5.   

    try {}catch(SQLException sqle) {
        System.err.println(sqle.getMessage());
        //Log
    }finally{
        if(conn != null) {
            try{
            conn.close();
            }catch(SQLException sqle) {         }
            conn = null;
        }
    }
      

  6.   

    问下楼上的西天,如果已经是null了是不是说明已经被关闭了?我这样试过,在关闭前先判断连接的isClosed方法的状态,为false时我才关闭的,但关闭时就会出现异常,也就是:"java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]非法的事务状态",不知道会不会跟数据库有关系?
      

  7.   

    可以通过判断是否已经是null来判断,比如说:if(con!=null)之类的
      

  8.   

    纠正一点,connection.commit();应该加上connection.rollback();的,这才是一个事务控制,还有你应该把这个commit()和rollback()加上一个try--catch块来包含起来,还在就是在rollback()后,你最好把事务开关打开,即connection.setAutoCommit(true);
      

  9.   

    public void setAutoCommit(boolean autoCommit) throws SQLException  Sets this connection's auto-commit mode to the given state. If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback. By default, new connections are in auto-commit mode. default情况下Commit就是处于Auto状态下,那是不用自已处理rollback的。
    public void connect() throws Exception
    {
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";
    Class.forName(driver);
    connection=DriverManager.getConnection(url);
    //connection.setAutoCommit(false);
    }
    //添加
    public boolean add(Operator operator)
    {
    try
    {
    int result;
    sqlAdd.setString(1,operator.getUserName());
    sqlAdd.setString(2,operator.getPassword());
    sqlAdd.setString(3,operator.getPower());
    result=sqlAdd.execute();

                               if (result==0)
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    catch(Exception e)
    {
    System.out.println("OperatorSql类中的add方法异常 : " + e);
                                if(connection!=null){
                                connection.close();}
    return false;
    }
                      finally{
        if(conn != null) {
            try{
            conn.close();
            }catch(SQLException sqle) {         }
            conn = null;
        }
    } }
      

  10.   

    最好的办法是这个:
    Connection conn=null;
    try
    {
        conn=DriverManager.getConnection();
        conn.setAutoCommit(false);
        处理事物
        conn.commit();
    }
    catch(SQLException exc)
    {
        exc.printStackTrace();
        try
        {
            conn.rollback();
        }
        catch(SQLException exc2)
        {
            exc2.printStackTrace();
        }
    }
    finally
    {
        try
        {
            if(conn!=null) conn.close();
        }
        catch(SQLException exc3){}
    }虽然烦一些但是很稳定
      

  11.   

    感觉JTA用在数据库连接里面时作用不大。
      

  12.   

    为什么用JAVA访问数据库会成这样?用VB多少次怎么都没什么问题.问题原因到底在哪儿?
      

  13.   

    我曾经要连续执行150次数据库操作,数据库也是ACCESS,运行后出现exception:数据库被管理员***锁定.....查了一些资料说是access并发性不好,我是这样解决的,每操作数据库50次后new一个thread,让他sleep(100)。再运行就没有错误了。我的程序中操作数据库的对象始终都是同一个,全部用完后再关闭。另外我没有仔细看你的程序,但我想如果你需要连续使用一个连接,为什么要关闭他呢??为什么不反复使用这个连接直到完成后再关闭他呢?
      

  14.   

    谢谢各位的帮助,使用ACCESS数据库问题还是没有解决,我已经换成SQL Server数据库了,就不存在此问题了,但我还是不明白为什么使用ACCESS数据库会关闭不了数据库,很多朋友是告诉我如何处理异常,而我想解决的是它为什么会出现异常,我希望的是代码正常,不应该出现异常,有对此了解的朋友希望和我联系,[email protected]。今天先把贴结了。