gpt4 book ai didi

java - 为什么我收到 http 500 响应?

转载 作者:行者123 更新时间:2023-12-02 10:04:24 26 4
gpt4 key购买 nike

我正在尝试通过使用“ID”作为路径参数来检索数据库中的值。但是,当我尝试检索这些值时,我在 URi( http://localhost:8080/JAX_RS/rest/details/123 ) 中收到 HTTP 500 响应。下面是我的 DAO 类(class)。如果需要,我还可以提供我的资源类(class)。对于任何反馈,我们都表示感谢。

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 = ?)");

关于java - 为什么我收到 http 500 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55407128/

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