2016-09-16 42 views
-2

我有一個使用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); 
    } 
} 

回答

0

@Asaana你說這工作在日食精細,這樣使我相信,現在你正在運行IDE之外,derby.jar文件是不是在你的類路徑中。

試試這個,如果你還沒有這樣做的話......

當您創建jar文件,(假設你進口艙單信息),在清單的類路徑頭放的derby.jar。 txt文件

例如, 在你manifest.txt文件:

包含此:

類路徑[DERBY_HOME] \ LIB \的derby.jar

其中[DERBY_HOME]是你的德比所在。因爲我不在我的屏幕附近,所以這關閉了我的頭頂。道歉。

0

@Ingrid在將它打包成一個可運行Jar文件後,我的軟件能夠找到derby.jar文件,但它無法找到我的數據庫SCHEMA和保存的數據。 謝謝。

0

好吧,我明白了。糾正我,如果我錯了,但你在IDE中創建你的表。您需要直接在應用程序內創建表(如下所示)或通過腳本創建表。

以下是創建數據庫,添加表格並直接從應用程序中插入數據的示例。 數據庫(MYDB)將在項目文件夾中創建(你啓動應用程序的文件夾。)

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import javax.swing.JOptionPane; 


public class DerbyExample {  

    private Connection con = null;  
    private final String dbName = "MyDB";  
    private final String protocol = "jdbc:derby:"; 
    private PreparedStatement psInsert;  
    private Statement s; 

    public DerbyExample() throws SQLException { 
     try {     
      con = DriverManager.getConnection(protocol + dbName + ";create=true"); 
     } 
     catch (SQLException ex) { 
      con = null; 
     } 
     if (con == null) { 
      JOptionPane.showMessageDialog(null, "Could not create/connect to shop database! - Contact System Administrator.  "); 
      System.exit(1); 
     }    
     con.setAutoCommit(false);    

      s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 

      s.execute("create table CATEGORIES(CATEGORYID int NOT NULL, CATEGORYNAME varchar(20))"); 
      s.execute("ALTER TABLE CATEGORIES ADD CONSTRAINT CATEGORYID_PK PRIMARY KEY (CATEGORYID)"); 
      psInsert = con.prepareStatement("insert into CATEGORIES values (?, ?)"); 
      psInsert.setInt(1, 1); 
      psInsert.setString(2, "Breakfast"); 
      psInsert.executeUpdate(); 
      psInsert.setInt(1, 3); 
      psInsert.setString(2, "Coffee"); 
      psInsert.executeUpdate(); 
      psInsert.setInt(1, 4); 
      psInsert.setString(2, "Dessert"); 
      psInsert.executeUpdate(); 
      con.commit(); 

      ResultSet rs = s.executeQuery("select * from CATEGORIES"); 
      rs.beforeFirst(); 
      while (rs.next()) { 
       System.out.println(rs.getInt(1)); 
       System.out.println(rs.getString(2)); 
      }    
      rs.close();   

      disconnect();   
     } 
    public void disconnect(){ 
     // Shutdown Derby 
     try { 
      DriverManager.getConnection("jdbc:derby:;shutdown=true"); 
     } catch (SQLException ex) {} 

     // Close statement (PreparedStatement extend Statement)    
     if (s != null) { 
      try { 
       s.close(); 
       s = null; 
      } catch (SQLException ex) {}   
     } 
     // Close connection 
     if(con != null){ 
      try { 
       con.close(); 
       con = null; 
      } catch (SQLException e) {} 
     }    
    } 

    public static void main(String[] args) throws SQLException { 

     DerbyExample ex = new DerbyExample();   
    } 
} 

讓我知道,如果這個解決方案可以解決你的問題。您可以在Derby Reference ManualDerby Developer's Guide以及示例here中找到大量信息。

+0

它工作。你救了我.Thankz – Asaana

+0

@Asaana歡迎您。如果回答您的問題,請將答案標記爲正確。謝謝 ;) – Ingrid