[JSP]JDBC 用户登陆,查看修改添加用户

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

先使用JavaBean,封装JDBC数据库的操作。[java]packagecom.yyqf.toolbean;/**数据库操作类*查询数据库,获取所有投票选项*获取指定IP上一次进行投票的时间*更新数据表,实现票数累加**/importjava.io.IOException;importjava.io.InputStream;importjava.sql.*;importjava.util.ArrayList;importjava.util.Properties;importcom.yyqf.valuebean.User;publicclassDB{privateStringDRIVER;privateStringURL;privateStringUSERNAME;privateStringPASSWORD;privateConnectionconn;privateStatementstmt;privatePreparedStatementprstmt;privateResultSetrs;publicDB()throwsIOException{Propertiesp=newProperties();InputStreamin=getClass().getResourceAsStream(/Config.properties);p.load(in);DRIVER=p.getProperty(DRIVER);URL=p.getProperty(URL);USERNAME=p.getProperty(USERNAME);PASSWORD=p.getProperty(PASSWORD);}publicvoidgetConnection(){try{Class.forName(DRIVER);}catch(Exceptione){System.out.println(驱动加载失败);e.printStackTrace();}try{conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);}catch(Exceptione){System.out.println(连接数据库失败);e.printStackTrace();}}publicvoidgetstmt(){getConnection();try{stmt=conn.createStatement();stmt.execute(USEUser_pwd);}catch(Exceptione){System.out.println(获取statment对象失败);e.printStackTrace();}}publicResultSetgetRs(Stringsql){ResultSetresultSet=null;getstmt();try{rs=stmt.executeQuery(sql);resultSet=rs;}catch(Exceptione){System.out.println(获取resultset失败);e.printStackTrace();}returnresultSet;}publicResultSetgetPrRs(Stringsql){ResultSetresultSet=null;try{getstmt();prstmt=conn.prepareStatement(sql);}catch(Exceptione){System.out.println(获取Preparestatment失败);e.printStackTrace();}try{rs=prstmt.executeQuery();resultSet=rs;}catch(Exceptione){System.out.println(获取Prepareresultset失败);e.printStackTrace();}returnresultSet;}publicvoidclosed(){try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(prstmt!=null)prstmt.close();if(conn!=null)conn.close();}catch(Exceptione){System.out.println(关闭数据库失败);e.printStackTrace();}}publicArrayListUserSelectAllUser(Stringsql){ArrayListUseruserlist=null;if(sql!=null){getRs(sql);if(rs!=null){userlist=newArrayListUser();try{while(rs.next()){Usertemp=newUser();temp.setUsername(MyTools.toChinese(MyTools.change(rs.getString(username))));temp.setPassword(MyTools.toChinese(MyTools.change(rs.getString(password))));temp.setPower(MyTools.toChinese(MyTools.change(rs.getString(power))));userlist.add(temp);}}catch(Exceptione){System.out.println(封装应用信息失败Accout_user表);e.printStackTrace();}finally{closed();}}}returnuserlist;}publicintupdate(Stringsql)//更新数据表{inti=-1;if(sql!=null&&!sql.equals()){getstmt();try{i=stmt.executeUpdate(sql);}catch(Exceptione){System.out.println(更新数据库失败);e.printStackTrace();}finally{closed();}}returni;}spanstyle=font-size:32px}/spanJSP常用工具封装[java]packagecom.yyqf.toolbean;/**JSP操作工具类*/importjava.sql.Date;importjava.text.SimpleDateFormat;publicclassMyTools{publicstaticStringintToStr(intnum)//把int转换为字符{returnString.valueOf(num);}publicstaticintstrToint(Stringstr)//把字符转换为int{if(str==null||str.equals())str=0;inti=0;try{i=Integer.parseInt(str);}catch(Exceptione){i=0;e.printStackTrace();}returni;}publicstaticbooleancompareTime(longtoday,longtemp)//比较上次和当前投票时间,参数都是毫秒级时间{intlimitTime=60;//设置限制时间60分钟longcount=today-temp;if(count=limitTime*60*1000)returnfalse;elsereturntrue;}publicstaticStringformatDate(longms)//转换时间为指定格式{Datedate=newDate(ms);SimpleDateFormatformat=newSimpleDateFormat(yyyy-mm-ddHH:mm:ss);StringstrDate=format.format(date);returnstrDate;}publicstaticStringtoChinese(Stringstr)//解决乱码问题,转换为中文{if(str==null)str=;try{str=newString(str.getBytes(ISO-8859-1),gb2312);}catch(Exceptione){str=;e.printStackTrace();}returnstr;}publicstaticStringchange(Stringstr)//转换网页表单提交数据{str=str.replace(,);str=str.replace(,);returnstr;}}用户信息ValueBean[java]packagecom.yyqf.valuebean;publicclassUser{privateStringusername;privateStringpassword;privateStringpower;publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetPower(){returnpower;}publicvoidsetPower(Stringpower){this.power=power;}}ajax操作的JS封装[javascript]varnet=newObject();//定义一个全局变量netnet.AjaxRequest=function(url,onload,onerror,method,params){//创建一个构造函数this.req=null;this.onload=onload;this.onerror=(onerror)?onerror:this.defaultError;this.loadDate(url,method,params);}net.AjaxRequest.prototype.loadDate=function(url,method,params){if(!method){method=GET;}if(window.XMLHttpRequest){this.req=newXMLHttpRequest();}elseif(window.ActiveXObject){this.req=newActiveXObject(Microsoft.XMLHTTP);}if(this.req){try{varloader=this;this.req.onreadystatechange=function(){net.AjaxRequest.onReadyState.call(loader);}this.req.open(method,url,true);//this.req.send(params);this.req.send(null);}catch(err){this.onerror.call(this);}}}net.AjaxRequest.onReadyState=function(){//重构onReadyState函数varreq=this.req;varready=req.readyState;if(ready==4){if(req.status==200){this.onload.call(this);}else{this.onerror.call(this);}}}net.AjaxRequest.prototype.defaultError=function(){//默认的错误处理函数alert(error

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功