关于jsp中使用javabean老报空指针异常javbean代码: public class c
关于jsp中使用javabean老报空指针异常javbean代码:publicclassconnectsql{publicstaticStatementgetStatem...
关于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"%>
<jsp:useBean id="dan" class="com.jdbc.connectsql" scope="page"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
reques 展开
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"%>
<jsp:useBean id="dan" class="com.jdbc.connectsql" scope="page"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
reques 展开
3个回答
展开全部
commit
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
学了有半年多了吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-04-23 · 知道合伙人软件行家
关注
展开全部
sqlserver的jar包加载了吗
追问
加载了,我单独测试javabean能成功写入数据,用jsp调用javabean就开始报错了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询