Buenos dias
escribo para solicitar ayuda urgente con pèrmisos de usuario
estoy trabajando con Netbeans 6.1 y Microsoft SQLSERVER2000
bien en la Base de Datos tengo una tabla usuario con los campos
login,
clave,
tipo_usuario
Donde los tipos de Usuario Pueden ser:
A = "Administrador"
C = "Cliente"
Atravez del Login.jsp mediante el metodo POST envio textinputs con los
nombre txtLogin y txtClave estos datos los envio al LoginServlet
para todo esto la conexion a la BD la hice atravez del datadource
atravez de un pool de conexiones en el administrador de consola del
GLASFISH V2 hasta aqui no hay problema ("aqui codigo"). CODIGO JAVA
proyectoanovo.utils
package proyectoanovo.utils;
public class AnovoBDUtils {
public static String DATASOURCE="jdbc/ANOVO";
public static String VALIDAR_TIPOUSUARIO=
"SELECT TIPO_USUARIO FROM USUARIO " +
"WHERE LOGIN=?";
public static String VALIDAR_USUARIO=
"SELECT LOGIN, CLAVE FROM USUARIO " +
"WHERE LOGIN=? AND CLAVE=?";
public static String VALIDAR_TIPOUSUARION=
"SELECT LOGIN, TIPO_USUARIO FROM USUARIO " +
"WHERE LOGIN=?";
}
proyectoanovo.jdbc
CLASE DAO
package proyectoanovo.jdbc;
import java.sql.*;
public abstract class DAOAnovo {
public Connection getConnection(String data)
{
Connection con = null;
try{
con=PAnovoServiceLocator.getInstance()
.getDataSource(data).getConnection();
}catch (Exception e){
System.out.println("Error en getConnection" +
e.getMessage());
}return con;
}
public void freeResource(Connection con) {
try {
if (con!=null) con.close();
} catch(SQLException sqle) {
System.out.println("Error freeResource : " +
sqle.getMessage());
}
}
public void freeResource(Statement stmt) {
try {
if (stmt!=null) stmt.close();
} catch(SQLException sqle) {
System.out.println("Error freeResource : " +
sqle.getMessage());
}
}
public void freeResource(PreparedStatement pstmt) {
try {
if (pstmt!=null) pstmt.close();
} catch(SQLException sqle) {
System.out.println("Error freeResource : " +
sqle.getMessage());
}
}
public void freeResource(CallableStatement cstmt) {
try {
if (cstmt!=null) cstmt.close();
} catch(SQLException sqle) {
System.out.println("Error freeResource : " +
sqle.getMessage());
}
}
public void freeResource(ResultSet rs) {
try {
if (rs!=null) rs.close();
} catch(SQLException sqle) {
System.out.println("Error freeResource : " +
sqle.getMessage());
}
}
}
CLASE LoginDAOAnovoImpl
package proyectoanovo.jdbc;
import java.sql.*;
import java.util.*;
import java.lang.*;
import proyectoanovo.utils.*;
import proyectoanovo.beans.*;
public class LoginDAOAnovoImpl extends DAOAnovo {
public boolean existeUsuario(String login, String clave)
{
boolean existe = false;
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs = null;
System.out.println("SQLUtils.DATASOURCE="+AnovoBDUtils.DATASOURCE);
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt = con.prepareStatement(AnovoBDUtils.VALIDAR_USUARIO);
pstmt.setString(1, login);
pstmt.setString(2, clave);
rs = pstmt.executeQuery();
if (rs.next()) existe = true;
}catch (Exception e){
System.out.println("Error existeUsuario "+
e.getMessage());
}finally{
freeResource(rs); freeResource(pstmt); freeResource(con);
}return existe;
}
public boolean existeTipoUsuario1(String login)
{
boolean existe = false;
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs = null;
System.out.println("SQLUtils.DATASOURCE="+AnovoBDUtils.DATASOURCE);
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt =
con.prepareStatement(AnovoBDUtils.VALIDAR_TIPOUSUARIO);
pstmt.setString(1, login);
rs = pstmt.executeQuery();
if (rs.next()) existe = true;
}catch (Exception e){
System.out.println("Error existeUsuario "+
e.getMessage());
}finally{
freeResource(rs); freeResource(pstmt); freeResource(con);
}return existe;
}
public Collection existeTipoUsuario(String login)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList tipousuario = null;
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt =
con.prepareStatement(AnovoBDUtils.VALIDAR_TIPOUSUARIO);
pstmt.setString(1,login);
rs = pstmt.executeQuery();
//if (rs.next()) //tipousuario = true;
while (rs.next())
{
TipoUsuarioBean lg = new TipoUsuarioBean();
lg.setTipoUsuario(rs.getString(1));
if (tipousuario==null) tipousuario= new ArrayList();
tipousuario.add(lg);
}
}catch (Exception e){
System.out.println("Error en existeTipoUsuario" +
e.getMessage());
}finally{
freeResource(rs); freeResource(pstmt); freeResource(con);
}return tipousuario;
}
public PreparedStatement tUsuarioPstmt (String login)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList tipousuario = null;
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt =
con.prepareStatement(AnovoBDUtils.VALIDAR_TIPOUSUARIO);
pstmt.setString(1,login);
rs = pstmt.executeQuery();
while (rs.next())
{
TipoUsuarioBean lg = new TipoUsuarioBean();
lg.setTipoUsuario(rs.getString(1));
if (tipousuario==null) tipousuario= new ArrayList();
tipousuario.add(lg);
}
}catch (Exception e){
System.out.println("Error en existeTipoUsuario" +
e.getMessage());
}finally {
freeResource(rs); freeResource(pstmt); freeResource(con);
}return pstmt;
}
public ResultSet tUsuarioRS (String login)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList tipousuario = null;
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt =
con.prepareStatement(AnovoBDUtils.VALIDAR_TIPOUSUARIO);
pstmt.setString(1,login);
rs = pstmt.executeQuery();
while (rs.next())
{
TipoUsuarioBean lg = new TipoUsuarioBean();
lg.setTipoUsuario(rs.getString(1));
if (tipousuario==null) tipousuario= new ArrayList();
tipousuario.add(lg);
}
}catch (Exception e){
System.out.println("Error en existeTipoUsuario" +
e.getMessage());
}finally {
freeResource(rs); freeResource(pstmt); freeResource(con);
}return rs;
}
public String tUsuarioString(String login)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//ArrayList tipousuario = null;
String tipousuario = "";
con = getConnection(AnovoBDUtils.DATASOURCE);
try{
pstmt =
con.prepareStatement(AnovoBDUtils.VALIDAR_TIPOUSUARIO);
pstmt.setString(1,login);
//pstmt.setString(2,tipo);
rs = pstmt.executeQuery();
//if (rs.next()) //tipousuario = true;
while (rs.next())
{
TipoUsuarioBean lg = new TipoUsuarioBean();
lg.setTipoUsuario(rs.getString(1));
if (tipousuario==null) tipousuario= new String();
//tipousuario.add(lg);
//tipousuario =
String.valueOf(lg.setTipoUsuario(rs.getString(1)));
//tipousuario=String.valueOf(lg.setTipoUsuario(rs.getString(1)));
tipousuario=String.valueOf(rs);
}
}catch (Exception e){
System.out.println("Error en existeTipoUsuario" +
e.getMessage());
}finally{
freeResource(rs); freeResource(pstmt); freeResource(con);
}return tipousuario;
}
}
LOGINSERVLET
public class LoginServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String login = request.getParameter("txtLogin");
String clave = request.getParameter("txtClave");
LoginDAOAnovoImpl lg = new LoginDAOAnovoImpl();
String siguiente = "/Bienvenida.jsp";
//String tUsuario=String.valueOf(lg.existeTipoUsuario(login));
String tUsuario=lg.tUsuarioString(login);
if (tUsuario.equals("A"))
siguiente = "/MenuA.jsp";
if (tUsuario.equals("C"))
siguiente = "/MenuC.jsp";
// if (tipu.equals(adm1))
//if (tipu.compareTo(adm1))
// adm=true;
/*if ((lg.existeTipoUsuario1(login)) == adm)
siguiente = "/MenuA.jsp";*/
/*if (lg.existeUsuario(login, clave))
{
//if ((lg.existeTipoUsuario1(login)) == adm)
siguiente = "/MenuA.jsp";
}*/
//if ((lg.existeTipoUsuario1(login)) == adm)
//if (lg.existeUsuario(login, clave))
//siguiente = "/MenuA.jsp";
//if ((lg.existeTipoUsuario1(login)) == cli)
//siguiente = "/MenuC.jsp";
/*
String tipoU = new String();
String tus = tipoU.valueOf(lg.existeTipoUsuario(login));*/
if (lg.existeUsuario(login, clave))
siguiente = "/AdmServlet";
request.getRequestDispatcher(siguiente)
.forward(request, response);
}
lo que necesito es recuperar el valor del tipo de usuario A o C en una
variable String utilizando como parametro de recuperacion el txtLogin
para poder compararlo y enviarlo al AdmServlet o al ClienteServlet
dependiendo del tipo_usuario que recupere
POR FAVOR AYUDENME ES URGENTISIMO Y NO ENCUENTRO EN LA RED NADA
PARECIDO MUCHAS GRACIAS.
ATTE
NILTON GONGORA ZEGARRA.