gpt4 book ai didi

javax.sql.datasource getconnection 返回 null

转载 作者:行者123 更新时间:2023-11-29 05:39:28 25 4
gpt4 key购买 nike

有人可以告诉我如何修复下面的代码,这样它就不会抛出错误吗?

下面这行代码给我一个空指针异常:

return dataSource.getConnection();

请注意,dataSource 是在 web.xml 中指定的 javax.sql.DataSource 的一个实例,在被其他代码调用时可以正常工作。

这是 DataAccessObject.java 中出现空指针的实际方法:

protected static Connection getConnection(){  
try {
return dataSource.getConnection(); //
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

这行代码调用了前面的方法:

connection = getConnection();  

它位于名为 CourseSummaryDAO 的类中的以下方法中,如下所示:

public List<CourseSummary> findAll(Long sid) {
LinkedList<CourseSummary> coursesummaries = new LinkedList<CourseSummary>();
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection(); //
String sql = "select * from coursetotals where spid=?";
statement = connection.prepareStatement(sql);
statement.setLong(1, sid);
rs = statement.executeQuery();
//for every row, call read method to extract column
//values and place them in a coursesummary instance
while (rs.next()) {
CourseSummary coursesummary = read("findAll", rs);
coursesummaries.add(coursesummary);
}
return coursesummaries;
}catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
close(rs, statement, connection);
}
}

为了简单地重现这一点,我创建了以下 TestCourseSummaries 类:

public class TestCourseSummaries {
public static void main(String[] args) {
Long id = new Long(1002);
CourseSummaryDAO myCSDAO = new CourseSummaryDAO();
List<CourseSummary> coursesummaries = myCSDAO.findAll(id);
for(int i = 0;i<coursesummaries.size();i++){
System.out.println("type, numunits are: "+coursesummaries.get(i).getCourseType()+","+coursesummaries.get(i).getNumUnits());
}
}
}

编辑:

为了解决 JustDanyul 的问题,我附上了在我的应用程序中调用的代码,以及由调用代码中的两个 DAO 对象扩展的底层 DataAccessObject 代码:

这是我的应用程序中触发错误的代码。看到有两个类,每个类都扩展了 DataAccessObject。也许他们互相冲突,导致第二个没有获得数据库连接?

protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {
String idString = req.getParameter("id");
Long id = new Long(idString);
ThisObj thisobj = new ThisDAO().find(id);
req.setAttribute("thisobj", thisobj);
ThoseObjDAO myThoseDAO = new ThoseObjDAO();
List<ThoseObj> thoseobjects = myThoseObjDAO.findAll(id);
req.setAttribute("thoseobjects", thoseobjects);
jsp.forward(req, resp);

下面是调用代码中由两个 DAO 类扩展的 DataAccessObject 类的代码:

public class DataAccessObject {
private static DataSource dataSource;
private static Object idLock = new Object();

public static void setDataSource(DataSource dataSource) {
DataAccessObject.dataSource = dataSource;
}

protected static Connection getConnection() {
try {return dataSource.getConnection();}
catch (SQLException e) {throw new RuntimeException(e);}
}

protected static void close(Statement statement, Connection connection) {
close(null, statement, connection);
}

protected static void close(ResultSet rs, Statement statement, Connection connection) {
try {
if (rs != null) rs.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {throw new RuntimeException(e);}
}

protected static Long getUniqueId() {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection();
synchronized (idLock) {
statement = connection.prepareStatement("select next_value from sequence");
rs = statement.executeQuery();
rs.first();
long id = rs.getLong(1);
statement.close();
statement = connection.prepareStatement("update sequence set next_value = ?");
statement.setLong(1, id + 1);
statement.executeUpdate();
statement.close();
return new Long(id);
}
}
catch (SQLException e) {throw new RuntimeException(e);}
finally{close(rs, statement, connection);}
}
}

在web.xml中创建数据源,如下:

<resource-ref>
<description>dataSource</description>
<res-ref-name>datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

最佳答案

我怀疑它“运行良好”的代码实际上是在应用程序服务器中运行的代码。您发布的示例仅运行 static void main() 方法,不会获取已在 web.xml 中定义的任何资源。

我猜您使用 JDNI 来设置初始数据源。然后使用类似的东西

@Resource(name="jdbc/mydb")
private DataSource dataSource;

建立你的连接。对吧?

编辑:

在看到您的代码后,您的数据源似乎是全新初始化的。仅仅将一个元素放入您的 web.xml 并不能单独完成。您还需要实际配置数据源,您知道,指定驱动程序、用户名、密码、uri 等等。

我猜 DAO 的 find() 方法有效,实际上并没有使用数据源。到目前为止你所展示的,并没有暗示你有一个初始化的数据源。

只是为了给您一个想法,我喜欢有关如何使用 Tomcat 和 JDNI 执行此操作的教程。 (或者更好的是,使用 spring-jdbc)。

http://www.mkyong.com/tomcat/how-to-configure-mysql-datasource-in-tomcat-6/

关于javax.sql.datasource getconnection 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18157290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com