Question

I've coded connection code for an embedded db in NetBeans. Something is wrong with my return connection. Any ideas? I've bolded/double-asterisked the code that is generating the errors. When trying to compile, I'm getting an error which reads, "cannot find symbol symbol: variable connection location: class ProductDB"

This is my first time working with 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;
        }
    }
}
Was it helpful?

Solution

You created your connection object as a variable in your initializing method - it goes out of scope when the method returns and is not visible in the rest of your program. You should declare the connection object as an instance variable instead.

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;
        }
    }
}

Also, you will want to call the connect method prior to actually trying to use the connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top