注:该servlet链接的是mySql数据库,数据库名为test_db 在运行前需要加载mySql数据库驱动


DBServlet servlet文件代码如下::

package com;
import java.io.*;                                    //导入java.io包
 import java.util.*;
 import java.sql.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 public class DBServlet extends HttpServlet{            //定义一个继承HttpServlet的公有类
     ServletConfig config=null;                        //定义一个ServletConfig对象
     private String driverName="";                    //定义私有字符串常量并初始化
     private String username="";                    //定义的数据库用户名
     private String password="";                    //定义的数据库连接密码
     private String dbName="";                        //定义的数据库名
     private Connection conn;                        //初始化连接
     private Statement stmt;                        //初始化数据库操作
     ResultSet rs=null;                            //初始化结果集
     public void init(ServletConfig config)throws ServletException{
      super.init(config);                            //继承父类的init()方法
      this.config=config;                            //获取配置信息
      driverName=config.getInitParameter("driverName");//从配置文件中获取JDBC驱动名
      username=config.getInitParameter("username");    //获取数据库用户名
       password=config.getInitParameter("password");    //获取数据库连接密码
       dbName=config.getInitParameter("dbName");    //获取要连接的数据库
         }
     public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{
        
         resp.setContentType("text/html;charset=GBK");    //设置字符编码格式
         PrintWriter out=resp.getWriter();                //实例化对象,用于页面输出
           out.println("<html>");                    //实现生成静态Html
           out.println("<head>");
           out.println("<meta http-equiv=\"Content-Type\"content=\"text/html;charset=GBK\">");
           out.println("<title>DataBase Connection</title>");
           out.println("</head>");
           out.println("<body bgcolor=\"white\">");
           out.println("<center>");
           String url="jdbc:mysql://127.0.0.1:3307/test_db";
           try{
                 Class.forName("com.mysql.jdbc.Driver");
                 conn=DriverManager.getConnection(url,username,password);
                 stmt=conn.createStatement();
                 String sql="select * from t_food_order";
                 rs=stmt.executeQuery(sql);
                   out.println("Servlet访问数据库成功");
                   out.println("<table border=1 bordercolorlight=#000000>");
                   out.println("<tr><td width=40>序号</td>");
                   out.println("<td>书名</td>");
                   out.println("<td>城市</td>");
                   //out.println("<td>州</td>");
                   //out.println("<td>国</td></tr>");
                    while(rs.next()){
                     out.println("<tr><td>"+rs.getString(1)+"</td>");
                     out.println("<td>"+rs.getString(2)+"</td>");
                     out.println("<td>"+rs.getString(3)+"</td>");
                     //out.println("<td>"+rs.getString(4)+"</td>");
                     //out.println("<td>"+rs.getString(5)+"</td>");
                     out.println("<tr>");
                    }
                    out.println("</table>");
                    rs.close();
                    stmt.close();
                    conn.close();    
                   
               }catch(Exception e){
               e.printStackTrace();
               out.println(e.toString());    
               }
               out.println("</center>");
           out.println("</body>");
           out.println("</html>");
            }
            public void doPost(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{
                this.doGet(req,resp);
                }
                public void destory(){
                    config=null;
                    driverName=null;
                    username=null;
                    password=null;
                    dbName=null;
                    conn=null;
                    stmt=null;
                    rs=null;
                    }
 }




web.xml文件如下::

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DBServlet</servlet-name>
    <servlet-class>com.DBServlet</servlet-class>
    <init-param>
            <param-name>driverName</param-name>
            <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
        <init-param>
        <param-name>username</param-name>            
            <param-value>root</param-value>                
        </init-param>
        <init-param>
            <param-name>password</param-name>        
            <param-value>root</param-value>                
        </init-param>
        <init-param>
            <param-name>dbName</param-name>    
            <param-value>test_db</param-value>    
        </init-param>

  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Ka</servlet-name>
    <servlet-class>com.Ka</servlet-class>
  </servlet>


 <servlet-mapping>
     <servlet-name>DBServlet</servlet-name>
     <url-pattern>/DBServlet</url-pattern>
   </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Ka</servlet-name>
    <url-pattern>/Ka</url-pattern>
  </servlet-mapping>

</web-app>

转自:http://www.cnblogs.com/penseur/archive/2012/05/15/2502120.html
posted on 2013-06-05 17:34  swxj  阅读(23402)  评论(1编辑  收藏  举报