JNDI Data Source / Prepared Statement

Manou1980 Messages postés 11 Date d'inscription jeudi 30 juin 2011 Statut Membre Dernière intervention 31 octobre 2011 - 27 oct. 2011 à 16:11
Manou1980 Messages postés 11 Date d'inscription jeudi 30 juin 2011 Statut Membre Dernière intervention 31 octobre 2011 - 31 oct. 2011 à 13:51
Bonjour,

Pouvez vous m'aider à executer un Query à l'aide PreparedStatement sachant que ma connexion passe par une DataSource JNDI.

Context.xml

<?xml version= "1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="admin" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/myfirstdb"/>

</Context>	







La Classe ConnexionBDD qui Fonctionne tres bien

package interactiondb;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.mysql.jdbc.PreparedStatement;




public class ConnexionBDD 
{

private static Connection connexion;
static DataSource dataSource =null;
public Context ctx;

public ConnexionBDD() 
{
 
  
this.connexion =  null;

  
try 
{


ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");



} 
catch (NamingException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}


}


public Connection getConnexion() 
{

try 
{

connexion= dataSource.getConnection();
} 
catch (SQLException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}
 
return this.connexion;
}


}







Et enfin la classe CRUD

package interactiondb;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.util.logging.Logger;
import java.util.logging.Level;


import model.Employees;
//import model.Member;
import interactiondb.ConnexionBDD;

public class CRUD 

{

ConnexionBDD connect  = new ConnexionBDD();
Connection con=null;
PreparedStatement pstmt;


public CRUD ()
{

System.out.println("Etape 1");

con = (Connection) connect.getConnexion();

System.out.println("Etape 2");


}

// A Verifier

public int login(String login, String password)
{

int count=0;

try 
{

System.out.println("Etape 3");

pstmt=(PreparedStatement) con.prepareStatement("select * from member"
+ " where login='" + login  + "' and password='" + password  + "'");

System.out.println("Etape 4");


ResultSet result = pstmt.executeQuery();

System.out.println("Etape 5");


// Exploitation des résultats
while (result.next()) 
{
count++;
}
} 
catch (Exception e) 
{

Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
} 

try 
{
pstmt.close();
} 
catch (SQLException e) 
{
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
}
return count;


}

/////////////////////////////


public Employees edit(int matricola)
{

Employees listemployees = null;

try 
{
pstmt=(PreparedStatement) con.prepareStatement("select * from employees"
+ " where matricola='" + matricola  + "'");
ResultSet result = pstmt.executeQuery();

// Exploitation des résultats
while (result.next()) 
{
listemployees = new Employees(result.getInt("matricola"),result.getString("dataas"),result.getString("nome"),result.getString("cognome"),result.getString("datafa"));                                            //////////////////////
}
} 
catch (Exception e) 
{

Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
} 

try 
{
pstmt.close();
} 
catch (SQLException e) 
{
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
}
return listemployees;


}





public int delete(int matricola) throws SQLException
{
int resultat=0;

try 
{

pstmt=(PreparedStatement) con.prepareStatement("delete from 'employees' where 'matricola'="+ matricola);
resultat = pstmt.executeUpdate();


} 
catch (Exception e) 
{

Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
} 

try 
{
pstmt.close();
} 
catch (SQLException e) 
{
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
}

return resultat;
}


public int update(Employees employee) throws SQLException
{

int resultat=0;

Employees emp = new Employees(employee.getmatricola(),employee.getdataas(),employee.getnome(),employee.getcognome(),employee.getdatafa());
try 
{
pstmt=(PreparedStatement) con.prepareStatement("UPDATE employees SET dataas = '" + emp.getdataas() + "', nome = '" + emp.getnome() + "', cognome = '" + emp.getcognome() + "', datafa = '" + emp.getdatafa() + "' WHERE matricola = '" + emp.getmatricola() + "'");
resultat = pstmt.executeUpdate();


} 
catch (Exception e) 
{

Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
} 



try 
{
pstmt.close();
} 
catch (SQLException e) 
{
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
}

return resultat;

}

public int insert(Employees p) throws SQLException
{

int resultat=0;

try 
{
pstmt=(PreparedStatement) con.prepareStatement("INSERT INTO employees(matricola,dataas,nome,cognome,datafa) " +
    "VALUES (" + p.getmatricola() + ", '" + p.getdataas() + "', '" + p.getnome() + "', '" + p.getcognome() + "','" + p.getdatafa() + "')");
resultat = pstmt.executeUpdate();


} 
catch (Exception e) 
{

Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
} 

try 
{
pstmt.close();
} 
catch (SQLException e) 
{
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
}

return resultat;

}


}

2 réponses

Manou1980 Messages postés 11 Date d'inscription jeudi 30 juin 2011 Statut Membre Dernière intervention 31 octobre 2011
28 oct. 2011 à 10:36
Le code fonctionnait très bien lorsque j'utilisais une connexion avec un Driver et non un jndi. La classe CRUD fonctionnait a merveille. Maintenant depuis que j'utilise un JNDI DataSource, la classe crud ne fonctionne plus et l'erreur se produit lors de l'execution de la ligne suivante:

con = (Connection) connect.getConnexion();



Et L'erreur est la suivante:

27-ott-2011 16.24.43 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet Controller threw exception
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to com.mysql.jdbc.Connection
at interactiondb.CRUD.(CRUD.java:30)
at control.Controller.processRequest(Controller.java:121)
at control.Controller.doPost(Controller.java:294)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
0
Manou1980 Messages postés 11 Date d'inscription jeudi 30 juin 2011 Statut Membre Dernière intervention 31 octobre 2011
31 oct. 2011 à 13:51
Le problème est resolu!!! Il fallait remplacer les import suivant:

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

par les import

import java.sql.Connection;
import java.sql.PreparedStatement;
0
Rejoignez-nous