【问题标题】:Derby and NetBeans - errors with "connection"Derby 和 NetBeans - “连接”错误
【发布时间】:2023-04-02 12:27:01
【问题描述】:

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

这是我第一次与德比合作。

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

【问题讨论】:

    标签: netbeans derby embedded-database


    【解决方案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 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-14
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多