[英]How do I retrieve a single cell from MySQL database using query and assigned it to a variable in Java Netbeans Apache
First I am going to try using query to retrieve the int min_stock single cell using the item description.首先,我将尝试使用查询来使用项目描述检索 int min_stock 单个单元格。 Then put that value into a variable.然后将该值放入变量中。 I want to be able to have the variable minStock to be equal to a number.我希望能够让变量 minStock 等于一个数字。 I want to use it to make operations in my program.我想用它在我的程序中进行操作。
PreparedStatement cm = con.prepareStatement(checkMinimumStock);
ResultSet minS = cm.executeQuery("SELECT min_stock FROM items WHERE item_description = '"+item+"'");
int minStock = minS.getInt("min_stock");```
you are choose wrong way to use PrepareStatment
.您选择了错误的方式来使用PrepareStatment
。
you have two option to do:你有两个选择:
1: 1:
String sql = "SELECT min_stock FROM items WHERE item_description =?";
PreparedStatement cm = con.prepareStatement(sql);
cm.setString(1, item);
ResultSet rs = cm.executeQuery();
2: 2:
String sql = "SELECT min_stock FROM items WHERE item_description = '" + item + "'";
ResultSet rs = con.createStatement().executeQuery(sql);
and then接着
if (rs.next())
int minStock = rs.getInt("min_stock");
else
//not found any match row in DB table
Try this尝试这个
String checkMinimumStock = "SELECT min_stock FROM items WHERE item_description = ? ";
PreparedStatement cm = con.prepareStatement(checkMinimumStock);
cm.setString(1,item);
ResultSet minS = cm.executeQuery();
if(minS.next()){
int minStock = rs.getInt("min_stock");
}
String checkMinimumStock = "SELECT min_stock FROM items WHERE item_description =? ";
Forgot to add this in the beginning of the code!忘记在代码开头添加这个了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.