⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdao.java

📁 用struts,spring实现网上商店的功能!!
💻 JAVA
字号:
package user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import shopping.util.*;

public class UserDAO implements IUserDAO {

	private final Log log = LogFactory.getLog(UserDAO.class);
	private static final String SELECT_BYNAME_STR = "SELECT * FROM user ";
	private static final String INSERT_STR = " INSERT INTO " + " user"
			+ " (username,password,phone,addr)" + " VALUES " + "(?,?,?,?)";
	private static final String CHECK_STR = "SELECT * FROM user where username =? and password=?";

	/*
	 * 用户注册
	 */
	public String saveUser(User user) {
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String userId = "";
		String info = "";
		try {
			con = DAOUtil.getDBConnection(this);
			st = con.prepareStatement(this.INSERT_STR);
			int i = 1;
			st.setString(i++, user.getUsername());
			st.setString(i++, user.getPassword());
			st.setString(i++, user.getPhone());
			st.setString(i++, user.getAddr());

			st.executeUpdate();
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
			info = "用户新增保存成功!";

		} catch (Exception e) {
			try {
				con.rollback();
				con.setAutoCommit(true);
			} catch (Exception ee) {

			}
			log.error("SQL:" + this.INSERT_STR + "执行错误--->", e);
			e.printStackTrace();
			info = "用户新增保存失败,请确认!";
		} finally {
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
			DAOUtil.closeConnection(con, this);
		}
		return info;
	}

	public String printUser(User user) {

		System.out.println(user.getUsername());
		return null;

	}

	/*
	 * 用户登录
	 */
	public String canLogin(String username, String password) {
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String userId = "";
		String info = "";
		boolean result = true;
		try {
			con = DAOUtil.getDBConnection(this);
			System.out.println("sql:" + this.CHECK_STR);
			st = con.prepareStatement(this.CHECK_STR);
			st.setString(1, username);
			st.setString(2, password);
			st.executeUpdate();
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
		} catch (Exception e) {
			try {
				con.rollback();
				con.setAutoCommit(true);
			} catch (Exception ee) {

			}
			log.error("SQL:" + this.INSERT_STR + "执行错误--->", e);
			e.printStackTrace();

		} finally {
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
			DAOUtil.closeConnection(con, this);
		}
		return null;
	}

	public String getUserByLoginName(String loginName) throws Exception {
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String user = "";

		try {
			con = DAOUtil.getDBConnection(this);
			System.out.println("sql:" + this.CHECK_STR);
			st = con.prepareStatement(this.CHECK_STR);
			st.setString(1, loginName);
			rs = st.executeQuery();
			if (rs.next()) {

			}

			st.executeUpdate();
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
		} catch (Exception e) {
			try {
				con.rollback();
				con.setAutoCommit(true);
			} catch (Exception ee) {

			}
			log.error("SQL:" + this.CHECK_STR + "执行错误--->", e);
			e.printStackTrace();

		} finally {
			DAOUtil.closeResultSet(rs, this);
			DAOUtil.closeStatement(st, this);
			DAOUtil.closeConnection(con, this);
		}
		return null;
	}

	public static User check(String username, String password)
			throws UserNotFoundException, PasswordNotCorrectException {
		User u = null;
		Connection conn = DB.getConn();
		String sql = "select * from user where username = '" + username + "'";
		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			if (!rs.next()) {
				throw new UserNotFoundException("用户不存在:" + username);
			} else {
				if (!password.equals(rs.getString("password"))) {
					throw new PasswordNotCorrectException("密码不正确哦!");
				}
				u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setPhone(rs.getString("phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return u;
	}

	public String checkUser(String username, String password) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -