2012-03-24 8 views
1

Ich habe Verbindungscode für eine eingebettete DB in NetBeans codiert. Etwas stimmt nicht mit meiner Rückverbindung. Irgendwelche Ideen? Ich habe den Code, der die Fehler erzeugt, fett/doppelt markiert. Wenn zu kompilieren versuchen, ich erhalte eine Fehlermeldung, die lautet: „kann nicht Symbol Symbol finden: variable Verbindung Standort: Klasse ProductDB“Derby und NetBeans - Fehler mit "Verbindung"

Dies ist mein erstes Mal der Arbeit mit 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; 
     } 
    } 
} 

Antwort

1

Sie erstellt Ihre Verbindungsobjekt als Variable in Ihrem Initialisierungsverfahren - es außerhalb des Gültigkeitsbereichs geht, wenn die Methode zurückgibt und im Rest des Programms nicht sichtbar ist. Sie sollten das Verbindungsobjekt stattdessen als Instanzvariable deklarieren.

Sie sollten auch die Verbindungsmethode aufrufen, bevor Sie versuchen, die Verbindung tatsächlich zu verwenden.