Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gestiondocumentos;
- /**
- *
- * @author Ana Calero
- */
- import javax.swing.JOptionPane;
- import java.sql.*;
- public class GestionDocumentos{
- static Statement stmt;
- static Connection con;
- public static void main(String args[]){
- int opcion = -1;
- do{
- try {
- opcion = getOpcion();
- if (opcion != 0){
- getSelected(opcion);
- }
- } catch(NumberFormatException e) {
- System.err.println("Número incorrecto: Introduzcalo de nuevo.");}
- }
- while ( opcion != 0);
- System.exit(0);
- }
- public static int getOpcion()
- {
- String opcion;
- int op;
- opcion = JOptionPane.showInputDialog(null,
- "1. Crear tabla Documentos\n"+
- "2. Borrar tabla Documentos\n"+
- "3. Listar datos de la tabla Documentos\n"+
- "4. Insertar datos en la tabla Documentos\n"+
- "5. Borrar datos en la tabla Documentoss\n"+
- "0. Salir \n"+
- "Escriba la opción.");
- op = Integer.parseInt(opcion);
- return op;
- }
- public static void getSelected(int opcion){
- if (opcion==1)
- crearTDocumentos();
- if(opcion==2){
- borrarTDocumentos();
- }
- if(opcion==3){
- listarTDocumentos();
- }
- if(opcion==3){
- listarTDocumentos();
- }
- if(opcion==4){
- //insertarDocumentos();
- }
- if(opcion==6){
- //borrarDatosDocumentos();
- }
- }
- public static Connection getConnection()
- {
- try {
- // Cargar el driver de mysql
- Class.forName("com.mysql.jdbc.Driver");
- } catch(java.lang.ClassNotFoundException e) {
- System.err.print("ClassNotFoundException: ");
- System.err.println(e.getMessage());
- }
- try {
- // Cadena de conexión para conectar con MySQL en localhost,
- //seleccionar la base de datos llamada ‘test’
- // con usuario y contraseña del servidor de MySQL: root y admin
- String connectionUrl = "jdbc:mysql://localhost/test?" +
- "user=root&password=admin";
- // Obtener la conexión
- con = DriverManager.getConnection(connectionUrl);
- } catch(SQLException ex) {
- System.err.println("SQLException: " + ex.getMessage());
- }
- return con;
- }
- public static void crearTDocumentos()
- {
- getConnection();
- String createString;
- createString = "CREATE TABLE Documentos (id_documento int(3), "
- + "nombre TEXT(25), "
- + "ubicacion TEXT(15), "
- + "fecha (date) "
- + "Constraint Documento_PK Primary Key (id_documento))";
- try {
- stmt = con.createStatement();
- stmt.executeUpdate(createString);
- stmt.close();
- con.close();
- JOptionPane.showMessageDialog(null,"Tabla Documentos creada.");
- } catch(SQLException ex) {
- System.err.println("SQLException: " + ex.getMessage());
- if (ex.getErrorCode()!=1050)
- JOptionPane.showMessageDialog(null,"Tabla de Documentos no creada.");
- else
- JOptionPane.showMessageDialog(null,"La tabla Documentos ya existe.");
- }
- }
- public static void borrarTDocumentos()
- {
- con = getConnection();
- String createString;
- createString = "DROP Documentos if exists Documentos";
- try {
- stmt = con.createStatement();
- stmt.close();
- con.close();
- JOptionPane.showMessageDialog(null,"Tabla Documentos eliminada");
- } catch(SQLException ex) {
- System.err.println("SQLException: " + ex.getMessage());
- JOptionPane.showMessageDialog(null,"La tabla Documentos no existe");
- }
- }
- public static void listarTDocumentos(){
- con = getConnection();
- String result = null;
- String selectString;
- selectString = "select * from Documentos" ;
- result ="id_documento \t \t Tipo \t \t Num_intervinientes \t Nom_fichero \n";
- try {
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(selectString);
- while (rs.next()) {
- int pr_id = rs.getInt("id_Documento");
- String prodName = rs.getString("tipo");
- String nomf = rs.getString("nom_fich");
- int id = rs.getInt("num_interv");
- result += pr_id + "\t \t"+ prodName + "\t \t" + id + "\t \t" + nomf + "\n";
- }
- stmt.close();
- con.close();
- } catch(SQLException ex) {
- System.err.println("SQLException: " + ex.getMessage());
- }
- JOptionPane.showMessageDialog(null, result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement