当前位置: 技术问答>java相关
如何在servlet中调用javaBean中的方法和出现java.lang.NullPointerException如何调试
来源: 互联网 发布时间:2015-09-16
本文导语: 有这样的一个Bean: package inittest; import java.sql.*; public class initBean { static final String Driver = "sun.jdbc.odbc.JdbcOdbcDriver"; static final String URL = "jdbc:odbc:wangliang"; private Connection con; private Statement sqlStatem...
有这样的一个Bean:
package inittest;
import java.sql.*;
public class initBean {
static final String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
static final String URL = "jdbc:odbc:wangliang";
private Connection con;
private Statement sqlStatement;
private ResultSet rs;
private int num;
public initBean() {
}
//建立与数据库的连接
public boolean connect(){
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
return false;
}
return true;
}
//用于对数据库进行查询等操作
public ResultSet opens(String sql)throws java.sql.SQLException{
ResultSet rs = sqlStatement.executeQuery(sql);
return(rs);
}
//向数据库提交一个SQL语句,执行一般操作,返回一个布尔值
public boolean executeSql(String sql){
try{
sqlStatement.execute(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return true;
}
//向数据库提交一个SQL语句,执行更新,返回一个整数
public int executeUpdate(String sql){
try{
int num = sqlStatement.executeUpdate(sql);
}catch(SQLException exx){
System.err.println(exx.getMessage());
}
return num;
}
//检查输入是否正确(用javaScript来检查)这里不用
public boolean checklogin(){
return true;
}
//关闭与数据库的连接
public void close(){
try{
con.close();
sqlStatement = null;
con = null;
}catch(SQLException exxx){
}
}
//end
}
有这样的一个servlet:
package inittest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import inittest.initBean;
public class saveservlet extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//得到输入的数据
String aa = request.getParameter("s1");
String bb = request.getParameter("s2");
String cc = request.getParameter("s3");
String dd = request.getParameter("s4");
String ee = request.getParameter("s5");
String ff = request.getParameter("s6");
String gg = request.getParameter("s7");
String hh = request.getParameter("s8");
String ii = request.getParameter("s9");
String jj = request.getParameter("s10");
String kk = request.getParameter("s11");
String ll = request.getParameter("s12");
//??????????????????????????????????????????????????????????????????????????????
//向数据库提交一个SQL语句
String sqlinsert = "INSERT INTO biao1("+"编号,"+"活动编号,"+"名称,"+"目标,"+"合作公司,"+"活动主管,"+"实际增长,"+"影响指标,"+"发起原因,"+"执行人数,"+"备注,"+"说明)"+
"values("+aa+","+bb+","+cc+","+dd+","+ee+","+ff+","+gg+","+hh+","+ii+","+jj+","+kk+","+ll+")";
//????这里如何调用Bean中的方法向数据库提交SQL语句???????????
try{
sqlStatement.execute(sqlinsert);
}catch(SQLException e){
out.println(e.getMessage());
}
//上面的在JBUDIER7中不对????????????????????????????????????????????????
out.println("");
out.println("saveservlet");
out.println("");
out.println("
out.println(aa);
out.println(bb);
out.println(cc);
out.println(dd);
out.println(ee);
out.println(ff);
out.println(gg);
out.println(hh);
out.println(ii);
out.println(jj);
out.println(kk);
out.println(ll);
out.println("");
//关闭连接
try{
con.close();
}catch(Exception e){
;
}
}
//Clean up resources
public void destroy() {
}
}
package inittest;
import java.sql.*;
public class initBean {
static final String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
static final String URL = "jdbc:odbc:wangliang";
private Connection con;
private Statement sqlStatement;
private ResultSet rs;
private int num;
public initBean() {
}
//建立与数据库的连接
public boolean connect(){
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
return false;
}
return true;
}
//用于对数据库进行查询等操作
public ResultSet opens(String sql)throws java.sql.SQLException{
ResultSet rs = sqlStatement.executeQuery(sql);
return(rs);
}
//向数据库提交一个SQL语句,执行一般操作,返回一个布尔值
public boolean executeSql(String sql){
try{
sqlStatement.execute(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return true;
}
//向数据库提交一个SQL语句,执行更新,返回一个整数
public int executeUpdate(String sql){
try{
int num = sqlStatement.executeUpdate(sql);
}catch(SQLException exx){
System.err.println(exx.getMessage());
}
return num;
}
//检查输入是否正确(用javaScript来检查)这里不用
public boolean checklogin(){
return true;
}
//关闭与数据库的连接
public void close(){
try{
con.close();
sqlStatement = null;
con = null;
}catch(SQLException exxx){
}
}
//end
}
有这样的一个servlet:
package inittest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import inittest.initBean;
public class saveservlet extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//得到输入的数据
String aa = request.getParameter("s1");
String bb = request.getParameter("s2");
String cc = request.getParameter("s3");
String dd = request.getParameter("s4");
String ee = request.getParameter("s5");
String ff = request.getParameter("s6");
String gg = request.getParameter("s7");
String hh = request.getParameter("s8");
String ii = request.getParameter("s9");
String jj = request.getParameter("s10");
String kk = request.getParameter("s11");
String ll = request.getParameter("s12");
//??????????????????????????????????????????????????????????????????????????????
//向数据库提交一个SQL语句
String sqlinsert = "INSERT INTO biao1("+"编号,"+"活动编号,"+"名称,"+"目标,"+"合作公司,"+"活动主管,"+"实际增长,"+"影响指标,"+"发起原因,"+"执行人数,"+"备注,"+"说明)"+
"values("+aa+","+bb+","+cc+","+dd+","+ee+","+ff+","+gg+","+hh+","+ii+","+jj+","+kk+","+ll+")";
//????这里如何调用Bean中的方法向数据库提交SQL语句???????????
try{
sqlStatement.execute(sqlinsert);
}catch(SQLException e){
out.println(e.getMessage());
}
//上面的在JBUDIER7中不对????????????????????????????????????????????????
out.println("");
out.println("saveservlet");
out.println("");
out.println("
恭喜!保存成功!保存的数据如下:The servlet has received a POST. This is the reply.
");out.println(aa);
out.println(bb);
out.println(cc);
out.println(dd);
out.println(ee);
out.println(ff);
out.println(gg);
out.println(hh);
out.println(ii);
out.println(jj);
out.println(kk);
out.println(ll);
out.println("");
//关闭连接
try{
con.close();
}catch(Exception e){
;
}
}
//Clean up resources
public void destroy() {
}
}
|
例1:
public boolean connect() throws Exception{
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
throw e;
}
finally{
return true;
}
}
或者抛出一个自定义错误,更友好的提示信息:
public boolean connect() throws Exception{
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
throw new Exception("连接数据库失败");
}
finally{
return true;
}
}
public boolean connect() throws Exception{
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
throw e;
}
finally{
return true;
}
}
或者抛出一个自定义错误,更友好的提示信息:
public boolean connect() throws Exception{
try{
try{
Class.forName(Driver);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
con = DriverManager.getConnection(URL);
sqlStatement = con.createStatement();
}catch(Exception e){
throw new Exception("连接数据库失败");
}
finally{
return true;
}
}
|
对于你的那个catch可以换成如下:
catch(SQLException e)
{
out.print("SQL error"+e);
}
catch(NullPointerException e)
{
out.print("NULL Pointer Error");
}
catch(SQLException e)
{
out.print("SQL error"+e);
}
catch(NullPointerException e)
{
out.print("NULL Pointer Error");
}