日期:2014-05-16  浏览次数:20341 次

Struts2+JQuery+Json登陆实例

在搭建之前.. 首先,需要准备struts2.0框架的5个核心包, 以及jsonplugin-0.32.jar

以及json-lib-2.2.2-jdk15.jar ,用来转换JSON对象的.,ojdbc14.jar oracle驱动包. jquery-1.2.6.js文件。

?

?

开始建立WebProject ,取名为Struts2AjaxDemo

把包分别复制到WEB-INF/lib包下面。

?

建立struts.xml文件 和 web.xml文件。 这个功能,还对数据库进行了操作..数据库代码,贴出来见表的sql语句:

建立表的SQL语句如下:

?

?

?

后台DAO连接Oracle数据库代码:

package pack.java.basedao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import pack.java.vo.User;

public class BaseConnection {
	private static final String USERNAME ="scott";
	private static final String PASSWORD ="tiger";
	private static final String DRIVER ="oracle.jdbc.driver.OracleDriver";
	private static final String URL  = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	private Connection connection = null;
	private PreparedStatement preparedStatement= null;
	private ResultSet resultSet = null;
	
	
	private Connection getConnection(){
		try {
			Class.forName(DRIVER);
			try {
				 connection =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
				 connection.setAutoCommit(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("连接失败!");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("驱动没有找到!");
		}
		return connection;
	}
	
	private PreparedStatement getPreparedStatement(String sql){
		getConnection();
		try {
			preparedStatement =  connection.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return preparedStatement;
	}
	
	public User getUserByName(String name){
		User user = null;
		String sql = "select userId,username,password,faicount,to_char(lastlogintime,'yyyy-MM-dd hh:mi:ss')tdate from user_info U where U.username = '"+name+"'";
		try {
			resultSet = getPreparedStatement(sql).executeQuery();
			while (resultSet.next()) {
				user = new User();
				user.setId(resultSet.getInt("USERID"));
				user.setName(resultSet.getString("USERNAME"));
				user.setPassword(resultSet.getString("PASSWORD"));
				user.setFaiCount(resultSet.getInt("FAICOUNT"));
				try {
					user.setLastLoginDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(resultSet.getString("tdate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection();
		}
		return user;
	}
	
	public Date getDBCurrentTime(){
		String sql = "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')as tdate from dual";
		Date date = null;
		try {
			resultSet = getPreparedStatement(sql).executeQuery();
			try {
				while (resultSet.next()) {
					date =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(resultSet.getString("tdate"));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection();
		}
		return date;
	}
	
	public boolean updateFailureCountToUserInfo(int totalNumber,String username){
		String sql = "update User_Info set  FAICOUNT="+totalNumber +" where User_INFO.username =  '"+username+"'";
		
		System.out.println(sql);
		try {
			int i = getPreparedStatement(sql).executeUpdate();
			connection.commit();
			
			return i>0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			closeConnection();
		}
	}
	
	public boolean updateLastLoginTimeToUserInfo(Date lastLogindate,String username){
		Str