jsp+javabean数据库操作报错

淡淡爱ing 发布于 2016/04/23 10:35
阅读 141
收藏 0
关于jsp中使用javabean老报空指针异常javbean代码: public class connectsql { public static Statement getStatement(){ Connection conn=null; Statement stmt=null; String url="jdbc:sqlserver://localhost:1444;DataBaseName=photouser"; String username="sa"; String password="123456"; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); try { conn=DriverManager.getConnection(url,username,password); stmt=conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return stmt; } public void adduser(String username,String password){ try { String sql="insert into yao(username,password) values('"+username+"','"+password+"')"; getStatement().executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } jsp代码: <%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%> <% request.setCharacterEncoding("gb2312"); String username=request.getParameter("username"); String password=request.getParameter("password"); dan.adduser("23213","2312412"); %> 报错: 严重: Servlet.service() for servlet [jsp] in context with path [/test] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.jdbc.connectsql.adduser(connectsql.java:43) at org.apache.jsp.Judge_jsp._jspService(Judge_jsp.java:84)
加载中
0
xpbob
xpbob
传入的参数有问题
返回顶部
顶部