标题:在servlet中调用访问数据库的bean返回空值
只看楼主
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
 问题点数:0 回复次数:10 
在servlet中调用访问数据库的bean返回空值

这是我的servlet
import Bean.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import Bean.Customer;

public class ViewServlet extends HttpServlet
{
private Bean.Customer customer;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
String ID = "xuefeng";
customer = Bean.Customer.getRecord(ID);
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
if(customer == null)
out.print("sdfsdf");
out.print("<h2>The information of the user:<h2><br>");
out.print("</body>");
out.print("</html>");
}
}
在与它同级的Bean(自己创建)文件夹里有Customer.class
但是上述代码返回的customer 为空
可是test.java
import Bean.*;
import java.sql.*;

public class test
{
public static void main(String[] args){
try
{
String ID = "xuefeng";
Customer customer = Customer.getRecord(ID);
System.out.println(customer.getFirstname() + " ");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
运行正常,是servlet需要额外配置什么东西么???
请大家赐教

搜索更多相关主题的帖子: bean servlet import 数据库 Bean 
2006-09-12 21:56
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
if(customer == null)
out.print("sdfsdf");
out.print("<h2>The information of the user:<h2><br>");
out.print("</body>");
out.print("</html>");

光凭这些代码,我想知道你是怎样判断你的customer是空的

淘宝杜琨
2006-09-12 23:03
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
得分:0 
if(customer == null)
out.print("sdfsdf");
它输出了sdfsdf
而且如果强行输出所读取得值,会报出
java.lang.NullPointerException
ViewServlet.doGet(ViewServlet.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

彪悍的人生不需要解释~~~
2006-09-13 08:55
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
哦,对,是我不小心没有看清楚,对不起

淘宝杜琨
2006-09-13 09:50
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
介意我看一下 Bean.Customer 这个类么?

淘宝杜琨
2006-09-13 09:51
z108979979
Rank: 1
等 级:新手上路
威 望:1
帖 子:252
专家分:0
注 册:2006-6-19
得分:0 
customer = Bean.Customer.getRecord(ID);

这句话什么意思 为什么get方法带着参数啊


2006-09-13 10:54
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
得分:0 
web.xml配置了么?

win32汇编
病毒 加密
目前兴趣所在
2006-09-13 11:36
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
得分:0 

package Bean;
import java.sql.*;

public class Customer
{
private int CustomerId;
private String Firstname;
private String Lastname;
private String Address1;
private String Address2;
private String Tel;

public Customer()
{
CustomerId = 0;
Firstname = "";
Lastname = "";
Address1 = "";
Address2 = "";
Tel = "";
}

public Customer(int CustomerId, String Firstname, String Lastname, String Address1, String Address2, String Tel)
{
this.CustomerId = CustomerId;
this.Firstname = Firstname;
this.Lastname = Lastname;
this.Address1 = Address1;
this.Address2 = Address2;
this.Tel = Tel;
}

public static Customer getRecord( String Firstname )
{
try
{
Conn con = new Conn();
ResultSet rs = con.getRs("SELECT * FROM Customer where Firstname=\'" + Firstname + "\'");
if(rs.next())
{
Customer customer = new Customer(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
return customer;
}
}
catch(Exception e)
{
}
return null;
}

public String getFirstname()
{
return this.Firstname;
}

public String getLastname()
{
return this.Lastname;
}

public String getAddress1()
{
return this.Address1;
}

public String getAddress2()
{
return this.Address2;
}

public String getTel()
{
return this.Tel;
}
/*
public static void main(String[] args)
{
try
{
String ID = "xuefeng";
Customer customer = Customer.getRecord(ID);
System.out.println(customer.getFirstname() + " ");
System.out.println(customer.getLastname() + " ");
System.out.println(customer.getAddress1() + " ");
System.out.println(customer.getAddress2() + " ");
System.out.println(customer.getTel() + " ");
System.out.println(customer.getFirstname() + " ");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
*/
}


彪悍的人生不需要解释~~~
2006-09-13 13:07
roy_guo
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-4-27
得分:0 

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<!-- Define servlets that are included in the example application -->

<servlet>
<servlet-name>ViewServlet</servlet-name>
<servlet-class>ViewServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ViewServlet</servlet-name>
<url-pattern>/view</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>Shopping</web-resource-name>
<url-pattern>/shop</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
</login-config>

<security-role>
<role-name>manager</role-name>
</security-role>
</web-app>
这个是xml,是不是还需要加点什么?


彪悍的人生不需要解释~~~
2006-09-13 13:08
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
好象挺棘手的呢


淘宝杜琨
2006-09-13 13:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-89782-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.082775 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved