开发者社区> 问答> 正文

jsp+javabean数据库操作报错?报错

关于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)

展开
收起
爱吃鱼的程序员 2020-06-09 15:38:31 403 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    传入的参数有问题

    2020-06-09 15:38:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载