【发布时间】:2014-10-09 14:59:13
【问题描述】:
我在嵌入式模式下使用 Derby 数据库。我编写了一个带有 main() 函数的 CreateDB 类,该函数在表中创建/插入/选择。工作正常。(我在项目构建路径中有必要的 jar 文件:))
但是,当我使用相同的数据库和表在我的 Soap Web 服务中选择行时,它给我的错误是表不存在。这是代码: 对于 CreateDB(工作得很好)
package com.myShop.www.ShipmentService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateDB {
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:Shipment_DB;create=true";
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
String SQLString = "CREATE TABLE SHPMTTYPAV_TBL1(" +
"SHPMTID INT NOT NULL, " +
"NAME VARCHAR(30) NOT NULL, " +
"DESCR VARCHAR(45) NOT NULL, " +
"MINCOST DOUBLE NOT NULL, " +
"COSTPEROUNCE DOUBLE NOT NULL, " +
"ESTDLVYDAYS INT NOT NULL, " +
"PRIMARY KEY(SHPMTID))";
connection.createStatement().execute((SQLString));
String insertSQL = "INSERT INTO SHPMTTYPAV_TBL1 VALUES(5, 'Priority Mail', 'Any item can be shipped', 5, .02, 1)";
connection.createStatement().execute(insertSQL);
// invoke sql query
String selectSQL = "SELECT * From SHPMTTYPAV_TBL1";
Statement prepStmt = connection.createStatement();
ResultSet rs = prepStmt.executeQuery(selectSQL);
while (rs.next()){
System.out.println(rs.getInt("SHPMTID") + rs.getString("NAME") + rs.getString("DESCR") + rs.getDouble("MINCOST") + " " + rs.getDouble("COSTPEROUNCE"));
}
}
}
SOAP 服务实现类中的代码:- 找不到表的错误
public class ShipmentServiceBindingImpl implements ShipmentServicePortType{
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:Shipment_DB;create=true";
public com.myShop.www.ShipmentService.ItemsInOrderResponseType[] getShpmtTps_Csts_DlvyDts(com.myShop.www.ShipmentService.ItemsInOrderType[] itemsInOrder) throws java.rmi.RemoteException, ClassNotFoundException, SQLException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
String selectSQL = "SELECT * From SHPMTTYPAV_TBL1";
Statement prepStmt = connection.createStatement();
ResultSet rs = prepStmt.executeQuery(selectSQL);
int len = itemsInOrder.length;
ItemsInOrderResponseType[] res = new ItemsInOrderResponseType[len];
for(int i = 0; i < len; i++){
rs.next();
res[i] = new ItemsInOrderResponseType();
res[i].setShpmtID(rs.getInt("SHPMTID"));
res[i].setShpmtName(rs.getString("NAME"));
res[i].setCost(rs.getDouble("COSTPEROUNCE"));
res[i].setEstDlvyDays(rs.getDouble("MINCOST"));
}
if (rs != null) rs.close();
if (prepStmt != null) prepStmt.close();
if (connection != null) connection.close();
return res;
}
}
【问题讨论】: