遇到一个问题,百思不得其解,调试了很久,不知错在哪里??

jiang5495 2010-01-20 10:58:54
有一段小servlet程序如下,
调试时遇到的问题是:为什么我在main函数里面连接查询数据库没问题,也就是说conn!=null,
但在doPost里面连接查询数据时,conn却为null,可以从运时时它跳转到指定的页百得知其为null,
实在想不明白这是为什么??
在两个不同的函数里面conn却会有不同的值??

希望会的朋友帮忙看看,不甚感激!!!!



import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import jiang5495.util.*;

import java.sql.*;

public class login extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String name=request.getParameter("name");
String password=request.getParameter("password");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

conn=DataBase.getConnection();
if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
}
try{

if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp?name=jiang5495");
}
stmt=conn.createStatement();
String sql="select password from student where name='"+password+"'";
rs=stmt.executeQuery(sql);
String npassword="iyuihjknhjkhxuihuihjhl";
while(rs.next()){
npassword=rs.getString(1);
}
if(npassword.equals(password)){
request.getRequestDispatcher("/main.jsp").forward(request, response);
}else{
// request.getRequestDispatcher("/main.jsp").forward(request, response);
response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
}

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request,response);
}

public static void main(String[] args){


Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=DataBase.getConnection();
try{
stmt=conn.createStatement();
String sql2="select * from bkliuhecai";
rs=stmt.executeQuery(sql2);
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception e){
e.printStackTrace();
}
}

}
...全文
359 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
对了你的代码没有任何错误。是环境问题。
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
楼主,确实是你引入的jar的问题。
你可能添加jar的时候,是:工程右键->properties->java build path->Libraries->add jars...的方式,从外面某个目录(不是你的WEB-INF下面的lib)把sqlserver的lib弄进来的。
解决方法如下:
把那个sqlserver的驱动包,放到你的WEB-INF下的lib下,然后,把javabuildpath里面上面说到的那种方式下的jar删了。然后,再右键工程-》refresh,然后再试,肯定可以了。

我刚才这么重现了你的问题,也通过这种方式解决了。不过我刚才那里报了驱动找不到,你这里怎么没报就奇怪了。,
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 jiang5495 的回复:]
引用 21 楼 crazylaa 的回复:
我迷糊了,可是上面说的tomcat执行的时候没有lib包的话,应该会报ClassNotFoundException 的啊。

继续调试中,我也迷糊了!
[/Quote]
我决定开jb测试下连oracle,你的login的jsp可以共享不啊?我懒得写了。。
jiang5495 2010-01-21
  • 打赏
  • 举报
回复
涉及到的主要java程序如下,
我想其它jsp页面应该不会有错吧


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import jiang5495.util.*;

import java.sql.*;

public class login extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String name=request.getParameter("name");
String password=request.getParameter("password");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

conn=DataBase.getConnection();
if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
}
try{

/*
if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp?name=jiang5495");
}
*/
if(conn==null){
System.out.println("天哪,为什么我就是取不到!");
response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
}


stmt=conn.createStatement();
String sql="select password from student where name='"+password+"'";
rs=stmt.executeQuery(sql);
String npassword="iyuihjknhjkhxuihuihjhl";
while(rs.next()){
npassword=rs.getString(1);
}
if(npassword.equals(password)){
request.getRequestDispatcher("/main.jsp").forward(request, response);
}else{
// request.getRequestDispatcher("/main.jsp").forward(request, response);
response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
}

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request,response);
}
/*
public static void main(String[] args){


Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=DataBase.getConnection();
try{
stmt=conn.createStatement();
String sql2="select * from bkliuhecai";
rs=stmt.executeQuery(sql2);
while(rs.next()){
System.out.println(rs.getString(1));
}
}catch(Exception e){
e.printStackTrace();
}
}
*/
}


package jiang5495.util;
import java.sql.*;
public class DataBase {

public static Connection getConnection(){
Connection conn=null;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=temp";
String uName = "sa";
String uPwd = "123456";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,uName,uPwd);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e2){
e2.printStackTrace();
}
if(conn==null){
System.out.println("you who you");
}
return conn;
}
public static void main(String[] args){
Connection conn=DataBase.getConnection();
Statement stmt=null;
ResultSet rs=null;
System.out.println("you are not a good boy!");
try{
stmt=conn.createStatement();
String sql="select * from web";
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}

}catch(Exception e3){
e3.printStackTrace();
}
}

}

jiang5495 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 crazylaa 的回复:]
我迷糊了,可是上面说的tomcat执行的时候没有lib包的话,应该会报ClassNotFoundException 的啊。
[/Quote]
继续调试中,我也迷糊了!
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
我迷糊了,可是上面说的tomcat执行的时候没有lib包的话,应该会报ClassNotFoundException 的啊。
qingzhe2008 2010-01-21
  • 打赏
  • 举报
回复
上面那些代码没看出有什么问题

应该从环境角度检查
qingzhe2008 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 axman 的回复:]
因为main和serlvet使用的classpath不同,driver在servlet环境中没有进入classpath.

[/Quote]

我觉得应该是这个原因,因为我碰过类似的问题

LZ到tomcat里看看对应的包有没有,或者包冲突了
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
catch(SQLException e2){
e2.printStackTrace();
}
->看不出什么问题,这样看看有没有其他异常呢?
catch(SQLException e2){
e2.printStackTrace();
} catch(Exception e3){
e3.printStackTrace();
}
jungle1171 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 crazylaa 的回复:]
楼主,确实是你引入的jar的问题。
你可能添加jar的时候,是:工程右键->properties->java build path->Libraries->add jars...的方式,从外面某个目录(不是你的WEB-INF下面的lib)把sqlserver的lib弄进来的。
解决方法如下:
把那个sqlserver的驱动包,放到你的WEB-INF下的lib下,然后,把javabuildpath里面上面说到的那种方式下的jar删了。然后,再右键工程-》refresh,然后再试,肯定可以了。

我刚才这么重现了你的问题,也通过这种方式解决了。不过我刚才那里报了驱动找不到,你这里怎么没报就奇怪了。,
[/Quote]
没错~
bayougeng 2010-01-21
  • 打赏
  • 举报
回复
初始化connection的代码,你应该写在init方法里。
psyuhen 2010-01-21
  • 打赏
  • 举报
回复
DataBase.getConnection()

不知道LZ这个是怎么写的。。开放代码给大家看看吧。
xlxyeyu 2010-01-21
  • 打赏
  • 举报
回复
谁知道你Connection是怎么实现的?

DataBase.getConnection()是否报错了,即出现ClassNotFoundException异常?或者什么时候自己释放掉了?

如果是出现ClassNotFoundException异常,出现的原因就是你jar包放错了地方!那就需要将sql的jar包放到lib下面,或者放到tomcat/common/lib里
ritchie511 2010-01-21
  • 打赏
  • 举报
回复
引用不对,代码迷糊!
不吃鱼的熊 2010-01-21
  • 打赏
  • 举报
回复
楼主,你这个环境问题,包引用不对!
代码间的舞者 2010-01-21
  • 打赏
  • 举报
回复
application和servlet是不一样的环境的。
1.application运行成功说明程序没问题。那么就是环境的问题了。
2.servlet的话,就要把数据库的驱动包放在tomcat的lib下。

py330316117 2010-01-21
  • 打赏
  • 举报
回复
在两个不同的函数里面conn却会有不同的值??
这是个肯定句,在两个不同的函数里面conn会有不同的值。因为你这个2个conn是不同的内存单元,除非你把conn设置在函数外,用static修饰它就是一个值了。
  if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
}
try{

/*
if(conn==null){
response.sendRedirect("http://localhost:8080/HelleWorld/main.jsp?name=jiang5495");
}
*/
if(conn==null){
System.out.println("天哪,为什么我就是取不到!");
response.sendRedirect("http://localhost:8080/HelleWorld/register.jsp?name=jiang5495");
}

这个地方怎么觉得你的if语句是一个意思,你怎么写了3遍
sunlongfei_001 2010-01-21
  • 打赏
  • 举报
回复
servlet,doPost,和doGet,是接受前台 post和get的请求,并返回结果。
你直接写main函数调用,当然不行。相当于没有调用的。如果你写在其他的函数里,
我想是可以获取连接的。
希望你试试。
_千鸟 2010-01-21
  • 打赏
  • 举报
回复
我之间遇到的连接数据库的错误(以连接SQL Server 2000为例)
1. sql没有升级sql server;
2. 没有导入sql连接的三个jar;
3. 没有创建数据库;
4. 没有设置好classpath;

lz的问题可以试试这样:


建议不要把数据库连接写在dopost()中.
jiang5495 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 crazylaa 的回复:]
这个真的很奇怪噢。。。
楼主能不能看看
DataBase.getConnection();
这个方法的代码啊?
怀疑使用了静态变量,而在别的地方把conn设为null了。。。
[/Quote]
public static Connection getConnection(){
Connection conn=null;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=temp";
String uName = "sa";
String uPwd = "123456";
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,uName,uPwd);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e2){
e2.printStackTrace();
}
if(conn==null){
System.out.println("you who you");
}
return conn;
}
加载更多回复(16)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧