elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  [AYUDA] jdbc , conexion que se queda colgando.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [AYUDA] jdbc , conexion que se queda colgando.  (Leído 1,409 veces)
bash

Desconectado Desconectado

Mensajes: 258



Ver Perfil
[AYUDA] jdbc , conexion que se queda colgando.
« en: 21 Octubre 2016, 18:37 pm »

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
  1.  
  2. public class RockPosDBManager
  3. {
  4. private boolean debugEnable;
  5. private int     engine;
  6. private int     typeOfConn;
  7.  
  8.  
  9. private String  Embeddeddriver    = "org.apache.derby.jdbc.EmbeddedDriver";
  10. private String  Clientdriver      = "org.apache.derby.jdbc.ClientDriver";
  11. private String  mySQLClientDriver = "com.mysql.jdbc.Driver";
  12.  
  13. private String  DerbyServer   = "jdbc:derby://localhost:1527/";
  14. private String  DerbyNoServer = "jdbc:derby:/";
  15. private String  MySQLServer   = "jdbc:mysql://localhost:3306/";
  16.  
  17.  
  18. private String  DBName;
  19. private String  UserName;
  20. private String  password;
  21.  
  22.  
  23. private String finalConnectionString;
  24. private String finalDriver;
  25. private boolean    Authentication;
  26.  
  27.  
  28. private boolean    create;
  29. private Connection connection;
  30. private boolean    isConnected;
  31.  
  32.  
  33. /**
  34. * @function Print
  35. * @brief  this function Print Information on terminal console if the Debug Flag is enable.
  36. * @param String - this String is the value that will be print onto the screen
  37. * */
  38. private void Print(String templ){
  39. if( debugEnable == false) return;
  40. if( templ != null )
  41. {
  42. //System.out.printf("DEBUG :  %s\n", templ );
  43. }
  44. }
  45.  
  46.  
  47. /**
  48. * @function Print
  49. * @brief  this constructor will set the current Engine, kind of connection, we have  avaliable Derby engine and Mysql .
  50. * @param  int sqlEngine =type of Engine rockPosClient = server, embedded or Client
  51. * @see  RockPosDBEngine &  RockPosTypeConn
  52. * */
  53. public RockPosDBManager(int sqlEngine , int rockposClient, boolean created ){
  54. this.engine     = sqlEngine;
  55.    this.typeOfConn = rockposClient;
  56.    this.debugEnable     = false;
  57.    this.isConnected = false;
  58.    this.create = created;
  59. }
  60.  
  61. /**
  62. * @function setDebugVerbose
  63. * @brief  this function will turn on the Debug Flag for Terminal.
  64. * @param  none
  65. * @see  
  66. * */
  67. public void setDebugVerbose(boolean e){
  68. debugEnable=e;
  69. }
  70.  
  71. /**
  72. * @function isConnectedIntoDatabase
  73. * @brief  this Function Verify if the databe is connected;
  74. * @param  none
  75. * @see  
  76. * */
  77. public boolean isConnectedIntoDatabase(){
  78. return this.isConnected;
  79. }
  80.  
  81. /**
  82. * @function SetDBNameConn
  83. * @brief  
  84. * @param  
  85. * @see  
  86. * */
  87. public void SetDBNameConn(String DBname){
  88.     try{
  89.     if(DBname == null)
  90.     throw new Exception("DB name Is null.");
  91.  
  92.     this.DBName = DBname;
  93.     Print("DB name is : "+ DBName);
  94.  
  95.  
  96.     switch(engine){
  97.  
  98.     case RockPosDBEngine.SQL_DERBY:
  99.      Print("ENGINE SQL_DERBY");
  100.     switch(typeOfConn)
  101.     {
  102.     case RockPosTypeConn.ROCKPOS_CLIENT:
  103.      Print("Type Of Connection  ROCKPOS_CLIENT");
  104.       finalConnectionString = DerbyServer   + this.DBName;
  105.       finalDriver = Clientdriver;
  106.       break;
  107.     case RockPosTypeConn.ROCKPOS_EMBEDDED:
  108.       Print("Type Of Connection  ROCKPOS_EMBEDDED");
  109.       finalConnectionString = DerbyNoServer  + this.DBName;
  110.       finalDriver = Embeddeddriver ;
  111.       break;
  112.     case RockPosTypeConn.ROCKPOS_SERVER:
  113.       Print("Type Of Connection  ROCKPOS_SERVER");
  114.       finalConnectionString = DerbyServer   + this.DBName;
  115.       finalDriver = Clientdriver;
  116.       break;
  117.     }
  118.     break;
  119.  
  120.  
  121.     case RockPosDBEngine.SQL_MYSQL:
  122.     Print("ENGINE SQL_MYSQL");
  123.  
  124.  
  125.     switch(typeOfConn)
  126.     {
  127.     case RockPosTypeConn.ROCKPOS_CLIENT:
  128.     ///   Print("Type Of Connection  ROCKPOS_CLIENT");
  129.       finalConnectionString = MySQLServer   + this.DBName;
  130.       finalDriver = mySQLClientDriver;
  131.       break;
  132.     case RockPosTypeConn.ROCKPOS_EMBEDDED:
  133.     ////  Print("Type Of Connection  ROCKPOS_EMBEDDED");
  134.       Print("MySQL does not work in Embedded Mode yet.");
  135.       finalDriver = mySQLClientDriver;
  136.       break;
  137.     case RockPosTypeConn.ROCKPOS_SERVER:
  138.     ////  Print("Type Of Connection  ROCKPOS_SERVER");
  139.       finalConnectionString = MySQLServer  + this.DBName;
  140.       finalDriver = mySQLClientDriver;
  141.       break;
  142.     }
  143.     break;
  144.     }
  145.      Print("Connection Driver is : "+ finalDriver);
  146.      Print("Connection String is  ===> "+ finalConnectionString);
  147.     if(this.UserName != null && this.password != null)
  148.     {
  149.     Print("Authentication is  On.");
  150.     Print("User name is =[ "+this.UserName+ " ] password = [ "+ this.password +"]");
  151.     Authentication = true;
  152.     }
  153.     else
  154.     {
  155.      Print("Authentication is  On.");
  156.      Print("No user name was provide.");
  157.     Authentication = false;
  158.  
  159.     }
  160.  
  161.     }catch(Exception e)
  162.     {
  163.      e.printStackTrace();
  164.     }
  165. }
  166.  
  167. /**
  168. * @function SetCredential
  169. * @brief  
  170. * @param  
  171. * @see  
  172. * */
  173. public void SetCredential(String user, String pass){
  174.   if(user != null)
  175.   this.UserName = user;
  176.   if(pass != null)
  177.     this.password = pass;
  178. }
  179.  
  180. public void RequireAuth(boolean auth){
  181. this.Authentication = auth;
  182. }
  183.  
  184.  
  185.  
  186. /**
  187. * @function Connect
  188. * @brief  
  189. * @param  
  190. * @see  
  191. * */
  192. public boolean Connect(){
  193.  
  194. this.connection = null;
  195. try{
  196. Class.forName(finalDriver);
  197. }
  198. catch(Exception e)
  199. {
  200. e.printStackTrace();
  201.  
  202. this.isConnected = false;
  203. return this.isConnected ;
  204. }
  205.  
  206. try{
  207.  
  208. String param;
  209.   if(create == false)
  210.   {
  211.  
  212.   param =  finalConnectionString +";user="+ this.UserName+";password="+ this.password;
  213.  
  214.   }
  215.   else
  216.   {
  217.   param =  finalConnectionString +";create=true;user="+this.UserName+";password="+ this.password;
  218.  
  219.   }
  220.  
  221.    System.out.printf("Param to Server : %s", param);
  222.    this.connection = DriverManager.getConnection(param);
  223.                    if(this.connection.isValid(30)){
  224.                     this.isConnected = true;
  225.                     System.out.print("\nEsta Conectado.");
  226.                    }
  227.                    else{
  228.                     this.isConnected = false;
  229.                     System.out.print("\nno Esta Conectado.");
  230.  
  231.                    }
  232.  
  233.  
  234. }
  235. catch(SQLException e)
  236. {
  237.   int code = e.getErrorCode();
  238.   e.printStackTrace();
  239.   this.isConnected = false;
  240.   return this.isConnected;
  241. }
  242.  
  243.  
  244.      return this.isConnected;
  245.  
  246. }
  247.  
  248.  
  249. public boolean Disconnect(){
  250.  
  251.   try {
  252.     this.connection.close();
  253.     } catch (SQLException e) {
  254.  
  255.      e.printStackTrace();
  256.  return isConnected;
  257.     }
  258.     this.isConnected = false;
  259. return isConnected;  
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. public boolean Execute(String sql){
  271. ///Print("Execute");
  272. boolean res = true;
  273. if(this.isConnected == false  )
  274. {
  275.   Print("theres is not Connection");
  276.   return false;
  277. }
  278.  
  279. PreparedStatement stmt   = null;
  280. try
  281. {
  282.    stmt   =  this.connection.prepareStatement(sql);
  283.    stmt.executeUpdate(sql);
  284. }catch(Exception e){
  285. e.printStackTrace();
  286. Print("Error Executing that Query");
  287. return false;
  288. }
  289. finally{
  290. ///Print("Execute Finally");
  291. try {
  292. stmt.close();
  293.  
  294. } catch (Exception e) {
  295.        Print("Error closing Statement.");
  296. }
  297. }
  298. return res;
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. public ResultSet Query(String sql){
  309.  
  310. if(this.isConnected == false  )
  311. {
  312.   Print("theres is not Connection");
  313.   return null;
  314. }
  315.  
  316. Statement stmt   = null;
  317. ResultSet result = null;
  318. try
  319. {
  320.  
  321.  
  322. this.connection.setAutoCommit(true);
  323.    stmt   =  this.connection.createStatement();
  324.    result = stmt.executeQuery(sql);
  325.    if(result == null)
  326.     Print("Result is null");
  327.  
  328. }catch(Exception e){
  329. Print("Error Executing that Query");
  330. }
  331. try {
  332. stmt.close();
  333. } catch (Exception e) {
  334.            Print("Error closing Statement.");
  335. }
  336. return result;
  337. }
  338.  
  339.  
  340. public void CloseConn(){
  341. try {
  342. this.connection.close();  
  343. Print("Closing Connection");
  344.  
  345. } catch (Exception e) {
  346.          Print("Error Closing Conection.");
  347. }
  348. this.isConnected= false;
  349.  
  350. }
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357. }
  358.  


algo que me falto ponerle es una prueba de lo que quiero hacer
Código
  1. RockPosDBManager rm = new RockPosDBManager(RockPosDBEngine.SQL_DERBY, RockPosTypeConn.ROCKPOS_CLIENT, true);
  2. rm.SetDBNameConn(RockPosConstant.DB_PATH+"d1234f");
  3. rm.SetCredential("dbmanager", "dbmanager");
  4. boolean creado = rm.Connect();
  5. if(creado == true)
  6. System.out.println("prueba 1Creado");
  7. else{
  8. System.out.println("prueba 1 no Creado");
  9.  
  10. }
  11. String sql = "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\'derby.database.defaultConnectionMode\',\'noAccess\')";
  12. creado = rm.Execute(sql);
  13. if(creado == true)
  14. System.out.println(" prueba 2 Se Ejecuto");
  15. else{
  16. System.out.println(" prueba 2 no sejecuto Creado");
  17.  
  18. }
  19.  
  20. rm.Disconnect();
  21. rm.SetDBNameConn(RockPosConstant.DB_PATH+"d1234f");
  22. rm.SetCredential("usuario2", "usuario2");
  23.    creado = rm.Connect();
  24. if(creado == true)
  25. System.out.println("\n\n\n prueba 2 Creado\n");
  26. else{
  27. System.out.println("\n\n\n prueba 2 no Creado\n");
  28.  
  29. }
  30. rm.Disconnect();
  31. rm.SetCredential("dbmanager", "dbmanager");
  32.    creado = rm.Connect();
  33. if(creado == true)
  34. System.out.println(" prueba 4 prueba pasada ");
  35. else{
  36. System.out.println("prueba 4 prueba no pasada");
  37.    return;
  38. }
  39.  
  40.  
  41.  
  42.  


« Última modificación: 21 Octubre 2016, 19:05 pm por bash » En línea

gracias por responder mis dudas
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Error de JDBC :S
Java
Xedrox 2 2,402 Último mensaje 19 Julio 2011, 04:21 am
por klaine
Un israelí responde al 'ciberintruso' saudí colgando datos de tarjetas de ...
Noticias
wolfbcn 0 1,245 Último mensaje 13 Enero 2012, 02:10 am
por wolfbcn
Conectar PDO a JDBC
PHP
ZeroVzla 1 1,328 Último mensaje 16 Septiembre 2013, 01:26 am
por ~ Yoya ~
Así es cómo están colgando películas completas en YouTube sin que Google ....
Noticias
wolfbcn 0 2,092 Último mensaje 17 Febrero 2015, 19:04 pm
por wolfbcn
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines