Foros del Web » Programación para mayores de 30 ;) » Java »

problema con conexion a BD

Estas en el tema de problema con conexion a BD en el foro de Java en Foros del Web. Hola: Estoy haciendo un aplicacion web la que utilizo un DataSoure. El codigo del context.xml es el siguiente: <Context antiJARLocking="true" crossContext="false" debug="0" docBase="Ejercicio5" path="/Ejercicio5" reloadable="true"> ...
  #1 (permalink)  
Antiguo 18/03/2010, 11:57
 
Fecha de Ingreso: mayo-2009
Mensajes: 18
Antigüedad: 14 años, 11 meses
Puntos: 0
problema con conexion a BD

Hola:
Estoy haciendo un aplicacion web la que utilizo un DataSoure. El codigo del context.xml es el siguiente:

<Context antiJARLocking="true" crossContext="false" debug="0" docBase="Ejercicio5" path="/Ejercicio5" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="localhost_log." suffix=".txt" timestamp="true"/>
<Resource auth="SERVLET" name="jdbc/quoting" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/quoting">
<parameters>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameters>
<parameters>
<name>driverName</name>
<value>jdbc:mysql://localhost:3306/quoting</value>
</parameters>
</ResourceParams>
</Context>

El archivo web.xml:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>Servlets.Main</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/Servlets/Main</url-pattern>
</servlet-mapping>
...
<resource-ref>
<res-ref-name>jdbc/quoting</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>SERVLET</res-auth>
</resource-ref>
</web-app>

Y el servlet Main:

public class Main extends HttpServlet {

/*CONEXION BASE DE DATOS*/
private DataSource ds;
private HttpSession sesion;

/*INICIALIZAR SERVLET. UTILIZAR JNDI PARA BUSCAR UN DataSource*/
@Override
public void init(){
try{
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/quoting");
}
catch(javax.naming.NamingException e){
System.out.println("Un problema ocurrió mientras se recuperaba un DataSurce");
System.out.println(e.toString());
}
}
...
private boolean autentificar (String _uid, String _pwd){
Connection conexion = null;
ResultSet rs = null;
try{
conexion = ds.getConnection("xxxx","xxxx");
Statement s = conexion.createStatement();
rs = s.executeQuery("SELECT * FROM usuario WHERE id_usuario='"+_uid+"' and contraseña='"+_pwd+"'");
return (rs.next());
}
catch(java.sql.SQLException e){
System.out.println("Un problema ocurrió mientras se accdía a la base de datos");
System.out.println(e.toString());
}
finally{
try{
conexion.close();
}
catch (SQLException e){
System.out.println("Un problema ocurrió mientras se cerraba la conexión a la base de datos");
System.out.println(e.toString());
}
}
return false;
}

Mi problema es el siguiente: Cuando se ejecuta el init() no da ningun error, pero mas tarde cuando llega al metodo autentificar y se ejecuta la instruccion conexion = ds.getConnection("xxxx","xxxx"); me salta al conexion.close() directamente.
¿Por qué? ?Qué está mal?
Gracias de antemano

Etiquetas: bd, conexion
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:30.