简体   繁体   中英

Why am I getting a http 500 response?

I am attempting to retrieve values in my database through using 'ID' for my path parameter. However I am getting a HTTP 500 response in the URi( http://localhost:8080/JAX_RS/rest/details/123 ) when attempting to retrieve those values. Below is my DAO class. I can also provide my Resources class if needed. Any feedback would be appreciated.

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;
    }
}

You have extra comma after TIMESTAMP in query

PreparedStatement pstmt = con.prepareStatement(
                "SELECT ID, NAME, AGE, TIMESTAMP, "
                        + "FROM APP.DETAILS "
                        + "WHERE (ID = ?)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM