package login;

import common.DBTool;

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

public class UserDB {
    public static UserInfo GetUserInfo(int id) {
        UserInfo u = new UserInfo();
        ResultSet rs = null;
        try{
            String sql = "select * from t_user where N_USER_ID = ?";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if(rs.next()) {
                u.setId(rs.getInt("N_USER_ID"));
                u.setName(rs.getString("VC_LOGIN_NAME"));
                u.setPass(rs.getString("VC_PASSWORD"));
            }else{
                u = null;
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return u;
    }
    public static UserInfo GetUserInfoByName(String username) {
        UserInfo u = new UserInfo();
        ResultSet rs = null;
        try{
            String sql = "select * from t_user where VC_LOGIN_NAME = ?";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if(rs.next()) {
                u.setId(rs.getInt("N_USER_ID"));
                u.setName(rs.getString("VC_LOGIN_NAME"));
                u.setPass(rs.getString("VC_PASSWORD"));
            }else{
                u = null;
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return u;
    }
}
