簡體   English   中英

為什么會收到http 500響應?

[英]Why am I getting a http 500 response?

我正在嘗試通過對路徑參數使用“ 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 = ?)");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM