当前位置:首页 > 科技 >

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

发布时间:2017-09-04 12:09:37

[JSP]JDBC 用户登陆,查看修改添加用户 先使用JavaBean,封装JDBC数据库的操作。

[java]

package com.yyqf.toolbean;

/*

* 数据库操作类

* 查询数据库,专题系统,获取所有投票选项

* 获取指定IP上一次进行投票的时间

* 更新数据表,实现票数累加

* */

import java.io.IOException;

import java.io.InputStream;

import java.sql.*;

import java.util.ArrayList;

import java.util.Properties;

import com.yyqf.valuebean.User;

public class DB

{

private String DRIVER;

private String URL;

private String USERNAME;

private String PASSWORD;

private Connection conn;

private Statement stmt;

private PreparedStatement prstmt;

private ResultSet rs;

public DB() throws IOException

{

Properties p = new Properties();

InputStream in = getClass().getResourceAsStream("/Config.properties");

p.load(in);

DRIVER = p.getProperty("DRIVER");

URL = p.getProperty("URL");

USERNAME = p.getProperty("USERNAME");

PASSWORD = p.getProperty("PASSWORD");

}

public void getConnection()

{

try {

Class.forName(DRIVER);

} catch (Exception e) {

System.out.println("驱动加载失败");

e.printStackTrace();

}

try {

conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);

} catch (Exception e) {

System.out.println("连接数据库失败");

e.printStackTrace();

}

}

public void getstmt()

{

getConnection();

try {

stmt = conn.createStatement();

stmt.execute("USE User_pwd");

} catch (Exception e) {

System.out.println("获取statment对象失败");

e.printStackTrace();

}

}

public ResultSet getRs(String sql)

{

ResultSet resultSet = null;

getstmt();

try {

rs = stmt.executeQuery(sql);

resultSet = rs;

} catch (Exception e) {

System.out.println("获取resultset失败");

e.printStackTrace();

}

return resultSet;

}

public ResultSet getPrRs(String sql)

{

ResultSet resultSet = null;

try {

getstmt();

prstmt = conn.prepareStatement(sql);

} catch (Exception e) {

System.out.println("获取Prepare statment失败");

e.printStackTrace();

}

try {

rs = prstmt.executeQuery();

resultSet = rs;

} catch (Exception e) {

System.out.println("获取Prepare resultset失败");

e.printStackTrace();

}

return resultSet;

}

public void closed()

{

try {

if (rs!=null) rs.close();

if (stmt!=null) stmt.close();

if (prstmt!=null) prstmt.close();

if (conn!=null) conn.close();

} catch (Exception e) {

System.out.println("关闭数据库失败");

e.printStackTrace();

}

}

public ArrayList<User> SelectAllUser(String sql)

{

ArrayList<User> userlist = null;

if (sql!=null){

getRs(sql);

if (rs!=null){

userlist = new ArrayList<User>();

try {

while (rs.next()){

User temp = new User();

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 (Exception e) {

System.out.println("封装应用信息失败 Accout_user表");

e.printStackTrace();

}finally{

closed();

}

}

}

return userlist;

}

public int update(String sql) //更新数据表

{

int i = -1;

if (sql!=null && !sql.equals(""))

{

getstmt();

try {

i = stmt.executeUpdate(sql);

} catch (Exception e) {

System.out.println("更新数据库失败");

e.printStackTrace();

}finally{

closed();

}

}

return i;

}

<span style="font-size:32px">}

</span>

JSP常用工具封装

[java]

package com.yyqf.toolbean;

/*

* JSP操作工具类*/

import java.sql.Date;

import java.text.SimpleDateFormat;

public class MyTools {

public static String intToStr(int num) //把int转换为字符

{

return String.valueOf(num);

}

public static int strToint(String str) //把字符转换为int

{

if (str==null || str.equals("")) str="0";

int i=0;

try {

i=Integer.parseInt(str);

} catch (Exception e) {

i=0;

e.printStackTrace();

}

return i;

}

public static boolean compareTime(long today,long temp) //比较上次和当前投票时间,参数都是毫秒级时间

{

int limitTime=60; //设置限制时间60分钟

long count=today-temp;

if (count<=limitTime*60*1000) return false;

else return true;

}

public static String formatDate(long ms) //转换时间为指定格式

{

Date date = new Date(ms);

SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");

String strDate = format.format(date);

return strDate;

}

public static String toChinese(String str) //解决乱码问题,转换为中文

{

if (str==null) str="";

try {

str=new String(str.getBytes("ISO-8859-1"),"gb2312");

} catch (Exception e) {

str="";

e.printStackTrace();

}

return str;

}

public static String change(String str) //转换网页表单提交数据

{

str=str.replace("<","<");

str=str.replace(">",">");

return str;

}

}

用户信息ValueBean

[java]

package com.yyqf.valuebean;

public class User {

private String username;

private String password;

private String power;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getPower() {

return power;

}

public void setPower(String power) {

this.power = power;

}

}

ajax操作的JS封装

[javascript]

var net=new Object(); //定义一个全局变量net

net.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=new XMLHttpRequest();

} else if (window.ActiveXObject){

this.req=new ActiveXObject("Microsoft.XMLHTTP");

}

if (this.req){

try{

var loader=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函数

var req=this.req;

var ready=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 fetching data!"

+"\n\nreadyState:"+this.req.readyState

+"\nstatus: "+this.req.status

+"\nheaders: "+this.req.getAllResponseHeaders());

}

登陆页面

[html]

<%@ page language="java" contentType="text/html; charset=gb2312"

pageEncoding="gb2312"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>登陆</title>

</head>

<body onload="setFocus()">

<b><font size="10">逸雨清风系统登陆</font></b><hr/>

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网络推广 http://www.feimao666.com

上一篇:linux教程之一
下一篇:最后一页