【发布时间】:2019-03-28 21:29:15
【问题描述】:
我正在尝试通过对路径参数使用“ID”来检索数据库中的值。但是,在尝试检索这些值时,我在 URi(http://localhost:8080/JAX_RS/rest/details/123) 中收到了 HTTP 500 响应。下面是我的 DAO 课程。如果需要,我还可以提供我的资源类。对于任何反馈,我们都表示感谢。
DetailsDAO
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DetailsDAO {
private Connection con = null;
public DetailsDAO() {
try {
System.out.println("Loading db driver...");
//Class.forName("org.apache.derby.jdbc.ClientDriver");
System.out.println("Driver loaded...");
con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/SOA4_DB",
"sean",
"sean");
} catch (SQLException ex) {
System.out.println("Exception!");
ex.printStackTrace();
}
}
public static void main(String[] args) {
DetailsDAO dao = new DetailsDAO(); // connect to db
List<Details> detailsList = dao.getAllDetails();
for (Details d : detailsList) {
System.out.println(d);
}
}
public List<Details> getAllDetails() {
List<Details> detailsList = new ArrayList<>();
try {
// SQL in here
PreparedStatement ps = con.prepareStatement(
"SELECT * FROM APP.DETAILS"
);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Details d = new Details
(rs.getInt("ID"),
rs.getString("NAME"),
rs.getInt("AGE"),
rs.getTimestamp("TIMESTAMP"));
detailsList.add(d);
}
} catch (SQLException ex) {
System.err.println("\nSQLException");
ex.printStackTrace();
}
return detailsList;
}
public Details getDetails(int id){
Details details = null;
try{
// SQL in here
PreparedStatement pstmt = con.prepareStatement(
"SELECT ID, NAME, AGE, TIMESTAMP, "
+ "FROM APP.DETAILS "
+ "WHERE (ID = ?)");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
// move the cursor to the start
if(!rs.next()){ // !F == T
return null;
}
// we have at least one record
details = new Details
(rs.getInt("ID"),
rs.getString("NAME"),
rs.getInt("AGE"),
rs.getTimestamp("TIMESTAMP"));
} catch (SQLException ex) {
Logger.getLogger(DetailsDAO.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("\nSQLException");
ex.printStackTrace();
}
return details;
}
}
【问题讨论】:
-
服务器上是否生成了异常和/或堆栈跟踪?
-
@NotaJD 异常 javax.servlet.ServletException: java.lang.NullPointerException 根本原因 java.lang.NullPointerException
标签: java http get localhost derby