登录函数“loginCheck”中的没有异常,但为什么在“insertUser”函数中从
“con=dataSource.getConnection();”开始报空指针异常。一下是我的代码:public boolean loginCheck(String username,String password)
{
ResultSet rs=null;
PreparedStatement ps=null;
boolean bool=false;
String sql="select * from users where username=? and password=?";
Connection con=null;
try
{
con=dataSource.getConnection();
ps=con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs=ps.executeQuery();
if(rs.next())
{
bool=true;
}
return bool;
}
catch(Exception ex)
{
return false;
}
finally
{
try
{
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
if(con!=null)
con.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public int insertUser(String username,String password,String age,String sex,String email)
{
int intRow=-1;
Connection con=null;
PreparedStatement ps=null;
String sql="insert into users values(?,?,?,?,?)";
try
{
con=dataSource.getConnection();
ps=con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ps.setString(3,age);
ps.setString(4,sex);
ps.setString(5,email);
intRow=ps.executeUpdate();
return intRow;
}
catch(Exception ex)
{
ex.printStackTrace();
return 0;
}
finally
{
try{
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
“con=dataSource.getConnection();”开始报空指针异常。一下是我的代码:public boolean loginCheck(String username,String password)
{
ResultSet rs=null;
PreparedStatement ps=null;
boolean bool=false;
String sql="select * from users where username=? and password=?";
Connection con=null;
try
{
con=dataSource.getConnection();
ps=con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs=ps.executeQuery();
if(rs.next())
{
bool=true;
}
return bool;
}
catch(Exception ex)
{
return false;
}
finally
{
try
{
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
if(con!=null)
con.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public int insertUser(String username,String password,String age,String sex,String email)
{
int intRow=-1;
Connection con=null;
PreparedStatement ps=null;
String sql="insert into users values(?,?,?,?,?)";
try
{
con=dataSource.getConnection();
ps=con.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ps.setString(3,age);
ps.setString(4,sex);
ps.setString(5,email);
intRow=ps.executeUpdate();
return intRow;
}
catch(Exception ex)
{
ex.printStackTrace();
return 0;
}
finally
{
try{
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
ps.setString(2,password);
ps.setString(3,age);
ps.setString(4,sex);
ps.setString(5,email);
-------------------------------------------------------------------
怎么回事,实现定义好了String username =""了么?如果是这样,那在这个方法之前,至少也要写上,this.username=username啊,不然怎么会有值呢
public CommonBean()
{}
public CommonBean(DataSource ds)
{
dataSource=ds;
}
这变量与两个函数在同一个类里