开发者

Retrieving data from database and writing new data into mysql

I have the following code running on Tomcat and the loop is not working. Everytime I am trying to ch开发者_StackOverflow社区eck 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>


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.


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!")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜