JNDI Data Source / Prepared Statement

JNDI Data Source / Prepared Statement - Java - Programmation

Marsh Posté le 27-10-2011 à 16:06:36    

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

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


 
 
 
 
 
 
La Classe ConnexionBDD qui Fonctionne tres bien
 

Code :
  1. package interactiondb;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.sql.DataSource;
  9. import javax.naming.Context;
  10. import javax.naming.InitialContext;
  11. import javax.naming.NamingException;
  12. import com.mysql.jdbc.PreparedStatement;
  13. public class ConnexionBDD
  14. {
  15. private static Connection connexion;
  16. static DataSource dataSource=null;
  17. public Context ctx;
  18. public ConnexionBDD()
  19. {
  20.     
  21.  this.connexion = null;
  22.  
  23.  try
  24.  {
  25.   ctx = new InitialContext();
  26.   dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB" );
  27.  }
  28.  catch (NamingException e)
  29.  {
  30.  // TODO Auto-generated catch block
  31.  e.printStackTrace();
  32.  }
  33. }
  34. public Connection getConnexion()
  35. {
  36.  try
  37.  {
  38.   connexion= dataSource.getConnection();
  39.  }
  40.  catch (SQLException e)
  41.  {
  42.   // TODO Auto-generated catch block
  43.   e.printStackTrace();
  44.  }
  45.  return this.connexion;
  46. }
  47. }


 
 
 
 
 
Et enfin la classe CRUD
 

Code :
  1. package interactiondb;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import com.mysql.jdbc.Connection;
  5. import com.mysql.jdbc.PreparedStatement;
  6. import java.util.logging.Logger;
  7. import java.util.logging.Level;
  8. import model.Employees;
  9. //import model.Member;
  10. import interactiondb.ConnexionBDD;
  11. public class CRUD
  12. {
  13. ConnexionBDD connect = new ConnexionBDD();
  14. Connection con=null;
  15. PreparedStatement pstmt;
  16. public CRUD ()
  17. {
  18.  System.out.println("Etape 1" );
  19.  con = (Connection) connect.getConnexion();
  20.  System.out.println("Etape 2" );
  21. }
  22. // A Verifier
  23. public int login(String login, String password)
  24. {
  25.  int count=0;
  26.  try
  27.  {
  28.   System.out.println("Etape 3" );
  29.   pstmt=(PreparedStatement) con.prepareStatement("select * from member"
  30.     + " where login='" + login  + "' and password='" + password  + "'" );
  31.   System.out.println("Etape 4" );
  32.   ResultSet result = pstmt.executeQuery();
  33.   System.out.println("Etape 5" );
  34.   // Exploitation des résultats
  35.   while (result.next())
  36.   {
  37.    count++;
  38.   }
  39.  }
  40.  catch (Exception e)
  41.  {
  42.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  43.  }
  44.  try
  45.  {
  46.   pstmt.close();
  47.  }
  48.  catch (SQLException e)
  49.  {
  50.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  51.  }
  52.  return count;
  53. }
  54. /////////////////////////////
  55. public Employees edit(int matricola)
  56. {
  57.  Employees listemployees = null;
  58.  try
  59.  {
  60.   pstmt=(PreparedStatement) con.prepareStatement("select * from employees"
  61.     + " where matricola='" + matricola  + "'" );
  62.   ResultSet result = pstmt.executeQuery();
  63.   // Exploitation des résultats
  64.   while (result.next())
  65.   {
  66.    listemployees = new Employees(result.getInt("matricola" ),result.getString("dataas" ),result.getString("nome" ),result.getString("cognome" ),result.getString("datafa" ));                                            //////////////////////
  67.   }
  68.  }
  69.  catch (Exception e)
  70.  {
  71.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  72.  }
  73.  try
  74.  {
  75.   pstmt.close();
  76.  }
  77.  catch (SQLException e)
  78.  {
  79.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  80.  }
  81.  return listemployees;
  82. }
  83. public int delete(int matricola) throws SQLException
  84. {
  85.  int resultat=0;
  86.  try
  87.  {
  88.   pstmt=(PreparedStatement) con.prepareStatement("delete from 'employees' where 'matricola'="+ matricola);
  89.   resultat = pstmt.executeUpdate();
  90.  }
  91.  catch (Exception e)
  92.  {
  93.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  94.  }
  95.  try
  96.  {
  97.   pstmt.close();
  98.  }
  99.  catch (SQLException e)
  100.  {
  101.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  102.  }
  103.  return resultat;
  104. }
  105. public int update(Employees employee) throws SQLException
  106. {
  107.  int resultat=0;
  108.  Employees emp = new Employees(employee.getmatricola(),employee.getdataas(),employee.getnome(),employee.getcognome(),employee.getdatafa());
  109.  try
  110.  {
  111.   pstmt=(PreparedStatement) con.prepareStatement("UPDATE employees SET dataas = '" + emp.getdataas() + "', nome = '" + emp.getnome() + "', cognome = '" + emp.getcognome() + "', datafa = '" + emp.getdatafa() + "' WHERE matricola = '" + emp.getmatricola() + "'" );
  112.   resultat = pstmt.executeUpdate();
  113.  }
  114.  catch (Exception e)
  115.  {
  116.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  117.  }
  118.  try
  119.  {
  120.   pstmt.close();
  121.  }
  122.  catch (SQLException e)
  123.  {
  124.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  125.  }
  126.  return resultat;
  127. }
  128. public int insert(Employees p) throws SQLException
  129. {
  130.  int resultat=0;
  131.  try
  132.  {
  133.   pstmt=(PreparedStatement) con.prepareStatement("INSERT INTO employees(matricola,dataas,nome,cognome,datafa) " +
  134.     "VALUES (" + p.getmatricola() + ", '" + p.getdataas() + "', '" + p.getnome() + "', '" + p.getcognome() + "','" + p.getdatafa() + "')" );
  135.   resultat = pstmt.executeUpdate();
  136.  }
  137.  catch (Exception e)
  138.  {
  139.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  140.  }
  141.  try
  142.  {
  143.   pstmt.close();
  144.  }
  145.  catch (SQLException e)
  146.  {
  147.   Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, e);
  148.  }
  149.  return resultat;
  150. }
  151. }



---------------
Manou1980
Reply

Marsh Posté le 27-10-2011 à 16:06:36   

Reply

Marsh Posté le 31-10-2011 à 13:57:35    

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;

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed