JSP文件(1)<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html><body bgcolor = cyan><font size = 2><form action = "mhq" method = post>查询bookdb数据库stu表:<br>输入ID:<input type = text name = "user" value = "book"><br>输入密码:<input type = text name = "password" value = "book123"><br><input type = submit value = "提交"><input type = reset value = "重置"></form></font></body></html>JSP文件(2)<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><jsp:useBean id="mhq" type = "mybean.Hellomhq" scope = "request"/><%@ page import = "java.sql.*"%><html><body bgcolor=cyan><table border = 1><% String []columnName = mhq.getColumnName(); %><tr><%for(String s:columnName){ %><th><%= s%></th> <% }%></tr><% String [][]record = mhq.getTableRecord();for(int i = 0;i<record.length;i++){ %> <tr> <% for(int j = 0;j<record[i].length;j++){ %> <td><%= record[i][j]%></td> <% } %></tr><% }%></table></body></html>bean 文件:package mybean ;public class Hellomhq{ String []columnName; String [][]tableRecord = null; public Hellomhq(){ tableRecord = new String[20][20]; columnName = new String[20]; } public void setTableRecord(String [][]s){ tableRecord = s; } public String [][]getTableRecord(){ return tableRecord; } public void setColumnName(String []s){ columnName = s; } public String []getColumnName(){ return columnName; } }servlet文件:package myservlet.mmm;import mybean.Hellomhq;import java.sql.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class HHH extends HttpServlet { public void init(ServletConfig config) throws ServletException{ super.init(config); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Hellomhq mhq = null; try{ mhq = (Hellomhq)request.getAttribute("mhq"); if(mhq==null){ mhq = new Hellomhq(); request.setAttribute("mhq", mhq); } } catch(Exception e){ mhq = new Hellomhq(); request.setAttribute("mhq", mhq); } try{ Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e){}; String user = request.getParameter("user"); String password = request.getParameter("password"); Connection con; Statement sql; ResultSet rs; try{ String uri = "jdbc:mysql://10.8.131.13/bookdb"; con = DriverManager.getConnection(uri,user,password); sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); String condition = "select * from stu "; rs = sql.executeQuery(condition); ResultSetMetaData metadata = rs.getMetaData(); int columncount = metadata.getColumnCount(); String []columnName = new String[columncount]; for(int i = 0;i<columnName.length;i++){ columnName[i] = metadata.getColumnName(i+1); } mhq.setColumnName(columnName); rs.last(); int rowNumber = rs.getRow(); String [][]tableRecord = mhq.getTableRecord(); tableRecord = new String[rowNumber][columncount]; rs.beforeFirst(); int i = 0; while(rs.next()){ for(int k = 0;k<columncount;k++){ tableRecord[i][k] = rs.getString(k+1); i++; } } mhq.setTableRecord(tableRecord); con.close(); RequestDispatcher aa = request.getRequestDispatcher("mhq1.jsp"); aa.forward(request, response); } catch(Exception ee){ System.out.println(ee); } }}web.xml文件:<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>mhq</servlet-name> <servlet-class>myservlet.mmm.HHH</servlet-class> </servlet> <servlet-mapping> <servlet-name>mhq</servlet-name> <url-pattern>/mhq</url-pattern> </servlet-mapping></web-app>
添加回答
举报
0/150
提交
取消