最好从while之后用System.out.print跟踪,看看取到数据库中的数据没有很可能是没有取到数据而抛出空指针异常
解决方案 »
- 在线求高手帮助,SSH整合报错问题
- Struts2开发的网站,给点建议 !
- JDBC驱动
- 救命!我用ajaxanywhere怎么出现这种错误!!!
- 谁有jsp+sql server 2000做的一个简易的留言板啊?
- 弱弱滴问:JSP语言实现商务网站(主要是办公信息自动化方面的)的各种工具的选择问题。
- 抛弃ibatis,抛弃hibernate,像这样访问数据库
- 奇!jsp两次运行结果不一样!
- 如何去掉myeclipse 9 会把img标签的src属性自动修改成onerror的属性值?
- 大神们,求助,怎么看学习一个WEB项目!
- 请问struts中如何给<html:text>加style?
- 初学JavaBeans,100分帮我解释这些数据库链接代码,每行都需要有解读,谢谢!
{
boolean Loggedin=true;
try{
connect();
//con.setAutoCommit(false);
Statement s = con.createStatement();System.out.println("creat");
//creat不能被输出
ResultSet rs = s.executeQuery("SELECT * FROM UserDB");System.out.println("select");
while(rs.next())
{
String pn = rs.getString(2);System.out.println(pn);
String pw = rs.getString(2);System.out.println(pw);
if(((user.getPhoneNumber()).equals(pn))&&((user.getPassword()).equals(pw)))
{user.setIsLoggedin(Loggedin);return;}
}
user.setIsLoggedin(!Loggedin);
rs.close();
s.close();
//con.commit();
disconnect();
}catch(SQLException cnfe){
error="SQLException:Could not connect to database.";
throw new SQLException(error);
}catch(Exception e){
error="Exception:An unkown error occurred while connecting to database.";
throw new Exception(error);
}
}CONNECT函数:
public void connect() throws ClassNotFoundException,SQLException,Exception //connect to database
{ System.out.println("connect");
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=UserDB","sa",""); }catch(ClassNotFoundException cnfe){
error="ClassNotFoundException:Could not locate DB driver.";
throw new ClassNotFoundException(error);
}catch(SQLException cnfe){
error="SQLException:Could not connect to database.";
throw new SQLException(error);
}catch(Exception e){
error="Exception:An unkown error occurred while connecting to database.";
throw new Exception(error);
} }
大家请帮我看看这个BEAN,在CONNECT里面完成数据库的连接,我在con.setAutoCommit(false);和Statement s = con.createStatement();之间插了一句输出语句,发现不能输出,但是当注释掉con.setAutoCommit(false);以后就可以输出,然后在Statement s = con.createStatement()之后加了一句输出语句,发现也不能输出,非常地不明白,希望高手指教
你再用这句话的时候要了解它的意义呀!在数据库第一次建立于数据库连接的时候,在缺省的情况下,连接是自动提交模式的,为了获得更好的性能,可以通过调用代布尔值false参数的Connection类的setAutoCommit()方法来关闭自动提交的功能,但是一旦关闭了自动提交功能,我们就必须通过调用Connection类的commitv()和rollback()方法来人工的方式对事务管理。所以在用这个的时候必须注意!
的注释,那个"creat"为什么还是不能被输出
如果我在TRY函数块里一开始将TARGET赋值,比如赋WELCOME.JSP就正常地连过去
这就说明在我那个SERVLET中经过几轮判断之后,其实TARGET没有被赋值,但是我想不通的就是如果什么条件都不满足的话,至少得被赋给ERROR.JSP吧
希望大家擦亮眼睛告诉我问题所在!谢谢拉~~~~~~~~~~
感谢kevinliuu和Arias