Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class ConnectionDerby {
  10.  
  11. private Connection conn = null;
  12. private Statement sttm = null;
  13.  
  14. public Connection CrearBD(String query) {
  15. try {
  16. //Obtenemos el Driver de Derby
  17. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  18. conn = DriverManager.getConnection("jdbc:derby:.\BD\nombrebasededatos.db;create=true");
  19. if (conn != null) {
  20. //JOptionPane.showMessageDialog(null, "Base de Datos Lista");
  21. try {
  22. PreparedStatement pstm = conn.prepareStatement(query);
  23. pstm.execute();
  24. pstm.close();
  25. //JOptionPane.showMessageDialog(null, "Base de Datos Creada Correctamente");
  26. System.out.println("SENTENCIA SQL EFECTUADA CORRECTAMENTE");
  27. } catch (SQLException ex) {
  28. //JOptionPane.showMessageDialog(null, ex.getLocalizedMessage());
  29. System.out.println(ex.getMessage());
  30. JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
  31. //JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL");
  32. }
  33. }
  34.  
  35. } catch (SQLException e) {
  36. System.out.println(e.getMessage());
  37. JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
  38. //JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN EJECUTAR LAS SENTENCIAS SQL parte 2");
  39. } catch (ClassNotFoundException e) {
  40. System.out.println(e.getMessage());
  41. JOptionPane.showMessageDialog(null, "NO SE PUDO EFECTUAR LA SENTENCIA SQL", "Error", JOptionPane.ERROR_MESSAGE);
  42. //JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN EJECUTAR LAS SENTENCIAS SQL parte 3");
  43. }
  44. return conn;
  45. }
  46.  
  47. public Connection AccederBD() {
  48. try {
  49. //Obtenemos el Driver de Derby
  50. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  51. //Obtenemos la Conexión
  52. conn = DriverManager.getConnection("jdbc:derby:.\BD\nombrebasededatos.db");
  53. if (conn != null) {
  54. System.out.println("Base de Datos Ya Leida Correctamente");
  55. //JOptionPane.showMessageDialog(null, "Base de Datos Ya Leida Correctamente");
  56. }
  57. } catch (SQLException e) {
  58. System.out.println(e.getMessage());
  59. System.out.println("Sistema Creado por Mario José Echeverría");
  60. System.out.println("NO SE ENCONTRO LA BASE DE DATOS");
  61. System.out.println("CREANDO BASE DE DATOS EN DERBY DATABASE");
  62. String createTableProyecto = "Sentence to create first table";
  63. String createTablePrimer = "Sentence to create second table";
  64. String createTableTopCoat = "Sentence to create third table";
  65. String createTableCotizacion = "Sentence to create fourth table"
  66. CrearBD(createTableProyecto);
  67. CrearBD(createTablePrimer);
  68. CrearBD(createTableTopCoat);
  69. CrearBD(createTableCotizacion);
  70. //*************PRUEBAS*****************
  71. } catch (ClassNotFoundException e) {
  72. System.out.println(e.getMessage());
  73. System.out.println("ERROR DE TIPO ClassNotFoundException");
  74. //JOptionPane.showMessageDialog(null, "TRONO LA APLICACION EN ACCEDER A LA BASE DE DATOS parte 2");
  75. }
  76. return conn;
  77. }
  78.  
  79. public void UID(String sqlcad) {
  80. try {
  81. //Obtenemos el Driver de Derby
  82. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  83. conn = DriverManager.getConnection("jdbc:derby:.\BD\nombrebasededatos.db");
  84. sttm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  85. sttm.executeUpdate(sqlcad);
  86. System.out.println("Conexión Exitosa a la Base de Datos");
  87. //JOptionPane.showMessageDialog(null, "Conexión exitosa");
  88. sttm.close();
  89. conn.close();
  90. if (conn != null) {
  91. System.out.println("Consulta Realizada Correctamente");
  92. //JOptionPane.showMessageDialog(null, "Base de Datos Ya Leida Correctamente");
  93. }
  94. } catch (SQLException e) {
  95. System.out.println("Error= " + e.getMessage());
  96. } catch (ClassNotFoundException e) {
  97. System.out.println("Error= " + e.getMessage());
  98. }
  99. }
  100.  
  101. public ResultSet getvalores(String sqlcad) {
  102. ResultSet rs = null;
  103. try {
  104. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  105. conn = DriverManager.getConnection("jdbc:derby:.\BD\nombrebasededatos.db");
  106. sttm = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  107. //String sqlcad = "Select nombre, m2xgal, pregal, precub, descripcion from primer";
  108. rs = sttm.executeQuery(sqlcad);
  109. return rs;
  110. } catch (Exception e) {
  111. System.out.println("Error= " + e.getMessage());
  112. return rs;
  113. }
  114. }
  115.  
  116. NetworkServerControl server=new NetworkServerControl(InetAddress.getLocalHost(),1527);
  117. server.start (null);
  118. //Class.forName n DriverManager.getConnection() declarations goes here.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement