关于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)
传入的参数有问题
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。