从数据库中检索数据并将新数据写入mysql
我在 Tomcat 上运行以下代码,但循环不起作用。每次我尝试检查名为“enero”的表中的新数据时,都没有正确遵循条件,并且只完成了第一个操作...以表中输入的重复记录结束。
你能帮忙吗?
<%-- <jsp:getProperty name="beanInstanceName" property="propertyName"/> --%>
<%@page import="java.sql.*"%>
<%
String REF=request.getParameter("d_ref");
String DATE=request.getParameter("i_date");
String UBC=request.getParameter("i_ubc");
%>
<%
try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection connection=DriverManager.getConnection("jdbc:odbc:db1","","");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/labacacias?user=root&password=letmein");
Statement statement=connection.createStatement();
String query;
query="SELECT * FROM enero";
boolean b=false;
ResultSet resultSet=statement.executeQuery(query);
while (resultSet.next())
{
if (REF.equals(resultSet.getString(1)) && DATE.equals(resultSet.getString(2)) && UBC.equals(resultSet.getString(3)))
{
out.println("Datos ya han sido adjuntados anteriormente!");
out.println("<a href=\"EneroU.jsp\">Click Aqui para Adjuntar nuevos Resultados</a>");
break;
}
if (!REF.equals(resultSet.getString(1)) && !DATE.equals(resultSet.getString(2)) && !UBC.equals(resultSet.getString(3)))
{
pageContext.forward("InsertE.jsp");
break;
}
}
}
catch (Exception e)
{
//e.printStackTrace();
out.println(e.toString());
}
%>
</body>
I have the following code running on Tomcat and the loop is not working. Everytime I am trying to check for new data in the table called "enero", the conditions are not followed correctly and only the first action is completed... ending in duplicates records entered in the table.
Can you help please!
<%-- <jsp:getProperty name="beanInstanceName" property="propertyName"/> --%>
<%@page import="java.sql.*"%>
<%
String REF=request.getParameter("d_ref");
String DATE=request.getParameter("i_date");
String UBC=request.getParameter("i_ubc");
%>
<%
try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection connection=DriverManager.getConnection("jdbc:odbc:db1","","");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/labacacias?user=root&password=letmein");
Statement statement=connection.createStatement();
String query;
query="SELECT * FROM enero";
boolean b=false;
ResultSet resultSet=statement.executeQuery(query);
while (resultSet.next())
{
if (REF.equals(resultSet.getString(1)) && DATE.equals(resultSet.getString(2)) && UBC.equals(resultSet.getString(3)))
{
out.println("Datos ya han sido adjuntados anteriormente!");
out.println("<a href=\"EneroU.jsp\">Click Aqui para Adjuntar nuevos Resultados</a>");
break;
}
if (!REF.equals(resultSet.getString(1)) && !DATE.equals(resultSet.getString(2)) && !UBC.equals(resultSet.getString(3)))
{
pageContext.forward("InsertE.jsp");
break;
}
}
}
catch (Exception e)
{
//e.printStackTrace();
out.println(e.toString());
}
%>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查你是否有条件。有很多&&在其中。
如果每次都完成第一个操作,则结果集中和参数中的字符串可能满足 if 条件。
Check you if condition. There are many && in it.
If first action is completed every time there might be a possibility that the strings in resultset and parameters are fulfilling the if condition.
我不确定这是否是正确的方法。您似乎从表中选择所有行,并一次将它们与作为请求参数传入的详细信息进行比较。如果它们全部匹配,则说明数据已经存在,否则,如果传入的值不匹配,则转发到不同的页面。除非你的表只有一行,否则你会得到奇怪的结果。
更有意义的是从表中选择 col1= REF 和 col2 = DATE 且 col3 = UBC。如果 rs.next() == false,则 pageContext.forward(),否则 out.println("Datos ya han sido adjuntados anteriormente!")
I'm not sure that this is the correct approach here. You seem to be selecting all rows from the table and comparing them one at a time to the details coming in as request parameters. If they all match, you say the data already exists, otherwise if the values passed in do not match you forward to a different page. Unless your table has only one row in it then you're going to get strange results.
What would make more sense would be to select from your table where col1= REF and col2 = DATE and col3 = UBC. If rs.next() == false, pageContext.forward(), else out.println("Datos ya han sido adjuntados anteriormente!")