读取操作不起作用。 Postgres 数据库和 Java。 (JAX-WS、SOAP)

发布于 2025-01-13 11:44:40 字数 2524 浏览 2 评论 0原文

我的想法是,我需要创建一个 READ 操作,该操作应按订单 ID 从数据库获取订单。

这是我的 OrderDao 类方法:

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

这是我尝试从 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());

我无法理解为什么会收到这些异常:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2025-01-20 11:44:40

我修复了它:

SQL 查询缺少“?” OrderDao 类中的运算符。另外,为了数据传输对象类,我将其放入字符串方法 -​​ 更有意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文