我试图通过使用'ID'作为我的路径参数来检索数据库中的值。但是,在尝试检索这些值时,我在URi(http://localhost:8080/JAX_RS/rest/details/123)中收到HTTP 500响应。下面是我的DAO课程。如果需要,我也可以提供我的Resources类。对于任何反馈,我们都表示感谢。
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;
}
}
在TIMESTAMP
查询后你有额外的逗号
PreparedStatement pstmt = con.prepareStatement(
"SELECT ID, NAME, AGE, TIMESTAMP, "
+ "FROM APP.DETAILS "
+ "WHERE (ID = ?)");