[clos] connexion

Fermé
tapha2150309 Messages postés 20 Date d'inscription mercredi 23 mars 2011 Statut Membre Dernière intervention 21 juillet 2013 - 8 mars 2013 à 21:34
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 9 mars 2013 à 11:22
je travaillai avec sqlserver 2005 .avec la fiche de connexion DB ci apres qui marché tres bien alors je veu plus utiliser sqlserver mais plutot mysql .je veu concervé le DB svp k fau til modifier pour k sa marche avec mysql.mrci voici le DB:

import java.sql.*;
import java.util.ResourceBundle;
/**
*
* @author fama-fall
*/
public class DB {
private int nbligne=0;
private Connection cn=null;
private Statement stmt=null;
private PreparedStatement pstmt;
public DB(){
try
{
//InputStream ins = this.getClass().getResourceAsStream("/propriete/props.properties" );
ResourceBundle bdl = ResourceBundle.getBundle("propriete/props");
String driver=bdl.getString("driver");
String url=bdl.getString("url");
String user=bdl.getString("login");
String password=bdl.getString("password");
Class.forName(driver);
cn=DriverManager.getConnection(url,user,password);
// cn.setAutoCommit(false);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

}

public void My_executeUpdate(String sql){
try{
if(stmt == null)
stmt = cn.createStatement();
nbligne=stmt.executeUpdate(sql);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}

public ResultSet My_ExecuteQuery(String sql,int option1){
ResultSet rs = null;
try
{
switch(option1)
{
case 0:
if(stmt == null)
stmt = cn.createStatement();
rs = stmt.executeQuery(sql);
break;

case 1:
if (stmt == null)
stmt= cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs= stmt.executeQuery(sql);
break;
case 2:
if (stmt == null)
stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs= stmt.executeQuery(sql);
break;
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return rs;
}

public Connection getCn() {return cn;}
public void setCn(Connection cn) {this.cn = cn;}

public void initPrepareCmd(String sql,int option2){
try
{
switch(option2)
{
case 0:
pstmt = getCn().prepareStatement(sql);
break;
case 1:
pstmt= getCn().prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
break;
case 2:
pstmt = getCn().prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
break;
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}

public void My_executePrepareUpdate(){
try{
nbligne=getPstmt().executeUpdate();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}

public ResultSet My_ExecutePrepareQuery(){
ResultSet rs = null;
try{
rs = getPstmt().executeQuery();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
return rs;
}

public void FermerConnexion(){
try{
if(stmt != null)
stmt.close();
if(getPstmt() != null)
getPstmt().close();
if(!cn.isClosed())
cn.close();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}

public void My_commit(){
try{
cn.commit();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}

public void My_rollback(){
try{
cn.rollback();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}

/**
* @return the nbligne
*/
public int getNbligne(){return nbligne;}
public void setNbligne(int nbligne) {this.nbligne = nbligne;}

/**
* @return the pstmt
*/
public PreparedStatement getPstmt() {
return pstmt;
}
public void beginTranssaction()
{
try {
cn.setAutoCommit(false);
} catch (Exception e) {
}

}


public void endTranssaction()
{
try {
cn.setAutoCommit(true);
} catch (Exception e) {
}

}
}

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
9 mars 2013 à 11:22
Bonjour,

- On commence par dire bonjour et on pose sa question poliment
- On n'utilise pas de langage SMS

Sujet clos
0
Rejoignez-nous