JNDI Data Source / Prepared Statement
Manou1980
Messages postés
12
Statut
Membre
-
Manou1980 Messages postés 12 Statut Membre -
Manou1980 Messages postés 12 Statut Membre -
Bonjour,
Pouvez vous m'aider à executer un Query à l'aide PreparedStatement sachant que ma connexion passe par une DataSource JNDI.
Context.xml
La Classe ConnexionBDD qui Fonctionne tres bien
Et enfin la classe CRUD
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;
}
}
A voir également:
- JNDI Data Source / Prepared Statement
- App data - Guide
- Get data back - Télécharger - Récupération de données
- Windows data recovery - Télécharger - Récupération de données
- Fichier .data - Guide
- Web data extractor - Télécharger - Divers Web & Internet