- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以告诉我如何修复下面的代码,这样它就不会抛出错误吗?
下面这行代码给我一个空指针异常:
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/
我在 spring 上下文中初始化了数据源 bean。请问应该用什么方式?为什么我就是不能写 dataSource.getConnection()? 最佳答案 有一个重要区别:dataSource.g
如果我使用 DriverManager.getConnection() 和 DataSource.getConnection() 获取连接对象,当 .close() 是在那些对象上调用的? 在 .cl
在 DataSource 接口(interface)上,我发现了两种获取连接的方法,有和没有用户名和密码参数。 Connection getConnection()Connection getConn
我想对这段代码进行单元测试,以验证调用了哪个 DriverManager.getConnection() 。DriverManager.getConnection(url,user,pass) 或 D
我正在使用jsPlumb statemachine 。我想稍后保存并加载我的图表。我用谷歌搜索,发现我必须使用 json 对象来做到这一点。我尝试了几个例子stackoverflow 。我能够获得每个
如何模拟 DriverManager.getConnection() 方法? 我想测试我的方法 setUpConnectiontoDB() 我用 PowerMock、easyMock 和 Mokito
数据库连接如下所示 public Connection getDBConection(){ Context context = new InitialContext(); DataSo
我在 Java 文件中找到了这个方法,我想知道这里发生了什么?这真的有效吗?我认为这个方法什么都不做,因为 getConnection() 方法创建了一个新连接,然后它被关闭了。我说得对吗? publ
我正在尝试从nodejs中的mysql连接池获取连接。但是池的 getConnection 方法不会返回回调函数。 (因此它不会显示任何错误或返回连接)。但我的 mysql 工作台显示已建立连接。当我
我正在使用 MySQL 5.5 及其默认值。我创建了一个用户/密码并运行了一个脚本来创建一个名为员工的数据库。通过命令提示符我可以访问数据库: mysql -u user -p SELECT * FR
我有以下情况:我有一个与数据库 (MySQL) 连接的 java 代码。当我从 eclipse 运行 java 代码并从数据库中获取数据时,一切正常。但是当我从 html 代码运行 java 代码作为
我的工作电脑有一个奇怪的问题,也许你可以解决问题。我目前正在使用 jee 进行开发,我遇到了有关 MySQL DB 的问题。 假设我有这个: for(int i = 0; i<20; i++) { /
我编写了 DAO 类 public List getAllClassRooms() { List classRoomsList = new ArrayList<>(); String
在使用 BoneCP 连接池时,我遇到了以下困惑,并希望听到一些关于这方面的建议: 是 getConnection BoneCP线程安全的方法?当有许多线程请求并行连接时,使用它的最佳方法是什么? 每
最近,我们在Hive连接中添加了SSL。我们通过添加 ssl=1;sslTrustStore=C:\\keytabs\\keys.truststore;trustStorePassword=foo 到
在 SignalR 中,ITransportHeartbeat.GetConnections() 的实现应该会为我提供正在跟踪的连接列表。在我的一个案例中,我总是得到一个不再存在的网络套接字连接。 以
我正在使用jdbc连接池,如果我编写像这样的代码 DatabaseMetaData dbMeta = getConnection().getMetaData(); 其中 getConnection()
我有以下代码来测试新服务器上访问数据库的链接,一切都适用于现有服务器,并且我能够访问该文件夹。 !if.exists 返回 true,我可以使用 Runtime.getRuntime().exec("
我正在使用 Java 和 mysql 作为数据库,但遇到了一个奇怪的问题:我的一位客户的连接非常不稳定,有时丢包率可能很高。好吧,我知道这不是软件的错误,但我去那里进行了测试,当程序调用“Driver
这个问题已经有答案了: Solving a "communications link failure" with JDBC and MySQL [duplicate] (25 个回答) 已关闭 4 年
我是一名优秀的程序员,十分优秀!