通过读取web.xml文件中的配置参数连接数据库(原)

时间:2022-03-15 13:56:05

通过读取web.xml文件中的配置参数连接数据库

1.问题提出
-javax.servlet.ServletException: Servlet.init() for servlet CreateDBServlet threw exception

这个错误是在看孙鑫老师的那本web编程详解,在第八章遇见的,就是第一个例子

但总是运行不成功。仔细观察发现自己犯了两个错误。

(1)CreateDBServlet.java代码

package org.sunxin.lesson.jsp.bookstore;

 

import javax.servlet.*;

import java.io.*;

import javax.servlet.http.*;

import java.sql.*;

 

public class CreateDBServlet extends HttpServlet

{

      private String url;

      private String user;

      private String password;

    

      public void init() throws ServletException

      {

          String driverClass=getInitParameter("driverClass");

          url=getInitParameter("url");

          user=getInitParameter("user");

          password=getInitParameter("password");

          try

          {

              Class.forName(driverClass);

          }

          catch(ClassNotFoundException ce)

          {

              throw new UnavailableException("加载数据库驱动失败!");

          }

      }

    

      public void doGet(HttpServletRequest req, HttpServletResponse resp)

                 throws ServletException,IOException

      {

          Connection conn=null;

          Statement stmt=null;

          try

          {

              conn=DriverManager.getConnection(url,user,password);

              stmt=conn.createStatement();

            stmt.executeUpdate("create database bookstore");

              stmt.executeUpdate("use bookstore");

              stmt.executeUpdate("create table bookinfo(id INT not null primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish_date DATE not null,price FLOAT(4,2) not null,amount SMALLINT,remark VARCHAR(200)) ENGINE=InnoDB");

              stmt.addBatch("insert into bookinfo values(1,'Java从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");

              stmt.addBatch("insert into bookinfo values(2,'JSP深入编程','李四','李四出版社','2004-10-1',56.00,20,null)");

              stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");

              stmt.executeBatch();

            

              PrintWriter out=resp.getWriter();

              out.println("success!");

              out.close();

          }

          catch(SQLException se)

          {

              se.printStackTrace();

          }

          finally

          {

              if(stmt!=null)

              {

                  try

                  {

                      stmt.close();

                  }

                  catch(SQLException se)

                  {

                      se.printStackTrace();

                  }

                  stmt=null;

              }

              if(conn!=null)

              {

                  try

                  {

                      conn.close();

                  }

                  catch(SQLException se)

                  {

                      se.printStackTrace();

                  }

                  conn=null;

              }

          }

      }

}

(2)没有修改的web.xml

<?xml version="1.0" encoding="gb2312"?>

 

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

      version="2.4">

    

      <context-param>

             <param-name>driverClass</param-name>

             <param-value>com.mysql.jdbc.Driver</param-value>

      </context-param>

      <context-param>

             <param-name>url</param-name>

             <param-value>jdbc:mysql://localhost:3306/bookstore</param-value>

      </context-param>

      <context-param>

             <param-name>user</param-name>

             <param-value>root</param-value>

      </context-param>

      <context-param>

             <param-name>password</param-name>

             <param-value>12345678</param-value>

      </context-param>

 

      <servlet>

          <servlet-name>CreateDBServlet</servlet-name>

          <servlet-class>org.sunxin.lesson.jsp.bookstore.CreateDBServlet</servlet-class>

      </servlet>

    

      <servlet-mapping>

          <servlet-name>CreateDBServlet</servlet-name>

          <url-pattern>/createdb</url-pattern>

</servlet-mapping>

 
2.问题解决
(1)stmt.executeUpdate("create database bookstore");

将这句代码注释掉,因为还没有创建数据库,那用conn=DriverManager.getConnection(url,user,password);连接数据库时,就会出问题,而且你要自己手动先create一个数据库,(create database bookstore;)这是mysql的命令就不多说了。

(2)编辑web.xml如下(注意修改你的数据库密码)

      <servlet>

              <servlet-name>CreateDBServlet</servlet-name>

              <servlet-class>org.sunxin.lesson.jsp.bookstore.CreateDBServlet</servlet-class>

               

             <init-param>

                    <param-name>driverClass</param-name>

                    <param-value>com.mysql.jdbc.Driver</param-value>

                </init-param>

                <init-param>

                    <param-name>url</param-name>

                    <param-value>jdbc:mysql://localhost:3306/bookstore</param-value>

                </init-param>

                <init-param>

                    <param-name>user</param-name>

                    <param-value>root</param-value>

                 </init-param>

                 <init-param>

                    <param-name>password</param-name>

                    <param-value>1234</param-value>

                 </init-param>

       

      </servlet>

    

      <servlet-mapping>

          <servlet-name>CreateDBServlet</servlet-name>

          <url-pattern>/createdb</url-pattern>

      </servlet-mapping>

然后把这个配置文件放到你的应用程序的WEB-INF目录下,注意WEB-INF目录一定要在你的程序的目录下,且为第一层目录。

这里还有一个问题就是,如我们没有手动创建自己的数据库,服务器并不给出错误提示,但也不会得到正确结果,浏览器只显示空白页,不知道为什么,如果谁知道这个问题的解决办法,可以通知我,我会非常的感谢,可以给我留言,也可以qq联系我。

我是用的tomcat6+jdk1.5+mysql5调试成功的。

Qq:124273269