javascript 我如何使用jsp从数据库中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13528557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i retrieve data from the database using jsp
提问by Ezhil
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
我想从我的数据库中检索信息。我使用 html 从用户那里动态获取输入,并通过 jsp 我从数据库(Mysql)中获取信息。以下是 jsp 代码
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 i.e the first one and it leaves the other.Where should i modify in this code so that it will retrieve both the data
我使用作者作为关键字来检索数据。现在我的数据库中有 2 个同名作者,但上面的代码只获取一个作者信息,即第一个,它留下另一个。我应该在哪里修改此代码它将检索两个数据
采纳答案by Shahrukh A.
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>
<% } %>
回答by user3465058
<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
试试这个它会起作用我在我的代码中使用相同的