gpt4 book ai didi

java - jsp在表单上控制服务器端

转载 作者:行者123 更新时间:2023-11-29 17:26:14 25 4
gpt4 key购买 nike

我想将发送的数据与数据库中的所有数据进行比较。该 coce 仅将日期插入与最后一行进行比较。如何与数据库中的所有行进行比较?...................................................... ................................................

String sData= request.getParameter("idatadata");
String sAzienda= request.getParameter("idazienda");
String sCommessa= request.getParameter("idcommessa");



String date = "";
String company = "";
String order = "";




Connect con = new Connect();

try {

Connection connection = con.createConnection();

Statement statement = connection.createStatement();
String sql ="SELECT * FROM table";

ResultSet resultSet = statement.executeQuery(sql);

while(resultSet.next()) {
date = resultSet.getString("iddata");
company = resultSet.getString("idazienda");
order = resultSet.getString("idcommessa");

}

if((sData.equals(date) && sAzienda.equals(company)) && sCommessa.equals(order)) {
out.print("already sent");
con.closeConnection(connection);

}

else {

DbConnect.insertInDb(connection, sData, sOre, sMinuti, sAzienda, sCommessa, sRifInterno);
dbc.closeConnection(connection);

response.getWriter().append("ok! ");

}
} catch (Exception e) {

e.printStackTrace();
}

}

最佳答案

您只检查最后一行的原因是,每次检索一行时,您的 while 循环都会覆盖您的局部变量:

while(resultSet.next()) {
// these lines overwrite the local vars for each row
date = resultSet.getString("iddata");
company = resultSet.getString("idazienda");
order = resultSet.getString("idcommessa");
}

但是在进入数据库的下一行之前,您实际上并没有检查循环内的本地变量。您应该将检查添加到循环中:

while(resultSet.next()) {
date = resultSet.getString("iddata");
company = resultSet.getString("idazienda");
order = resultSet.getString("idcommessa");

// add your check here
if((sData.equals(date) && sAzienda.equals(company)) && sCommessa.equals(order)) {
out.print("already sent");
break;
}
}

最好只根据您要查找的数据执行选择。如果没有得到结果,则数据不在数据库中。这种方法效率更高。如果您确实决定走这条路(这是一个好主意),请使用准备好的语句,这样您就不会在代码中引入 SQL 注入(inject)漏洞。

关于java - jsp在表单上控制服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com