首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Derby和NetBeans - "connection“错误

Derby和NetBeans - "connection“错误
EN

Stack Overflow用户
提问于 2012-03-24 12:02:13
回答 1查看 779关注 0票数 1

我已经为NetBeans中的嵌入式数据库编写了连接代码。我的返回连接有问题。有什么想法吗?我用粗体/双星号标记了生成错误的代码。在尝试编译时,我得到一个错误消息:“找不到符号符号:变量连接位置:类ProductDB”

这是我第一次使用Derby。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-24 12:21:10

您在初始化方法中将connection对象创建为一个变量-当该方法返回时,它会超出作用域,并且在程序的其余部分不可见。您应该将connection对象声明为实例变量。

代码语言:javascript
复制
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方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9849046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档