Bonjour,
j'ai implémenté un manager pour encapsuler mes accès à la base de données. Mon problème est que l'object Connection devient "null" dès que je quitte la méthode qui l'instancie. alors que le même code en vrac fonctionne. Quelqu'un à il déjà été confonté au problème et a une solution.
voici ma classe correspondant au manager
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DatabaseAccess {
private Connection conn;
private PreparedStatement prepstmnt;
private DataSource datasrc;
private String errormsg;
private Statement stmnt;
/**
* Method databaseAccess
*
* Class Constructor
*/
public DatabaseAccess() {
conn = null;
prepstmnt = null;
datasrc=null;
stmnt=null;
errormsg=null;
}
/**
* Method dbConnect
*
*
*/
public void dbConnect(String JNDIparameter) throws NamingException, SQLException {
//
// get Database Access from JNDI
try {
errormsg="";
Context initCtx = new InitialContext();
DataSource datasrc = (DataSource)initCtx.lookup("java:comp/env/jdbc/"+JNDIparameter);
Connection conn = datasrc.getConnection();
initCtx.close(); //destruction du contexte JNDI
}
catch (NamingException ne) {
errormsg=" Could not access to JNDI
\n"+ne.toString();
throw new NamingException(errormsg);
}
catch (SQLException sqle) {
errormsg ="
Could not connect to Database
\n"+sqle.toString();
throw new SQLException(errormsg);
}
catch (Exception e) {
errormsg= " Could not connect to Database
\n"+e.toString();
throw new SQLException(errormsg);
}
}
/**
* Method closeResultSet
*
*
*
*/
public void closeResultSet(ResultSet rs) throws SQLException {
try {
errormsg ="";
rs.close();
}
catch (SQLException sqle){
errormsg= " Could not close recordset
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
/**
* Method getResultSet
*
*
* @return
*
*/
public ResultSet getResultSet(String query) throws SQLException{
ResultSet rs =null;
try {
errormsg= "";
Statement stmnt = (Statement) conn.createStatement();
rs=stmnt.executeQuery(query);
}
catch (SQLException sqle){
errormsg=" Could not execute query: "+query+"
\n"+sqle.toString();
throw new SQLException(errormsg);
}
return rs;
}
/**
* Method dbClose
*
*
*/
public void dbClose() throws SQLException {
try{
errormsg ="";
if (stmnt!= null) {
stmnt.close();
}
if (conn!=null) {
conn.close();
}
}
catch(SQLException sqle) {
errormsg=" Could not close Database Access
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
/**
* Method doRollback
*
*
*/
public void doRollback() throws SQLException {
try {
errormsg ="";
conn.rollback();
}
catch (SQLException sqle){
errormsg= " Could not rollback transaction
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
/**
* Method doCommit
*
*
*/
public void doCommit() throws SQLException {
try {
errormsg ="";
conn.commit();
}
catch (SQLException sqle){
errormsg= " Could not commit transaction
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
/**
* Method doActionSqlQuery
*
*
*/
public void doActionSqlQuery(String query) throws SQLException {
try {
doActionSqlQuery(query, true);
}
catch (SQLException sqle){
errormsg =sqle.toString();
throw new SQLException(errormsg);
}
}
/**
* Method doActionSqlQuery
*
*
*/
public void doActionSqlQuery(String query, boolean autocommit) throws SQLException {
try {
errormsg="";
if (conn!=null) {
conn.setAutoCommit(autocommit);
} else {
System.out.println("conn perdu");
}
Statement stmnt = (Statement) conn.createStatement();
if (stmnt==null) {
System.out.println("stmnt perdu");
}
stmnt.executeQuery(query);
}
catch (SQLException sqle){
errormsg="
Could not execute query: "+query+"
\n"+sqle.toString();
throw new SQLException(errormsg);
}
}
}
Afficher la suite