读取操作不起作用。 Postgres 数据库和 Java。 (JAX-WS、SOAP)
我的想法是,我需要创建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了它:
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.