package com.neusoft.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.vo.Cyy_User;
public class User_Dao {
//系统普通用户的操作
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//----------------------------------------------------------------------------------
//-----用户登录,首先查询用户名是否存在,再判断密码是否正确------------------------------
//----------------------------------------------------------------------------------
public static Cyy_User selectUser(String user_name,String user_pass) throws SQLException{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Cyy_User where user_name=?";
Cyy_User cu=new Cyy_User();
// System.out.println("++++++传过来的pass+++++"+user_pass);
try {
con=com.neusoft.connect.Connect_Util.connect_util(con);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user_name);
rs = pstmt.executeQuery();
while(rs.next()){
String pass=new String();
pass=rs.getString("user_pass");
// System.out.println("------查找的pass-----"+pass);
// System.out.println(user_pass.equals(pass));
if(!user_pass.equals(pass)){
System.out.println("密码错误!");
}
else { //在menu里已有输出
cu.setUser_auth(rs.getString("user_auth"));
cu.setUser_name(rs.getString("user_name"));
cu.setUser_id(rs.getString("user_id"));
cu.setUser_mail(rs.getString("user_mail"));
cu.setUser_pass(rs.getString("user_pass"));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
com.neusoft.connect.Connect_Util.close(con, pstmt, rs);
}
return cu;
}
//-----------------------------------------------------------------------
//-----用户注册:首先判断用户名是否存在,则提醒用户。----------------------------------
//------------------管理员添加用户-------------------------------------------------
//-----------------------------------------------------------------------
public static int insertUser(String user_name,String user_pass,String user_mail){
int i = 0;
Connection con = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
ResultSet rs=null;
String sql1="select * from Cyy_User where user_name=?";
String sql2 = "insert into Cyy_User (user_auth,user_id,user_name,user_pass,user_mail)values('普通用户',seq_cyy_user.nextval,?,?,?)";
try{
con=com.neusoft.connect.Connect_Util.connect_util(con);
pstmt1 = con.prepareStatement(sql1);
pstmt2 = con.prepareStatement(sql2);
pstmt1.setString(1, user_name);
rs=pstmt1.executeQuery();
if(rs.next()){
// System.out.println(rs.getString("user_name")); //判断
System.out.println("该用户名已存在!");
i=0;
}
else{
i=1;
pstmt2.setString(1, user_name);
pstmt2.setString(2, user_pass);
pstmt2.setString(3, user_mail);
pstmt2.executeUpdate();
}
}catch (Exception e){
e.printStackTrace();
}finally{
com.neusoft.connect.Connect_Util.close(con, pstmt1, rs);
com.neusoft.connect.Connect_Util.close(con, pstmt2);
}
return i;
}
//-------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~ 用户修改自己的信息~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static void alter(String user_name,String user_pass,String user_mail,String user_id){
Connection con = null;
PreparedStatement pstmt = null;
String sql = "update Cyy_User set user_name=?,user_pass=?,user_mail=?where user_id=?";
try {
con=com.neusoft.connect.Connect_Util.connect_util(con);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user_name);
pstmt.setString(2, user_pass);
pstmt.setString(3, user_mail);
pstmt.setString(4, user_id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
com.neusoft.connect.Connect_Util.close(con, pstmt);
}
}
//--------------------------------------------------------------------------
//--------用戶查看自己的信息----------------------------------------------------
//--------------------------------------------------------------------------
public static void searchUser(String user_name){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Cyy_User where user_name=?";
try {
con=com.neusoft.connect.Connect_Util.connect_util(con);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user_name);
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("user_id"));
System.out.println(rs.getString("user_pass"));
System.out.println(rs.getString("user_mail"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
com.neusoft.connect.Connect_Util.close(con, pstmt, rs);
}
}
//--------------------------------------------------------------------------
//---------------------管理员出寻全部用户---------------------------------------
//--------------------------------------------------------------------------
public static List<Cyy_User> selectAll(){
Connection con = null;
Statement stmt=null;
ResultSet rs = null;
List<Cyy_User> list=new ArrayList<Cyy_User>();
String sql="select * from Cyy_User";
try{
con=com.neusoft.connect.Connect_Util.connect_util(con);
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
Cyy_User cu=new Cyy_User();
cu.setUser_auth(rs.getString("user_auth"));
cu.setUser_id(rs.getString("user_id"));
cu.setUser_name(rs.getString("user_name"));
cu.setUser_pass(rs.getString("user_pass"));
cu.setUser_mail(rs.getString("user_mail"));
list.add(cu);
}
}catch (Exception e){
e.printStackTrace();
}finally{
com.neusoft.connect.Connect_Util.close(con, stmt, rs);
}
return list;
}
//--------------------------------------------------------------------------
//---------------------管理员根据ID查询用户---------------------------------------
//--------------------------------------------------------------------------
public static List<Cyy_User> select_id(String user_id){
Connection con = null;
PreparedStatement pstmt=null;
ResultSet rs = null;
List<Cyy_User> list=new ArrayList<Cyy_User>();
String sql="select * from Cyy_User where user_id=?";
try{
con=com.neusoft.connect.Connect_Util.connect_util(con);
pstmt=con.prepareStatement(sql);
pstmt.setString(1, user_id);
rs=pstmt.executeQuery();
while(rs.next()){
Cyy_User cu=new Cyy_User();
cu.setUser_auth(rs.getString("user_auth"));
cu.setUser_id(rs.getString("user_id"));
cu.setUser_name(rs.getString("user_name"));
cu.setUser_pass(rs.getString("user_pass"));
cu.setUser_mail(rs.getString("user_mail"));
list.add(cu);
}
}catch (Exception e){
e.printStackTrace();
}finally{
com.neusoft.connect.Connect_Util.close(con, pstmt, rs);
}
return list;
}
//--------------------------------------------------------------------------
//---------------------管理员根据用户名查询用户---------------------------------------
//--------------------------------------------------------------------------
public static List<Cyy_User> select_name(String user_name){
Connection con = null;
PreparedStatement pstmt=null;
ResultSet rs = null;
List<Cyy_User> list=new ArrayList<Cyy_User>
- 1
- 2
- 3
前往页