Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 [求助OK]Servlet 调用数据库值出错。
caoyi





发贴: 32
于 2004-09-21 11:18 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我写了一个 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


话题树型展开
人气 标题 作者 字数 发贴时间
1680 [求助OK]Servlet 调用数据库值出错。 caoyi 5399 2004-09-21 11:18

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923