继上一个问题之后,我发现代码中的罪魁祸首是这个对象
我在 Java 中的 Connection 类中有一个名为“conn”的对象。
在我的旧代码中,没有突出显示对象名称。正如你在下面看到的。
但是,在我在 Eclipse 上的较新版本的代码中,由于该对象始终为空,因此该对象一直被突出显示。
我的DatabaseLogic类
package org.ari;
//class dependencies
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DatabaseLogic
{
private static Connection conn;
public static String openDatabase() throws IOException, SQLException,
NamingException
{
// context class gives naming standards of the surrounding environment
// of the servlet i.e. the web server ,
// allowing the servlet to interface with the web servers resources
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:comp/env");
// servlet looks up for a connection pool called "jdbc/POOL"
DataSource ds = (DataSource) envContext.lookup("jdbc/POOL");
// connection is then made/requests to connection pool
String result = ds.toString();
try
{
conn = ds.getConnection();
}
catch (SQLException e)
{
System.out.println( e.toString());
}
return result;
}
public static void closeDatabase()
{
try
{
conn.close();
}
catch (SQLException e)
{
}
}
// queryId is the parameter to be used for querying for relevant records
// - possibly change name to make it less specific e.g. recordid
public static String getData(String queryId, int requestNumber)
throws SQLException
{
String result = "";
if (queryId != null)
{
try
{
if (conn == null)
{
//result = "We are in here";
result = openDatabase();
}
// prepare a statement for use in query
Statement stmt = conn.createStatement();
// query parameratised with queryId
String qry = "SELECT RECORD_ID, USER_ID, OPERATION_CD, BUSCOMP_NAME, OPERATION_DT, FIELD_NAME, OLD_VAL, NEW_VAL, AUDIT_LOG, ROW_ID, BC_BASE_TBL FROM S_AUDIT_ITEM WHERE RECORD_ID='"
+ queryId + "'";
ResultSet results = stmt.executeQuery(qry);
result = XMLBuilder.xmlBuilder(results, queryId,
requestNumber);
// close the connection
stmt.close();
results.close();
}
catch (Exception e)
{
// log.error("Cannot connect to database :" + e);
}
}
else
{
// not sure if ever reached
result = "The query parameter is a null value";
}
return result;
}
}
这个突出显示是什么意思,我猜 Eclipse 正在以不同的方式处理这个对象,这就是为什么这个对象不再正常运行的原因。
我该如何解决这个问题,有什么想法吗?我已尝试恢复旧版本的代码,但此突出显示仍然相同。
我猜这是IDE中某处的一些设置。
谢谢