JSP+Servlet+JDBC实现登陆功能 谁能帮我看下哪错了
publicstaticvoidmain(String[]args)throwsSQLException{ UserDaodao=newUserDao(); Listls=newArrayList(); ls=dao.query(); for(inti=0;i |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|
publicclassUserDao{ publicListquery()throwsSQLException{ Connectionconn=DBUtil.getConnection(); PreparedStatementptmt=null; ResultSetrs=null; Stringsql="select*fromuser"; ptmt=conn.prepareStatement(sql); rs=ptmt.executeQuery(); Useruser=null; Listlist=newArrayList(); while(rs.next()){ user=newUser(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); list.add(user); } rs.close(); ptmt.close(); conn.close(); returnlist; } }这是报错的那个代码 19行是ptmt = conn.prepareStatement(sql); |
|
|
|
|
目测是因为DBUtil里的getConnection()方法返回的connection是空.
将try{}catch(){}代码放到getConnection()中试下,别用static{}代码块了. |
|
|
|
|
publicclassDBUtil{ privatestaticfinalStringUSER="root"; privatestaticfinalStringPASSWORD="mj199609"; privatestaticfinalStringURL="jdbc:mysql://localhost:3306/reg"; privatestaticConnectionconn=null; static{ try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(URL,USER,PASSWORD); }catch(ClassNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } publicstaticConnectiongetConnection(){ returnconn; }} |
|
|
|
|