所以我有一个mySQL数据库,其中有更多表,包括:
Product with idProduct,name,price and quantity (INT,Varchar,double,int) types.
Client with idClient,name,address,email (INT,varchar,varchar,varchar) types.
我有以下代码用于从客户/产品中选择* *,其中idClient / idProduct = X。 客户端代码有效,但产品代码无效。
public Product findById(int id) {
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
int idp=0;
String nume="";
double pret=0;
int q=0;
String query="Select * from Product Where idProduct="+id+";";
int ok=0;
try {
con=ConnectionFactory.getConnection();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
ok=1;
idp=rs.getInt("idProduct");
nume=rs.getString("name");
pret=rs.getDouble("price");
q=rs.getInt("quantity");
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
ConnectionFactory.close(con);
ConnectionFactory.close(stmt);
ConnectionFactory.close(rs);
}
if (ok==1)
{
Product nou=new Product(idp,nume,pret,q);
return nou;
}
else return null;
}
}
public Client findById(int id) {
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
int idc=0;
String nume="";
String adr="";
String em="";
String ph="";
String query="Select * from Client Where idClient="+id+";";
int ok=0;
try {
con=ConnectionFactory.getConnection();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
ok=1;
idc=rs.getInt("idClient");
nume=rs.getString("name");
adr=rs.getString("address");
em=rs.getString("email");
ph=rs.getString("phone");
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
ConnectionFactory.close(con);
ConnectionFactory.close(stmt);
ConnectionFactory.close(rs);
}
if (ok==1)
{
Client nou=new Client(idc,nume,adr,em,ph);
return nou;
}
else return null;
}'''
在我看来,它完全一样。结果是: '''
model.Product@32eff876
[idClient: 1 George address: Cluj email: george@y.com phone: 0722]
'''
因此它对客户有效10/10,但对产品无效。您能帮我吗?我检查了数据库,一切正常。
这是我直接从mysql服务器复制的日期,因此名称也不错,类型也一样。我不知道那是什么,我删除了表并再次创建了几次,我也重新创建了整个数据库,但是它是相同的
Table: product
Columns:
idProduct int AI PK
name varchar(45)
price double
quantity int
Table: client
Columns:
idClient int AI PK
name varchar(45)
address varchar(45)
email varchar(45)
phone varchar(45)
insert语句对它们两个都适用。
答案 0 :(得分:0)
有一个用于SQL调试的公式:
答案 1 :(得分:0)
如前所述,我提供的评论解决了该问题。以下只是改善代码的方法。
Why do you think it doesn't work? Product is showing to be non-null model.Product@32eff876. Add a toString method to product class.
PreparedStatement
。ok
设置为1,而是快速失败,如果存在则从while循环返回结果。IllegalStateException
public Product findById(int id) {
String query="SELECT * FROM Product WHERE idProduct=?;";
try (Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = con.prepareStatement(query)) {
stmt.setInt(1, id);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
int productId = rs.getInt("idProduct");
String name = rs.getString("name");
double price = rs.getDouble("price");
int quantity = rs.getInt("quantity");
return new Product(productId, name, price, quantity);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
throw new IllegalArgumentException(String.format("No product can be found for this id: %s", id));
}