日期:2014-05-16  浏览次数:20532 次

一个查询数据库的servlet例子
//SelectStudent.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SelectStudent extends HttpServlet {
private String url;
private String user;
private String password;

public void init() throws ServletException {
String driverName=this.getInitParameter("driverClass");
url = this.getInitParameter("url");
user = this.getInitParameter("user");
password = this.getInitParameter("password");
try {
Class.forName(driverName);
} catch(ClassNotFoundException ce) {
ce.printStackTrace();
System.out.println("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out=resp.getWriter();
String name = req.getParameter("name");
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection(url,user,password);
String sql = "select * from studentinfo where StudentName = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmt = rs.getMetaData();
int iCount = rsmt.getColumnCount();
out.println("<table border=1px bordercolor=#FF0000>");
out.println("<tr>");
for(int i=1;i<=iCount;i++) {
out.println("<td align=center>");
out.println(rsmt.getColumnLabel(i));
out.println("</td>");
}
out.println("</tr>");
while(rs.next()) {
out.println("<tr>");
for(int i=1;i<=iCount;i++) {
out.println("<td align=center>");
out.println(rs.getString(i));
out.println("</td>");
}
out.println("</tr>");
}
out.println("</table>");
} catch (Exception e){}
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
doGet(req,resp);
}
}

//jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>学生信息查询</title>
</head>
<body>
<form action="select" method="POST">
姓名:<input type="text" name="name" />
<input type="submit" value="查询"/>
</form>
</body>
</html>

//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <servlet>
     <servlet-name>StudentInfo</servlet-name>
     <servlet-class>StudentInfo</servlet-class>
  </servlet>
 
  <servlet-mapping>