我在NetBeans中编写了嵌入式数据库的连接代码。我的返回连接有问题。有任何想法吗?我用粗体/双星号表示产生错误的代码。在尝试编译时,我收到的错误是“无法找到符号 符号:变量连接 location:class ProductDB“
这是我第一次使用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;
}
}
}
答案 0 :(得分: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方法。