Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Derby和NetBeans-错误为“0”;“连接”;_Netbeans_Derby_Embedded Database - Fatal编程技术网

Derby和NetBeans-错误为“0”;“连接”;

Derby和NetBeans-错误为“0”;“连接”;,netbeans,derby,embedded-database,Netbeans,Derby,Embedded Database,我已经为NetBeans中的嵌入式db编写了连接代码。我的回程连接有问题。有什么想法吗?我用粗体/双星号标出了产生错误的代码。当试图编译时,我得到一个错误,它是“找不到符号” 符号:变量连接 位置:类别ProductDB“ 这是我第一次和德比合作 import java.util.*; import java.sql.*; public class ProductDB implements ProductDAO { private static Connection connect(

我已经为NetBeans中的嵌入式db编写了连接代码。我的回程连接有问题。有什么想法吗?我用粗体/双星号标出了产生错误的代码。当试图编译时,我得到一个错误,它是“找不到符号” 符号:变量连接 位置:类别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;
        }
    }
}
import java.util.*;
导入java.sql.*;
公共类ProductDB实现ProductDAO
{
专用静态连接
{
尝试
{
//设置数据库url字符串
字符串dbUrl=“jdbc:derby:MurachDB”;
//使用用户名和密码创建属性对象
属性=新属性();
properties.put(“用户”,“用户”);
properties.put(“密码”,“密码”);
//创建并返回连接
Connection Connection=DriverManager.getConnection(dbUrl,properties);
返回**连接**;
}
捕获(SQLE异常)
{
用于(可丢弃的t:e)
e、 printStackTrace();
返回null;
}
}
公共阵列列表getProducts()
{
尝试
{
ArrayList产品=新的ArrayList();
String query=“选择产品代码、说明、价格”
+“根据产品代码ASC从产品订单”;
PreparedStatement ps=**连接**.prepareStatement(查询);
结果集rs=ps.executeQuery();
while(rs.next())
{
字符串代码=rs.getString(“产品代码”);
字符串描述=rs.getString(“描述”);
双倍价格=rs.getDouble(“价格”);
产品p=新产品(代码、说明、价格);
产品.加入(p);;
}
rs.close();
ps.close();
退货产品;
}
捕获(SQLException sqle)
{
//sqle.printStackTrace();//用于调试
返回null;
}
}
公共产品getProduct(字符串代码)
{
尝试
{
字符串选择产品=
选择产品代码、说明、价格+
“来自产品”+
“其中ProductCode=?”;
PreparedStatement ps=**连接**.PreparedStatement(选择产品);
ps.setString(1,代码);
结果集rs=ps.executeQuery();
如果(rs.next())
{
字符串描述=rs.getString(“描述”);
双倍价格=rs.getDouble(“价格”);
产品p=新产品(代码、说明、价格);
rs.close();
ps.close();
返回p;
}
其他的
返回null;
}
捕获(SQLException sqle)
{
//sqle.printStackTrace();//用于调试
返回null;
}
}
公共布尔addProduct(产品p)
{
尝试
{
插入字符串=
在产品中插入(产品代码、说明、价格)+
“值(?,?)”;
PreparedStatement ps=**连接**。PreparedStatement(插入);
ps.setString(1,p.getCode());
ps.setString(2,p.getDescription());
ps.setDouble(3,p.getPrice());
ps.executeUpdate();
ps.close();
返回true;
}
捕获(SQLException sqle)
{
//sqle.printStackTrace();//用于调试
返回false;
}
}
公共布尔删除产品(产品p)
{
尝试
{
字符串删除=
“从产品中删除”+
“其中ProductCode=?”;
PreparedStatement ps=**连接**.PreparedStatement(删除);
ps.setString(1,p.getCode());
ps.executeUpdate();
ps.close();
返回true;
}
捕获(SQLException sqle)
{
//sqle.printStackTrace();//用于调试
返回false;
}
}
公共布尔更新产品(产品p)
{
尝试
{
字符串更新=
“更新产品集”+
“说明=?”+
“价格=?”+
“其中ProductCode=?”;
PreparedStatement ps=**连接**.PreparedStatement(更新);
ps.setString(1,p.getDescription());
ps.setDouble(2,p.getPrice());
ps.setString(3,p.getCode());
ps.executeUpdate();
ps.close();
返回true;
}
捕获(SQLException sqle)
{
//sqle.printStackTrace();//用于调试
返回false;
}
}
}

您在初始化方法中将连接对象创建为变量-当方法返回时,它超出范围,在程序的其余部分不可见。您应该将连接对象声明为实例变量

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方法。

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

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方法