我有一些OAF代码,我正在启动sql连接但是当我试图关闭它时,它会抛出sql异常。这是代码。
try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("handle exception here:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
conn.close(); // throws exception here
}
答案 0 :(得分:0)
尝试使用try和catch子句
附上结束代码 try
{
conn =
(OracleConnection)oadbtransactionimpl.getJdbcConnection();
String queryForEmpty =
"select ... query here";
projectDetailsStatment =
conn.prepareStatement(queryForEmpty);
projectDetailsStatment.setString(1,sprojectid);
ResultSet rs = projectDetailsStatment.executeQuery();
}
catch(SQLException e)
{
String sqlErrMsg = e.getMessage();
throw new OAException((new StringBuilder()).append("Exception in Finding Project Type:").append(sqlErrMsg).toString(), (byte)0);
}
finally
{
try{
conn.close(); // throws exception here
}
catch(SQLException e)
{
//your code here
}
}
答案 1 :(得分:0)
任何JDBC操作的基本方案都非常简单:
执行语句并获取ResultSet。
处理ResultSet。
关闭所有内容:ResultSet, 声明和连接。
BAD SOLUTION
public void handleJDBC() {
try {
DataSource dataSource = getDataSource();
Connection connection = dataSource.getConnection();
operation(connection)
connection.close();
} catch (SQLException e) {
//do something here
}
}
良好做法
finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
if (exception != null) {
exception = new DoubleException(exception, e);
} else {
exception = e;
}
}
}
}
if (exception != null) {
throw exception;
}