例子2:顺序查询数据库中的表数据(JSP)

本文展示了一个使用JSP进行数据库查询的示例,包括从用户接收输入、处理请求并展示查询结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="inquire"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <FORM action="inquire.jsp" Method="post"> 
    <br>输入数据库名:<input type=text name="dataBaseName" size=8>
    <br/>
    输入表的名字:<input type=text name="tableName" size=8>
    <br>输入用户名:<input type=text name="user" size=8>
    <br>输入密码:<input type="password" name="password" size=8>
    <br/>
    <br/><input type=submit name="g" value="提交">
    </FORM>
  </body>
</html>

inquire.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">   
    <title>My JSP 'inquire.jsp' starting page</title>  
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head> 
  <body> 
   <% String database=request.getParameter("dataBaseName");
   String tName=request.getParameter("tableName");
   String id=request.getParameter("user");
   String secret=request.getParameter("password");
    %>
<inquire:QueryTag dataBaseName="<%=database%>"
tableName="<%=tName%>"
user="<%=id%>"
password="<%=secret %>"/>
<br>
    在<%=biao%>表查询到记录:
    <br>
    <br><%=queryResult %>
  </body>
</html>

QueryTag.tag

<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="dataBaseName" required="true" %>
<%@ attribute name="tableName" required="true" %> 
<%@ attribute name="user" required="true" %>  
<%@ attribute name="password" required="false" %>  
<%@ variable name-given="biao" scope="AT_END" %>
<%@ variable name-given="queryResult" scope="AT_END" %>
<%  StringBuffer result;     
    result=new StringBuffer();
    try{  Class.forName("com.mysql.jdbc.Driver");
    }
    catch(Exception e){} 
    Connection con;
    Statement sql; 
    ResultSet rs;
    try{  result.append("<table border=1>"); 
          String uri="jdbc:mysql://127.0.0.1/"+dataBaseName;
          con=DriverManager.getConnection(uri,user,password);
          DatabaseMetaData metadata=con.getMetaData();
          ResultSet rs1=metadata.getColumns(null,null,tableName,null);
          int 字段个数=0;
          result.append("<tr>");
          while(rs1.next()){
             字段个数++;
             String clumnName=rs1.getString(4);
             result.append("<td>"+clumnName+"</td>");
          }
          result.append("</tr>");
          sql=con.createStatement();
          rs=sql.executeQuery("SELECT * FROM "+tableName);
          while(rs.next()){
               result.append("<tr>");
               for(int k=1;k<=字段个数;k++) 
                  result.append("<td>"+rs.getString(k)+"</td>");
               result.append("</tr>");
          }
          result.append("</table>");
          con.close();
    }
    catch(SQLException e){
          result.append("请输入正确的用户名和密码");
}
//返回对象queryResult: 
    jspContext.setAttribute("queryResult",new String(result)); 
    jspContext.setAttribute("biao",tableName);          //返回biao对象
%> 

项目目录结构显示图:

运行效果显示图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值