test

package com.struts.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.struts.util.db.DBManager;
import com.struts.vo.User;

public class UserDAO {
 public User Login(String username,String password) throws Exception{
  User user = new User();
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs =null;
  String sql = "select * from users where username=? and password=?";
  conn = DBManager.getConnection();
  try {
   ps = conn.prepareStatement(sql);
   ps.setString(1, username);
   ps.setString(2, password);
   rs = ps.executeQuery();
   rs.next();
   user.setId(rs.getInt("id"));
   user.setUsername(rs.getString("username"));
   user.setPower(rs.getString("power"));
  } catch (SQLException e) {
   //e.printStackTrace();
   throw new Exception("用户不存在或用户名密码错误!登录失败!");
  }finally{
   DBManager.closeAll(rs, ps, conn);
  }
  return user;
 }
}

你可能感兴趣的:(java,DAO,sql,struts)