Advertisement
Guest User

Untitled

a guest
May 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. package gestiondocumentos;
  2.  
  3. /**
  4. *
  5. * @author Ana Calero
  6. */
  7. import javax.swing.JOptionPane;
  8. import java.sql.*;
  9.  
  10. public class GestionDocumentos{
  11. static Statement stmt;
  12. static Connection con;
  13.  
  14. public static void main(String args[]){
  15.  
  16. int opcion = -1;
  17.  
  18. do{
  19. try {
  20. opcion = getOpcion();
  21. if (opcion != 0){
  22. getSelected(opcion);
  23. }
  24. } catch(NumberFormatException e) {
  25. System.err.println("Número incorrecto: Introduzcalo de nuevo.");}
  26. }
  27. while ( opcion != 0);
  28. System.exit(0);
  29.  
  30.  
  31. }
  32.  
  33. public static int getOpcion()
  34. {
  35. String opcion;
  36. int op;
  37. opcion = JOptionPane.showInputDialog(null,
  38. "1. Crear tabla Documentos\n"+
  39. "2. Borrar tabla Documentos\n"+
  40. "3. Listar datos de la tabla Documentos\n"+
  41. "4. Insertar datos en la tabla Documentos\n"+
  42. "5. Borrar datos en la tabla Documentoss\n"+
  43. "0. Salir \n"+
  44. "Escriba la opción.");
  45. op = Integer.parseInt(opcion);
  46. return op;
  47. }
  48.  
  49. public static void getSelected(int opcion){
  50. if (opcion==1)
  51. crearTDocumentos();
  52. if(opcion==2){
  53. borrarTDocumentos();
  54. }
  55. if(opcion==3){
  56. listarTDocumentos();
  57. }
  58. if(opcion==3){
  59. listarTDocumentos();
  60. }
  61.  
  62. if(opcion==4){
  63. //insertarDocumentos();
  64. }
  65.  
  66. if(opcion==6){
  67. //borrarDatosDocumentos();
  68. }
  69.  
  70. }
  71.  
  72. public static Connection getConnection()
  73. {
  74.  
  75. try {
  76. // Cargar el driver de mysql
  77. Class.forName("com.mysql.jdbc.Driver");
  78.  
  79. } catch(java.lang.ClassNotFoundException e) {
  80. System.err.print("ClassNotFoundException: ");
  81. System.err.println(e.getMessage());
  82. }
  83.  
  84. try {
  85. // Cadena de conexión para conectar con MySQL en localhost,
  86. //seleccionar la base de datos llamada ‘test’
  87. // con usuario y contraseña del servidor de MySQL: root y admin
  88. String connectionUrl = "jdbc:mysql://localhost/test?" +
  89. "user=root&password=admin";
  90. // Obtener la conexión
  91. con = DriverManager.getConnection(connectionUrl);
  92.  
  93. } catch(SQLException ex) {
  94. System.err.println("SQLException: " + ex.getMessage());
  95. }
  96.  
  97. return con;
  98. }
  99.  
  100. public static void crearTDocumentos()
  101. {
  102. getConnection();
  103. String createString;
  104. createString = "CREATE TABLE Documentos (id_documento int(3), "
  105. + "nombre TEXT(25), "
  106. + "ubicacion TEXT(15), "
  107. + "fecha (date) "
  108. + "Constraint Documento_PK Primary Key (id_documento))";
  109.  
  110. try {
  111. stmt = con.createStatement();
  112. stmt.executeUpdate(createString);
  113. stmt.close();
  114. con.close();
  115. JOptionPane.showMessageDialog(null,"Tabla Documentos creada.");
  116. } catch(SQLException ex) {
  117. System.err.println("SQLException: " + ex.getMessage());
  118. if (ex.getErrorCode()!=1050)
  119. JOptionPane.showMessageDialog(null,"Tabla de Documentos no creada.");
  120. else
  121. JOptionPane.showMessageDialog(null,"La tabla Documentos ya existe.");
  122.  
  123. }
  124. }
  125.  
  126.  
  127.  
  128. public static void borrarTDocumentos()
  129. {
  130. con = getConnection();
  131. String createString;
  132. createString = "DROP Documentos if exists Documentos";
  133.  
  134. try {
  135. stmt = con.createStatement();
  136. stmt.close();
  137. con.close();
  138. JOptionPane.showMessageDialog(null,"Tabla Documentos eliminada");
  139. } catch(SQLException ex) {
  140. System.err.println("SQLException: " + ex.getMessage());
  141. JOptionPane.showMessageDialog(null,"La tabla Documentos no existe");
  142. }
  143.  
  144. }
  145.  
  146.  
  147. public static void listarTDocumentos(){
  148.  
  149. con = getConnection();
  150. String result = null;
  151. String selectString;
  152. selectString = "select * from Documentos" ;
  153. result ="id_documento \t \t Tipo \t \t Num_intervinientes \t Nom_fichero \n";
  154. try {
  155. stmt = con.createStatement();
  156. ResultSet rs = stmt.executeQuery(selectString);
  157. while (rs.next()) {
  158. int pr_id = rs.getInt("id_Documento");
  159. String prodName = rs.getString("tipo");
  160. String nomf = rs.getString("nom_fich");
  161. int id = rs.getInt("num_interv");
  162. result += pr_id + "\t \t"+ prodName + "\t \t" + id + "\t \t" + nomf + "\n";
  163. }
  164. stmt.close();
  165. con.close();
  166.  
  167. } catch(SQLException ex) {
  168. System.err.println("SQLException: " + ex.getMessage());
  169. }
  170. JOptionPane.showMessageDialog(null, result);
  171. }
  172.  
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement