caoyi
发贴: 32
|
于 2004-09-21 11:18
我写了一个 servlet的程序希望能够调用数据库中的数据显示到网页上面。 希望各位能够帮忙。谢谢。 web输入值为http://localhost:8080/ch07/init-servlet servlet放在webapps\ch07\WEB-INF\classes\com\jspdev\ch07 web.xml在webapps\ch07\WEB-INF 下面是出错信息 java.lang.NullPointerException com.jspdev.ch07.JDBCServlet.doGet(JDBCServlet.java:26) javax.servlet.http.HttpServlet.service(HttpServlet.java:743) javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
下面是servlet的程序和web.xml的配置.
JDBCServlet.java
package com.jspdev.ch07; import java.sql.*; import javax.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import com.mysql.jdbc.Driver;
public class JDBCServlet extends HttpServlet { String driver; String url; String password; String user;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); try { Connection con=getConnection(); Statement stmt=con.createStatement(); ResultSet rst=stmt.executeQuery("select * from user_info"); while(rst.next()) { out.println(rst.getString("userID")); out.println("<br>"); } rst.close(); stmt.close(); con.close(); } catch(SQLException e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException { doGet(request,response); }
public void init()throws ServletException { driver=getInitParameter("DRIVER"); url=getInitParameter("URL"); user=getInitParameter("USER"); password=getInitParameter("PASSWORD"); } private Connection getConnection() { Connection con=null; try { Class.forName(driver); con = DriverManager.getConnection(url,user,password); } catch(Exception e) { e.printStackTrace(); } return con; } }
web.xml <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmls:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" version="2.4"> <display-name>Welcome to Tomcat</display-name> <description> JSP application development </description> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>com.jspdev.ch07.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>JDBCServlet</servlet-name> <servlet-class>com.jspdev.ch07.JDBCServlet</servlet-class> <init-param> <param-name>DRIVER</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;DatabaseName=BookDB</param-value> <param-value>jdbc:mysql://localhost:3306/BookDB?</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>JDBCServlet</servlet-name> <url-pattern>/init-servlet</url-pattern> </servlet-mapping> </web-app>
CREATE TABLE user_info(userID NVARCHAR(40),password nvarchar(40), name nvarchar(40)); INSERT INTO user_info VALUES('tsaoyi', 'a01', 'gujunchao')
caoyi edited on 2004-09-21 11:45
|