简体   繁体   中英

How do i retrieve data from the database using jsp

I want to retrieve information from my database.I got the input dynamically from user using html and through jsp i get the information from the database(Mysql).The following is the jsp code

       Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/indi", "root", "");

Statement statement = connection.createStatement();

         String id1 = request.getParameter("id");   

         ResultSet resultset = statement.executeQuery("select * from books where author = '" + id1 + "'") ; 

            if(!resultset.next()) {
                out.println("Sorry, could not find that publisher. ");
            } else {
        %>

        <TABLE BORDER="1">
            <TR>
               <TH>name</TH>
               <TH>author</TH>
               <TH>money</TH>
               <TH>company</TH>

           </TR>


           <TR>
               <TD> <%= resultset.getString(1) %> </TD>
               <TD> <%= resultset.getString(2) %> </TD>
               <TD> <%= resultset.getString(3) %> </TD>
               <TD> <%= resultset.getString(4) %> </TD>

           </TR>

       </TABLE>
       <BR>
       <% 
           } 
    }
       %>

I used author as a keyword to retrieve the data.Now i have 2 authors with the same name in my database but the above code fetches only one authors info ie the first one and it leaves the other.Where should i modify in this code so that it will retrieve both the data

try to make a loop on resultset.

while(rs.next( )){  
   %>
<TABLE BORDER="1">
        <TR>
           <TH>name</TH>
           <TH>author</TH>
           <TH>money</TH>
           <TH>company</TH>

       </TR>


       <TR>
           <TD> <%= resultset.getString(i) %> </TD>
           <TD> <%= resultset.getString(i+1) %> </TD>
           <TD> <%= resultset.getString(i+2) %> </TD>
           <TD> <%= resultset.getString(i+3) %> </TD>

       </TR>

   </TABLE>
   <BR>


 <% }  %>
         <tr>
         <TD> <%= resultset.getString(name) %> </TD>
       <TD> <%= resultset.getString(author) %> </TD>
       <TD> <%= resultset.getString(money) %> </TD>
       <TD> <%= resultset.getString(company) %> </TD>
       </tr>

try out with this one it will work i am using the same in my code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM