'READ operation not working. Postgres database and Java. (JAX-WS, SOAP)

The idea is that I need to create a READ operation which should get order from Database by order ID.

This is my OrderDao class method:

@Override public Order read(int orderId) throws SQLException {

    Order order = null;

    //declare and initialize variables
    int orderid = 0; String description = "error"; float amount = 0; boolean delivered = false;

    //Create a statement using connection object

    PreparedStatement statement = conn.prepareStatement("SELECT orderid, description, amount, delivered FROM orders WHERE orderid = ");
    // Set orderid in SQL statement to a parameter(orderId) that was passed when calling the READ method.
    statement.setInt(1, orderId);

    //Execute the query above (statement)
    ResultSet resultSet = statement.executeQuery();

    System.out.println("test outside while");

    // Process the ResultSet object.
    if (resultSet.next())
    {
        orderid = resultSet.getInt("orderid");
        description = resultSet.getString("description");
        amount = resultSet.getInt("amount");
        delivered = resultSet.getBoolean("delivered");
        order = new Order(orderId, description, amount, delivered);

        System.out.println("test inside while");
    }
    return order;
    }

and this is the client where I try to access and get this information from the DBS:

public class Client {

public static void main(String[] args) throws MalformedURLException, SQLException {
    URL WSDL_URL = new URL("http://localhost:8080/Dao?WSDL");
    QName SERVICE_NAME = new QName("http://service.orderSystem/", " DaoManageOrders ");

    //CREATE THE JAXWS (CXF)supplied service
    Service service = Service.create(WSDL_URL, SERVICE_NAME);
    System.out.println("SOAP Service is now CREATED...");

    Dao os = service.getPort(Dao.class); // GET PORT ALWAYS SPECIFIES INTERFACE, ELSE YOU WILL GET exception
    System.out.println("=========================");

    //WE CREATE ORDERS HERE:
    Order order1 = new Order(1,"Beer", 200, false);
    Order read = new Order();
    read.setDescription(os.read(1).getDescription());
    System.out.println(read.getDescription());

I cannot understand why I am getting these exceptions:

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Marshalling Error: java.sql.SQLException is not known to this context
at org.apache.cxf.jaxws.JaxWsClientProxy.mapException(JaxWsClientProxy.java:195)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
at jdk.proxy2/jdk.proxy2.$Proxy30.read(Unknown Source)
at orderSystem.client.Client.main(Client.java:31)


Solution 1:[1]

I fixed it:

The SQL query was missing "?" operator in OrderDao class. Also in order Data transfer object class I put to string method - makes more sense.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s