问题描述
运行一下显示的是空指针异常附上代码control.javapackagejavaBean;importjava.io.InputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.Hashtable;importjava.util.Properties;importjava.util.Vector;publicclasscontrol{privateConnectionconn=null;privateResultSetrs=null;privatePreparedStatementprestmt=null;privateVector<String[]>vec=newVector<String[]>();privateStringdriverName="org.gjt.mm.mysql.Driver";privateStringurl="jdbc:mysql://localhost:3306/ymy";booleanflag1=false;booleanflag2=false;booleanflag3=false;booleanflag4=false;privateStringuser="root";privateStringpassword="root";publiccontrol(){}publicvoidsetDriverName(StringnewDriverName){driverName=newDriverName;}publicStringgetDriverName(){returndriverName;}publicvoidsetUrl(StringnewUrl){url=newUrl;}publicStringgetUrl(){returnurl;}publicvoidsetUser(StringnewUser){user=newUser;}publicStringgetUser(){returnuser;}publicvoidsetPassword(StringnewPassword){password=newPassword;}publicStringgetPassword(){returnpassword;}publicConnectiongetConnection(){try{Class.forName(driverName);returnDriverManager.getConnection(url,user,password);}catch(Exceptione){e.printStackTrace();returnnull;}}/***函数功能:删除数据**@paramtableName*表名*@paramcondition*条件例:deletefrom表wherecondition*/publicvoidsetDeleteData(StringtableName,Stringcondition){Stringsql="";if(condition==null||condition==""){sql="deletefrom"+tableName;}else{sql="deletefrom"+tableName+"where"+condition;}try{prestmt=conn.prepareStatement(sql);intrscount=prestmt.executeUpdate();if(rscount>0){flag1=true;}}catch(SQLExceptione){e.printStackTrace();}}publicbooleanisDeleteData(){returnflag1;}/***函数功能:从表中取出符合条件的数据**@paramtableName*表名*@paramfield*列名*@paramcondition*查询条件*@return一个向量集合,每个向量含String[]field*/publicvoidsetGetData(StringtableName,String[]field,Stringcondition){Vector<String[]>vec=newVector<String[]>();StringstrField="",sql="";for(inti=0;i<field.length;i++){strField+=field[i]+",";}strField=strField.substring(0,strField.lastIndexOf(","));if(condition==null||condition==""){sql="select"+strField+"from"+tableName;}else{sql="select"+strField+"from"+tableName+"where"+condition;}try{prestmt=conn.prepareStatement(sql);rs=prestmt.executeQuery();while(rs.next()){String[]temp=newString[field.length];for(inti=0;i<field.length;i++){temp[i]=rs.getString(field[i]);}vec.add(temp);}}catch(SQLExceptione){e.printStackTrace();}}publicVector<String[]>getGetData(){returnvec;}/***函数功能:用户登陆检查*@paramtable*@paramcondition*@return*/publicvoidsetCheckedLogin(Stringtable,Stringcondition){try{Stringsql="select*from"+table+"where"+condition;prestmt=conn.prepareStatement(sql);rs=prestmt.executeQuery();if(rs.next()){flag2=true;}}catch(SQLExceptione){e.printStackTrace();}}publicbooleanisCheckedLogin(){returnflag2;}/***函数功能说明:插入数据**@paramtableName*@paramfield*@paramvalue*@throwsSQLException*第二个参数和第三个参数个数要相等,若为空或"",则表示该表全部字段*/publicvoidsetInsertData(StringtableName,String[]field,String[]value){if(field==null||value==null||field.length==0||value.length==0||field.length!=value.length)flag3=false;StringstrField="",strValue="";for(inti=0;i<field.length;i++){strField+=field[i]+",";strValue+="'"+value[i]+"',";}strField=strField.substring(0,strField.lastIndexOf(","));strValue=strValue.substring(0,strValue.lastIndexOf(","));try{Stringsql="insertinto"+tableName;sql+="("+strField+")values(";sql+=strValue+")";prestmt=conn.prepareStatement(sql);intrscount=prestmt.executeUpdate();if(rscount>0)flag3=true;}catch(SQLExceptione){e.printStackTrace();}}publicbooleanisInsertData(){returnflag3;}/***函数功能:修改数据**@paramtableName*表名*@paramfield*字段列表*@paramvalue*值列表*@paramcondition*条件例:update表set字段1=值1where条件*/publicvoidsetModifyData(StringtableName,String[]field,String[]value,Stringcondition){if(field==null||value==null||field.length==0||value.length==0||field.length!=value.length)flag4=false;Stringstr="";for(inti=0;i<field.length;i++){str+=field[i]+"='"+value[i]+"',";}str=str.substring(0,str.lastIndexOf(","));//Debug(str);Stringsql="";if(condition==null||condition==""){sql="update"+tableName+"set"+str;}else{sql="update"+tableName+"set"+str+"where"+condition;}//Debug(sql);try{prestmt=conn.prepareStatement(sql);intrscount=prestmt.executeUpdate();if(rscount>0){flag4=true;}}catch(SQLExceptione){e.printStackTrace();}}publicbooleanisModifyData(){returnflag4;}}test.jsp<%@pagelanguage="java"contentType="text/html;charset=utf-8"pageEncoding="utf-8"import="java.io.*,java.sql.*,java.util.*"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"><title>Inserttitlehere</title></head><body><jsp:useBeanid="delete"class="javaBean.control"></jsp:useBean><%StringstudentNumber4=request.getParameter("studentNumber4");delete.getConnection();delete.setDeleteData("student","");if(delete.isDeleteData())out.print("删除成功");else{out.print("删除失败");}%></body>
解决方案
解决方案二:
哪一行报错,贴出来