Java 使用JSP在数据库中搜索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24406047/
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
Searching data in database using JSP
提问by Shakir Ali
What is the problem with the following code? I want to search data in the database using the following JSP code.
以下代码有什么问题?我想使用以下 JSP 代码在数据库中搜索数据。
search.jsp
搜索.jsp
<%@ page import="java.sql.*" %>
<html>
<form>
<table>
<%
String value=request.getParameter("id");
int v=Integer.parseInt(value);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee ","root", "root");
Statement st=conn.createStatement();
ResultSet rs = st.executeQuery("select * from test where id="+v+"");
if(rs.next()){
%>
<tr><td>Name: </td><td<input type="text" value="<%=rs.getString("name")%>" > </td></tr>
<tr><td>Address: </td><td<input type="text" value="<%=rs.getString("address")%>" > </td></tr>
<tr><td>Contact No: </td><td<input type="text" value="<%=rs.getInt("contactNo")%>" > </td></tr>
<tr><td>Email: </td><td<input type="text" value="<%=rs.getString("email")%>" > </td></tr>
<%
}
%>
</table>
</form>
</html>
采纳答案by SparkOn
change the select query your query is wrong try:
更改选择查询您的查询是错误的尝试:
"select * from test where id='"+v+"'"
One simple example :
一个简单的例子:
<%
String value=request.getParameter("student_id");;
int v=Integer.parseInt(value);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "welcome");
Statement st=conn.createStatement();
ResultSet rs = st.executeQuery("select * from student where student_id='"+v+"'");
if(rs.next()){
%>
<tr><td>Name: </td><td><input type="text" value="<%=rs.getString("student_name")%>"/> </td></tr>
<%
}
%>
回答by Braj
I never suggest you to use Scripletin JSP instead use JavaServer Pages Standard Tag Librarythat is more easy to use and less error prone.
我从不建议您在 JSP 中使用Scriplet,而是使用更易于使用且不易出错的JavaServer Pages 标准标记库。
You can achieve it using JSTL SQL Tag Library.
您可以使用JSTL SQL 标记库来实现它。
Use JSTL Core c:forEach, c:forTokens Tagthat is s a good alternative to embedding a Java for
, while
, or do-while loop
via a Scriptlet.
使用JSTL核心C:的forEach,C:forTokens标签就是一个很好的替代嵌入一个Java for
,while
或do-while loop
通过一个scriptlet。
Sample code: (change it as per your need)
示例代码:(根据您的需要更改)
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:query dataSource="${dataSource}"
sql="select name,address,contactNo,email from test where id=?" var="result">
<sql:param value="${param.id}"></sql:param>
</sql:query>
<table border="1">
<c:forEach var="row" items="${result.rows}">
<tr>
<td><input type="text" value="${row.name}"/></td>
<td><input type="text" value="${row.address}"/></td>
<td><input type="text" value="${row.contactNo}"/></td>
<td><input type="text" value="${row.email}"/></td>
</tr>
</c:forEach>
</table>
Find complete sample code here on JSTL SQL Tag.
在JSTL SQL Tag上找到完整的示例代码。
回答by shylesh krishna
1-->Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root", "root");
1-->Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root", "root");
in your code there is a space after tha database name
在您的代码中,数据库名称后面有一个空格
2--> Name: " > Address: " > Contact No: " > Email: " >
2--> 姓名:》 > 地址:》 > 联络号码:》 > 电邮:》 >
check your Name:
检查您的姓名:
i think while loop is better than if in this case
我认为 while 循环比 if 在这种情况下更好