2012-03-24 64 views
1

我已經編寫了NetBeans中嵌入數據庫的連接代碼。我的返回連接有問題。有任何想法嗎?我用粗體/雙星號標出了產生錯誤的代碼。當試圖編譯時,我得到一個錯誤,它顯示「無法找到符號 符號:變量連接 位置:class ProductDB」德比和NetBeans - 「連接」錯誤

這是我第一次與Derby合作。

import java.util.*; 
import java.sql.*; 

public class ProductDB implements ProductDAO 
{ 

    private static Connection connect() 
    { 
     try 
     { 
        // set the db url string 
        String dbUrl = "jdbc:derby:MurachDB"; 

        // create a Properties ovject with username and password 
        Properties properties = new Properties(); 
        properties.put("user", ""); 
        properties.put("password", ""); 

        // create and return the connection 
        Connection connection = DriverManager.getConnection(dbUrl, properties); 
        return **connection**; 
     } 
     catch(SQLException e) 
     { 
     for (Throwable t : e) 
        e.printStackTrace(); 
       return null; 
     } 
    } 

    public ArrayList<Product> getProducts() 
    { 
     try 
     { 
      ArrayList<Product> products = new ArrayList<Product>(); 

      String query = "SELECT ProductCode, Description, Price " 
         + "FROM Products ORDER BY ProductCode ASC"; 
      PreparedStatement ps = **connection**.prepareStatement(query); 
      ResultSet rs = ps.executeQuery(); 

      while(rs.next()) 
      { 
       String code = rs.getString("ProductCode"); 
       String description = rs.getString("Description"); 
       double price = rs.getDouble("Price"); 

       Product p = new Product(code, description, price); 
       products.add(p); 
      } 
      rs.close(); 
      ps.close(); 
      return products; 
     } 
     catch(SQLException sqle) 
     { 
      //sqle.printStackTrace(); // for debugging 
      return null; 
     } 
    } 

    public Product getProduct(String code) 
    { 
     try 
     { 
      String selectProduct = 
       "SELECT ProductCode, Description, Price " + 
       "FROM Products " + 
       "WHERE ProductCode = ?"; 
      PreparedStatement ps = **connection**.prepareStatement(selectProduct); 
      ps.setString(1, code); 
      ResultSet rs = ps.executeQuery(); 

      if (rs.next()) 
      { 
       String description = rs.getString("Description"); 
       double price = rs.getDouble("Price"); 
       Product p = new Product(code, description, price); 
       rs.close(); 
       ps.close(); 
       return p; 
      } 
      else 
       return null; 
     } 
     catch(SQLException sqle) 
     { 
      //sqle.printStackTrace(); // for debugging 
      return null; 
     } 
    } 

    public boolean addProduct(Product p) 
    { 
     try 
     { 
      String insert = 
       "INSERT INTO Products (ProductCode, Description, Price) " + 
       "VALUES (?, ?, ?)"; 
      PreparedStatement ps = **connection**.prepareStatement(insert); 
      ps.setString(1, p.getCode()); 
      ps.setString(2, p.getDescription()); 
      ps.setDouble(3, p.getPrice()); 
      ps.executeUpdate(); 
      ps.close(); 
      return true; 
     } 
     catch(SQLException sqle) 
     { 
      //sqle.printStackTrace(); // for debugging 
      return false; 
     } 
    } 

    public boolean deleteProduct(Product p) 
    { 
     try 
     { 
      String delete = 
       "DELETE FROM Products " + 
       "WHERE ProductCode = ?"; 
      PreparedStatement ps = **connection**.prepareStatement(delete); 
      ps.setString(1, p.getCode()); 
      ps.executeUpdate(); 
      ps.close(); 
      return true; 
     } 
     catch(SQLException sqle) 
     { 
      //sqle.printStackTrace(); // for debugging 
      return false; 
     } 
    } 

    public boolean updateProduct(Product p) 
    { 
     try 
     { 
      String update = 
       "UPDATE Products SET " + 
        "Description = ?, " + 
        "Price = ? " + 
       "WHERE ProductCode = ?"; 
      PreparedStatement ps = **connection**.prepareStatement(update); 
      ps.setString(1, p.getDescription()); 
      ps.setDouble(2, p.getPrice()); 
      ps.setString(3, p.getCode()); 
      ps.executeUpdate(); 
      ps.close(); 
      return true; 
     } 
     catch(SQLException sqle) 
     { 
      //sqle.printStackTrace(); // for debugging 
      return false; 
     } 
    } 
} 

回答

1

您在初始化方法中創建了連接對象作爲變量 - 它在方法返回時超出範圍並且在程序的其餘部分中不可見。您應該將連接對象聲明爲實例變量。

private volatile Connection connection; 

private synchronized Connection connect() { 
    if (connection != null) 
     return connection; 
    else { 
     try { 
      // ... 

      // create and return the connection 
      connection = DriverManager.getConnection(dbUrl, properties); 
      return connection; 
     } catch (SQLException e) { 
      for (Throwable t : e) 
       e.printStackTrace(); 
      return null; 
     } 
    } 
} 

此外,您將希望在實際嘗試使用連接之前調用connect方法。