2016-09-16 9 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ファイルを作成するとき

は(あなたがマニフェスト情報をインポートすると仮定した場合)、...

これを試してみてください、あなたのマニフェストのクラス-Pathヘッダにルderby.jar置きます。 txtファイル

例:あなたのmanifest.txtファイルで :

のClass-Path [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

それは働いた。あなたは私を救った。タンクス – Asaana

+0

@Asaana大歓迎です。あなたの質問に答えた場合、答えを正しいものとしてマークしてください。ありがとう;) – Ingrid

関連する問題