Apache Derbyを使用したJavaデータベースプロジェクトがあります。 Eclipse IDEでうまく動作します。しかし、プロジェクトを実行可能なJARにパッケージングした後、ソフトウェア はデータベースをロードして保存することができません。Apache Derbyデータベースを使用したJavaプロジェクトのパッケージ化
// JDBC CONNECTION METHOD
public void connect() throws Exception{
if(con != null) return;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
} catch (ClassNotFoundException e) {
throw new Exception("Driver not found");
}
String url = "jdbc:derby:src/MyDB;create=true";
con = DriverManager.getConnection(url);
}
//JDBC DISCONNECITON METHOD
public void disconnect(){
if(con != null){
try {
con.close();
} catch (SQLException e) {
}
}
}
// SAVING DATA INTO JDBC DATABASE
public void save() throws SQLException{
String checkSQL = "SELECT COUNT(*) AS Count FROM appData.stock WHERE id =? ";
PreparedStatement checkStmt = con.prepareStatement(checkSQL);
String insertSQL = "INSERT INTO appData.stock(productName,unitPrice,quantity,manufacturer,stockDate,value) VALUES(?,?,?,?,?,?)";
PreparedStatement insertStmt = con.prepareStatement(insertSQL);
for(Product product : stock){
int id = product.getId();
String productName = product.getProductName();
float price = product.getProductPrice();
int quantity = product.getQuantity();
String manufacturer = product.getManufacturer();
String date = product.getDate();
float value = product.getValue();
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery();
checkResult.next();
int count =checkResult.getInt(1);
if(count == 0){
int col = 1;
//insertStmt.setInt(col++, id);
insertStmt.setString(col++, productName);
insertStmt.setFloat(col++, price);
insertStmt.setInt(col++, quantity);
insertStmt.setString(col++, manufacturer);
insertStmt.setString(col++, date);
insertStmt.setFloat(col++, value);
insertStmt.executeUpdate();
}
}
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("Courier New",Font.PLAIN,26)));
JLabel message = new JLabel("Your Stock Database has been Updated and Saved");
message.setFont(new Font("Courier New",Font.PLAIN,21));
JOptionPane.showMessageDialog(null, message);
checkStmt.close();
insertStmt.close();
}
//RETRIEVING DATA FROM DATABASE IN JDBC
public void load() throws SQLException{
stock.clear();
String sql = "SELECT * FROM appData.stock ";
Statement selectStatement = con.createStatement();
ResultSet result = selectStatement.executeQuery(sql);
while(result.next()){
int id = result.getInt("id");
String productName = result.getString("productName");
float price = result.getFloat("unitPrice");
int quantity = result.getInt("quantity");
String manufacturer = result.getString("manufacturer");
String date = result.getString("stockDate");
float value = result.getFloat("value");
Product product = new Product(id,productName,quantity,price,manufacturer,date,value);
stock.add(product);
}
}
それは働いた。あなたは私を救った。タンクス – Asaana
@Asaana大歓迎です。あなたの質問に答えた場合、答えを正しいものとしてマークしてください。ありがとう;) – Ingrid