2011-05-18 79 views
0

我在這裏有一個小問題。 基本上我想創建一個連接池使用一個類的數據庫。該池可供其他類用於執行查詢。我已經創建了連接類的其他類子類。 這是我到目前爲止。與數據庫的Java連接

Connection類/(連接池類)

import java.sql.*; public class connect extends cPool { 
    public static void main(String[] args) { 
     cPool en = new cPool(); //crate an object in the name of cPoll calss 
     Connection conn = null; 
     Object data; 
     data = (connect) conn; 
     en.readinfo(); //call object then method name 
     String userName = "root"; 
      String password = "" + en.paword + "";// hold outside try catch block to get and set 
      String url = "" + en.url + ""; 

     try 
     { 

      Class.forName ("com.mysql.jdbc.Driver").newInstance(); 
      conn = DriverManager.getConnection (url, userName, password); 
      System.out.println ("Database connection established"); 



     } 
     catch (Exception e) 
     { 
      System.err.println ("Cannot connect to database server"); 
      System.err.println("Tried connecting using" + url + userName + password +""); 

     } 
     finally 
     { 

     } 
    }  

}

這裏是執行語句類

import java.sql.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class getProduct extends connect { 
    public static void main(String[] args) { 
     connect cn = new connect(); 
     Connection conn = cn.data; 

     try { 
      Statement stmt = conn.createStatement(); 
      ResultSet rs = stmt.executeQuery("SELECT * FROM My_Table"); 
     } 
     catch (SQLException ex) { 
      Logger.getLogger(getProduct.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     finally 
     { 

     } 
    } 
} 

我不能執行任何語句。從第二課,當我做我得到一個錯誤與createStatement()。它說'不可編譯的源代碼 - 找不到符號' 非常感謝。

+1

那麼問題是什麼?我沒有看到問題。 – duffymo 2011-05-18 09:09:13

+0

和有什麼問題? – Fortega 2011-05-18 09:09:55

+0

我無法執行任何語句。從我做的時候,我得到了createStatement()的錯誤。它說'不可編譯的源代碼 - 找不到符號' – Sunny 2011-05-18 09:13:06

回答

1

連接池是一個高級主題,由您的代碼判斷我會說讓它暫時休息一下並首先學習一些Java基礎知識。因此,也許你應該使用現有的解決方案,而不是:

1

你有問題。

我不會推薦所有這種繼承; extends不是一個好主意。

getProduct應該是一種方法,而不是一個類。

你的getProduct類是沒有用的。你不會從中得到結果。你不清理資源。在你編寫一個合適的JDBC類之前,不要擔心集中。

像這樣的東西(左一些事情爲你打開弄清楚):

package persistence; 

public class ProductDaoImpl implements ProductDao 
{ 
    private static final String BASE_SELECT = "select * from product "; 

    private Connection connection; 

    public ProductDaoImpl(Connection connection) { this.connection = connection; } 

    public List<Product> find() throws SQLException 
    { 
     List<Product> products = new ArrayList<Product>(); 

     Statement st = null; 
     ResultSet rs = null; 

     try 
     { 
      st = this.connection.createStatement(); 
      rs = st.executeQuery(BASE_SELECT); 
      while (rs.next()) 
      { 
       Product product = new Product(); 
       // map columns into product 
       products.add(product); 
      } 
     } 
     finally 
     { 
      DatabaseUtils.close(rs); 
      DatabaseUtils.close(st); 
     } 

     return products; 
    } 
} 
+0

感謝幫助。 – Sunny 2011-05-18 10:17:52

+0

如果有幫助,就表決答案;接受它,如果它是正確的。我更喜歡你的感謝。 – duffymo 2011-05-18 11:23:25

+0

我沒有5點聲望點這麼做..... – Sunny 2011-05-18 16:24:42

0

不知道問題是什麼,但我認爲一個合理的答案是「不寫自己」。有許多不錯的選擇,其中兩個由肖恩(+1)提及。我會添加我自己的最愛:BoneCP