sqlCon.close ();//sqlCon是数据库连接,关闭了就行了,不过他会抛出异常,你可以定义一个函数
//***************************************************
//名称:closeDb
//功能:关闭数据库
//输入:
//输出:
//返回:
//***************************************************
public void closeDb () throws Exception
{
sqlCon.close ();
}或者用try catch块来处理异常
//***************************************************
//名称:closeDb
//功能:关闭数据库
//输入:
//输出:
//返回:
//***************************************************
public void closeDb () throws Exception
{
sqlCon.close ();
}或者用try catch块来处理异常
}
catch(exception e){}
finally{
sqlcon.close();
}
如
try{
//connection 连接代码
}catch(SQLException e){
connection.close;
}
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();
}
}
System.err.println(sqle.getMessage());
//Log
}finally{
if(conn != null) {
try{
conn.close();
}catch(SQLException sqle) { }
conn = null;
}
}
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;
}
} }
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){}
}虽然烦一些但是很稳定