查询数据库的实例(eclipse+tomcat+mysql *web.xml)

环境:
Eclipse3.2 rc6
Tomcat 5.5
TomcatPluginV31
MySql5.0

一,新建一个tomcat项目.
项目名称:DataBase

二,新建一个index.jsp文件到项目根目录下.

<%@ page language="java" contentType="text/html; charset=gb2312"
   pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>数据查询</title>
</head>
<body>
  <form action="GetData" method="GET" name="form1" target="_blank"> 
    <p>  &nbsp;名称: <input type="text" name="text1" size="10" maxlength="10">  &nbsp;&nbsp;<input type="Submit" name="button1" value="查询">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p>&nbsp;
 </p></form>
   </body>
</html>

三,在WEB-INF/src 下面创建Servlet类:GetData.java

包:com.database
超类:HttpServlet

重载init()和doGet()方法.

package com.database;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetData extends HttpServlet {

 private static final long serialVersionUID = 1L;
 String driver,DataUrl,Username,Pw,ErrorStr;
 Connection conn=null;
 int ierror=0;

 public void init() throws ServletException {
  // TODO Auto-generated method stub

  ServletContext sc=getServletContext();
  
   driver=sc.getInitParameter("Driver_type");
   DataUrl=sc.getInitParameter("url");
   Username=sc.getInitParameter("UName");
   Pw=sc.getInitParameter("Pw");
  try{
  Class.forName(driver);
  conn=DriverManager.getConnection(DataUrl, Username, Pw);
  
  }
  catch(Exception e){
  ierror=1;
  ErrorStr=e.toString();
  }
 }


 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
  // TODO Auto-generated method stub
  
  arg0.setCharacterEncoding("gb2312");
  arg1.setContentType("text/html;charset=gb2312");

  PrintWriter out = arg1.getWriter();
  // 如果是用GET方式提交,再获取字符串时要进行内码转换.如果是post则不用.
  String Key = new String(arg0.getParameter("text1").getBytes("iso8859-1"), "gb2312");

  if (ierror == 1) {
   out.print("数据库发生错误:" + "<br>" + ErrorStr);

  }
  out.println("<%@ page language=/"java/" contentType=/"text/html; charset=gb2312/" %>");
  try {
   Statement sm = conn.createStatement();
   String isql = "select * from table1 where name='" + Key + "'";
   ResultSet rs = sm.executeQuery(isql);
   ResultSetMetaData rm = rs.getMetaData();
   int icount = rm.getColumnCount();
   rs.last();
   int rr = rs.getRow();
   out.println("<br>查询记录数:");
   out.println(rr);
   out.println("<br>");
   out.println("<table border='0' width='200px'>");
   out.println("<tr>");
   for (int i = 1; i <= icount; i++) {
    // 打印表的字段名
    out.println("<td>");
    out.println(rm.getColumnLabel(i));
    out.println("</td>");
   }
   out.println("</tr>");

   rs.beforeFirst();
   out.println("<tr>");
   while (rs.next()) {
    for (int i=1;i<=icount;i++){
     out.println("<td>");
     out.println(rs.getString(i));
     out.println("</td>");
    }
   }

   out.println("</tr>");
   out.println("</Table>");
  }

  catch (Exception e) {
   out.println("执行出错!");
  }
 }
}

四,在WEB-INF下创建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

 <context-param>
  <param-name>Driver_type</param-name>
  <param-value>com.mysql.jdbc.Driver</param-value>
 </context-param>

 <context-param>
  <param-name>url</param-name>
  <param-value>jdbc:mysql://localhost/vllia</param-value>
 </context-param>

 <context-param>
  <param-name>UName</param-name>
  <param-value>root</param-value>
 </context-param>

 <context-param>
  <param-name>Pw</param-name>
  <param-value>abcde</param-value>
 </context-param>

 <servlet>
  <servlet-name>GetD</servlet-name>
  <servlet-class>com.database.GetData</servlet-class>

 </servlet>

 <servlet-mapping>
  <servlet-name>GetD</servlet-name>
  <url-pattern>/GetData</url-pattern>
 </servlet-mapping>

 </web-app>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值