我的网络服务遇到了逻辑错误。它返回0而不是返回总价格。这是因为它要求我的代码底部的另一个return语句,是否存在此错误?在此先感谢您的帮助
public int checkPart(java.lang.String partname, int quantity) throws ClassNotFoundException, SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://ConnectionString);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT PartName, Price, Quantity FROM parts WHERE PartName =" + partname + ";");
while (rs.next()) {
int PriceVar = rs.getInt(1);
int total = PriceVar * rs.getInt(2);
return total;
}
rs.close();
return 0;
}
catch (ClassNotFoundException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
}
return 0;
答案 0 :(得分:0)
试试这种格式。
int total = 0;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://ConnectionString");
// ...
if (rs.next()) {
int PriceVar = rs.getInt(1);
total = PriceVar * rs.getInt(2);
}
rs.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (con != null) {
con.close();
}
}
return total;
答案 1 :(得分:0)
你最好在一段时间内添加休息时间以获得第一个结果:
int total = 0;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://ConnectionString");
// ...
if (rs.next()) {
int PriceVar = rs.getInt(1);
total = PriceVar * rs.getInt(2);
break; // get first one
}
rs.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(WarehouseWebService.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (con != null) {
con.close();
}
}
return total;
答案 2 :(得分:0)
使用 SQL 的sum()
功能代替执行此操作
SELECT SUM(price) FROM table_name;
这将从您的表 table_name
中显示价格列的返还金额修改强>
ResultSet rs = st.executeQuery("SELECT SUM(Price) FROM parts WHERE PartName =" + partname + ";");
P.S 采取这个概念,不要只是复制粘贴查询。它可能有不包括撇号的问题。 :-)
答案 3 :(得分:0)
首先,您的查询不正确(您确实应该绑定参数)。简短的回答,改变这个
WHERE PartName =" + partname + ";");
到
WHERE PartName ='" + partname + "';"); // <-- single quotes.
但是,我认为你真的想要(使用try-with-resources
来清理)
public int checkPart(String name, int quantity)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://ConnectionString";
String sql = "SELECT Price FROM parts WHERE PartName=?";
try (Connection con = DriverManager.getConnection(url);
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
}
throw new SQLException(String.format("No PartName={%s} Found", name));
}
答案 4 :(得分:-1)
当这种情况声明一个局部变量并最后返回它时。尽量避免一次返回它会造成混乱。 你可以使用sql的sum方法,你可以很容易地得到所有的总和,你可以避免循环。