[AYUDA] jdbc , conexion que se queda colgando.

Iniciado por bash, 21 Octubre 2016, 18:37 PM

0 Miembros y 1 Visitante están viendo este tema.

bash

un Saludo a Todos !!

tengo un incovenienten , les cuento estoy creado una clase para que me haga de manager con las bases de datos es decir que me cree la conexion , que ejecutes los query o  alguna actulizacion y luego cierre la conexion , el siguiente incoveniente es el que tengo , mi clase se conecta bien a la base de datos (estoy usando Auntenticacion )si fallo me da error y luego si trato ingresar con un usuario valido como quiera me da la misma excepcion(rechanzando la conexion ), estoy usando Derby pero supongo que es problema de programacion, la verdad estoy perdido. dejare el codigo a ver si me pueden hechar una mano o consejo

Código (java) [Seleccionar]


public class RockPosDBManager
{
private boolean debugEnable;
private int     engine;
private int     typeOfConn;


private String  Embeddeddriver    = "org.apache.derby.jdbc.EmbeddedDriver";
private String  Clientdriver      = "org.apache.derby.jdbc.ClientDriver";
private String  mySQLClientDriver = "com.mysql.jdbc.Driver";

private String  DerbyServer   = "jdbc:derby://localhost:1527/";
private String  DerbyNoServer = "jdbc:derby:/";
private String  MySQLServer   = "jdbc:mysql://localhost:3306/";


private String  DBName;
private String  UserName;
private String  password;

 
private String finalConnectionString;
private String finalDriver;
private boolean    Authentication;


private boolean    create;
private Connection connection;
private boolean    isConnected;


/**
* @function Print
* @brief  this function Print Information on terminal console if the Debug Flag is enable.
* @param String - this String is the value that will be print onto the screen
* */
private void Print(String templ){
if( debugEnable == false) return;
if( templ != null )
{
//System.out.printf("DEBUG :  %s\n", templ );
}
}


/**
* @function Print
* @brief  this constructor will set the current Engine, kind of connection, we have  avaliable Derby engine and Mysql .
* @param  int sqlEngine =type of Engine rockPosClient = server, embedded or Client
* @see  RockPosDBEngine &  RockPosTypeConn
* */
public RockPosDBManager(int sqlEngine , int rockposClient, boolean created ){
this.engine     = sqlEngine;
   this.typeOfConn = rockposClient;
   this.debugEnable     = false;
   this.isConnected = false;
   this.create = created;
}

/**
* @function setDebugVerbose
* @brief  this function will turn on the Debug Flag for Terminal.
* @param  none
* @see  
* */
public void setDebugVerbose(boolean e){
debugEnable=e;
}

/**
* @function isConnectedIntoDatabase
* @brief  this Function Verify if the databe is connected;
* @param  none
* @see  
* */
public boolean isConnectedIntoDatabase(){
return this.isConnected;
}

/**
* @function SetDBNameConn
* @brief  
* @param  
* @see  
* */
public void SetDBNameConn(String DBname){
    try{
    if(DBname == null)
    throw new Exception("DB name Is null.");
   
    this.DBName = DBname;
    Print("DB name is : "+ DBName);
   
   
    switch(engine){
   
    case RockPosDBEngine.SQL_DERBY:
     Print("ENGINE SQL_DERBY");
    switch(typeOfConn)
    {
    case RockPosTypeConn.ROCKPOS_CLIENT:
     Print("Type Of Connection  ROCKPOS_CLIENT");
      finalConnectionString = DerbyServer   + this.DBName;
      finalDriver = Clientdriver;
      break;
    case RockPosTypeConn.ROCKPOS_EMBEDDED:
      Print("Type Of Connection  ROCKPOS_EMBEDDED");
      finalConnectionString = DerbyNoServer  + this.DBName;
      finalDriver = Embeddeddriver ;
      break;
    case RockPosTypeConn.ROCKPOS_SERVER:
      Print("Type Of Connection  ROCKPOS_SERVER");
      finalConnectionString = DerbyServer   + this.DBName;
      finalDriver = Clientdriver;
      break;
    }
    break;
   
   
    case RockPosDBEngine.SQL_MYSQL:
    Print("ENGINE SQL_MYSQL");
   
   
    switch(typeOfConn)
    {
    case RockPosTypeConn.ROCKPOS_CLIENT:
    ///   Print("Type Of Connection  ROCKPOS_CLIENT");
      finalConnectionString = MySQLServer   + this.DBName;
      finalDriver = mySQLClientDriver;
      break;
    case RockPosTypeConn.ROCKPOS_EMBEDDED:
    ////  Print("Type Of Connection  ROCKPOS_EMBEDDED");
      Print("MySQL does not work in Embedded Mode yet.");
      finalDriver = mySQLClientDriver;
      break;
    case RockPosTypeConn.ROCKPOS_SERVER:
    ////  Print("Type Of Connection  ROCKPOS_SERVER");
      finalConnectionString = MySQLServer  + this.DBName;
      finalDriver = mySQLClientDriver;
      break;
    }
    break;
    }
     Print("Connection Driver is : "+ finalDriver);
     Print("Connection String is  ===> "+ finalConnectionString);
    if(this.UserName != null && this.password != null)
    {
    Print("Authentication is  On.");
    Print("User name is =[ "+this.UserName+ " ] password = [ "+ this.password +"]");
    Authentication = true;
    }
    else
    {
     Print("Authentication is  On.");
     Print("No user name was provide.");
    Authentication = false;
   
    }

    }catch(Exception e)
    {
     e.printStackTrace();
    }
}

/**
* @function SetCredential
* @brief  
* @param  
* @see  
* */
public void SetCredential(String user, String pass){
  if(user != null)
  this.UserName = user;
  if(pass != null)
    this.password = pass;
}

public void RequireAuth(boolean auth){
this.Authentication = auth;
}



/**
* @function Connect
* @brief  
* @param  
* @see  
* */
public boolean Connect(){

this.connection = null;
try{
Class.forName(finalDriver);
}
catch(Exception e)
{
e.printStackTrace();

this.isConnected = false;
return this.isConnected ;
}

try{

String param;
  if(create == false)
  {
 
  param =  finalConnectionString +";user="+ this.UserName+";password="+ this.password;
 
  }
  else
  {
  param =  finalConnectionString +";create=true;user="+this.UserName+";password="+ this.password;
 
  }

   System.out.printf("Param to Server : %s", param);
   this.connection = DriverManager.getConnection(param);
                   if(this.connection.isValid(30)){
                    this.isConnected = true;
                    System.out.print("\nEsta Conectado.");
                   }
                   else{
                    this.isConnected = false;
                    System.out.print("\nno Esta Conectado.");
                   
                   }
                       
   
}
catch(SQLException e)
{
  int code = e.getErrorCode();
  e.printStackTrace();
  this.isConnected = false;
  return this.isConnected;
}

                   
     return this.isConnected;

}


public boolean Disconnect(){

  try {
    this.connection.close();
    } catch (SQLException e) {
   
     e.printStackTrace();
 return isConnected;
    }
    this.isConnected = false;
return isConnected;  
}









public boolean Execute(String sql){
///Print("Execute");
boolean res = true;
if(this.isConnected == false  )
{
  Print("theres is not Connection");
  return false;
}

PreparedStatement stmt   = null;
try
{
   stmt   =  this.connection.prepareStatement(sql);
   stmt.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
Print("Error Executing that Query");
return false;
}
finally{
///Print("Execute Finally");
try {
stmt.close();

} catch (Exception e) {
       Print("Error closing Statement.");
}
}
return res;
}








public ResultSet Query(String sql){

if(this.isConnected == false  )
{
  Print("theres is not Connection");
  return null;
}

Statement stmt   = null;
ResultSet result = null;
try
{


this.connection.setAutoCommit(true);
   stmt   =  this.connection.createStatement();
   result = stmt.executeQuery(sql);
   if(result == null)
    Print("Result is null");
   
}catch(Exception e){
Print("Error Executing that Query");
}
try {
stmt.close();
} catch (Exception e) {
           Print("Error closing Statement.");
}
return result;
}


public void CloseConn(){
try {
this.connection.close();  
Print("Closing Connection");

} catch (Exception e) {
         Print("Error Closing Conection.");
}
this.isConnected= false;

}






}



algo que me falto ponerle es una prueba de lo que quiero hacer
Código (java) [Seleccionar]

RockPosDBManager rm = new RockPosDBManager(RockPosDBEngine.SQL_DERBY, RockPosTypeConn.ROCKPOS_CLIENT, true);
rm.SetDBNameConn(RockPosConstant.DB_PATH+"d1234f");
rm.SetCredential("dbmanager", "dbmanager");
boolean creado = rm.Connect();
if(creado == true)
System.out.println("prueba 1Creado");
else{
System.out.println("prueba 1 no Creado");

}
String sql = "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\'derby.database.defaultConnectionMode\',\'noAccess\')";
creado = rm.Execute(sql);
if(creado == true)
System.out.println(" prueba 2 Se Ejecuto");
else{
System.out.println(" prueba 2 no sejecuto Creado");
   
}

rm.Disconnect();
rm.SetDBNameConn(RockPosConstant.DB_PATH+"d1234f");
rm.SetCredential("usuario2", "usuario2");
    creado = rm.Connect();
if(creado == true)
System.out.println("\n\n\n prueba 2 Creado\n");
else{
System.out.println("\n\n\n prueba 2 no Creado\n");
     
}
rm.Disconnect();
rm.SetCredential("dbmanager", "dbmanager");
    creado = rm.Connect();
if(creado == true)
System.out.println(" prueba 4 prueba pasada ");
else{
System.out.println("prueba 4 prueba no pasada");
    return;
}



gracias por responder mis dudas