在线求救!!!在线求救!!!在线求救!!!在线求救!!! java.lang.NullPointerException at com.UserMan.userExist(UserMan.java:32)空指针,看一下UserMan.java是什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UserMan.java的32行,哎,帖子不能修改 chubbchubb(长街)你好:你能否说的详细点,我改如何该。谢谢了! 你看一下UserMan.java的32行的代码,有个变量在运行的时候是null你把UserMan.java的代码放上来 chubbchubb(长街) UserMan.java如下:package com;import javax.naming.*;import javax.sql.*;import java.sql.*;import java.util.*;public class UserMan{ Context ctx=null; DataSource ds=null;//构造函数 public UserMan() { //从连接池中获取数据库连接 try{ ctx = new InitialContext(); ds = (DataSource)ctx.lookup("document"); } catch(NamingException e){ e.printStackTrace(); } } //检查指定的用户是否存在 //userId为要检查的用户ID public boolean userExist(String userId){ Connection con = null; PreparedStatement ps = null; ResultSet rs=null; boolean occupied=true; try{ String sqlquery="select * from users where id=?"; con=ds.getConnection(); ps=con.prepareStatement(sqlquery); ps.setString(1,userId); rs=ps.executeQuery(); if(!rs.next()) occupied=false; } catch(SQLException e){ e.printStackTrace(); } finally{ if (rs != null) try {rs.close();} catch (SQLException ignore) {} if (ps != null) try {ps.close();} catch (SQLException ignore) {} if (con != null) try {con.close();} catch (SQLException ignore) {} } return occupied; } //验证用户是否为合法用户 //user为用户ID,pwd为登陆用户提交的密码 public boolean isValidUser(String user,String pwd) { Connection con = null; PreparedStatement ps = null; ResultSet rs=null; boolean isValid=false; try{ String sqlquery="select * from users where id=? and password=?"; con=ds.getConnection(); ps=con.prepareStatement(sqlquery); ps.setString(1,user); ps.setString(2,pwd); rs=ps.executeQuery(); if(rs.next()) isValid=true; } catch(SQLException e){ e.printStackTrace(); } finally{ if (rs != null) try {rs.close();} catch (SQLException ignore) {} if (ps != null) try {ps.close();} catch (SQLException ignore) {} if (con != null) try {con.close();} catch (SQLException ignore) {} } return isValid; }} 你的ds为Null也就是没有找到数据源 UserMan.java的32行是:con=ds.getConnection()么? chubbchubb(长街):32行是: con=ds.getConnection();我该如何做? 那就明白了,你的ds是空的,没有取到啊。ds = (DataSource)ctx.lookup("document");这里没有异常么?你看一下你的datasource的设置,SQLServer我没有用过,不知道怎么设 chubbchubb(长街):谢谢,问题一解决是:数据源出了问题。 键盘响应事件问题 Java将中文转成unicode存入到文本文件 刚开始学java,有个特别简单的问题 if(ch>>>8==0)与if(ch<=255)两句的区别? where does the java download? 一个类在实例化的时候,怎样才能把自己加到别的实例中去? 菜鸟问题! 简单的程序,简单的问题!! jdk1.4在swing中的汉字显示问题(和普通的乱码问题不一样)???????????? jbuilder鬼问题!!!大家进来捉鬼! [求助]新手学java,关于按钮和标签的问题:为什么我的按钮按下没有响应啊??请大家进来看一下 如何用java程序打开一个ie窗口!!
哎,帖子不能修改
你能否说的详细点,我改如何该。谢谢了!
你把UserMan.java的代码放上来
UserMan.java如下:package com;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
public class UserMan
{
Context ctx=null;
DataSource ds=null;
//构造函数
public UserMan()
{
//从连接池中获取数据库连接
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("document");
}
catch(NamingException e){
e.printStackTrace();
} }
//检查指定的用户是否存在
//userId为要检查的用户ID
public boolean userExist(String userId){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean occupied=true;
try{
String sqlquery="select * from users where id=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlquery);
ps.setString(1,userId);
rs=ps.executeQuery();
if(!rs.next())
occupied=false;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return occupied;
} //验证用户是否为合法用户
//user为用户ID,pwd为登陆用户提交的密码
public boolean isValidUser(String user,String pwd)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean isValid=false;
try{
String sqlquery="select * from users where id=? and password=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlquery);
ps.setString(1,user);
ps.setString(2,pwd);
rs=ps.executeQuery();
if(rs.next())
isValid=true;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return isValid;
}
}
也就是没有找到数据源
con=ds.getConnection()么?
32行是: con=ds.getConnection();
我该如何做?
ds = (DataSource)ctx.lookup("document");
这里没有异常么?
你看一下你的datasource的设置,SQLServer我没有用过,不知道怎么设