Advertisement
Guest User

Terminado

a guest
Sep 4th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.util.Scanner;
  6.  
  7. public class Clientes
  8. {
  9.    
  10.     private static Scanner teclado = new Scanner(System.in);
  11.    
  12.     private static String db_host = "178.63.95.145";
  13.     private static String db_user = "minecraft";
  14.     private static String db_pass = "2030xdc9h";
  15.     private static String db_name = "clientes";
  16.    
  17.     private static Connection conexion;
  18.     private static Statement st;
  19.    
  20.     public static void main(String[] args)
  21.     {
  22.         conectar();
  23.        
  24.         crearTablasDB();
  25.        
  26.         registrarClientes();
  27.        
  28.     }
  29.    
  30.     private static void conectar()
  31.     {
  32.         try
  33.         {
  34.             Class.forName("com.mysql.jdbc.Driver");
  35.             conexion = DriverManager.getConnection("jdbc:mysql://"+db_host+"/"+db_name,db_user, db_pass);
  36.             st = conexion.createStatement();
  37.             System.out.println("Conexión establecida con éxito.");
  38.         }
  39.         catch (Exception ex)
  40.         {
  41.             System.out.println("No hemos podido conectar con MySQL: "+ ex.getMessage());
  42.         }
  43.     }
  44.    
  45.     private static void crearTablasDB()
  46.     {
  47.         try
  48.         {
  49.             st.execute("CREATE TABLE IF NOT EXISTS registro ( nombre VARCHAR(50) NOT NULL, dni VARCHAR(9) NOT NULL, telefono VARCHAR(10) NOT NULL, domicilio VARCHAR(50) NOT NULL ) ENGINE=InnoDB;");
  50.             System.out.println("Tabla creada en caso de no existir.");
  51.         }
  52.         catch (SQLException e)
  53.         {
  54.             System.out.println("No se ha podido crear la tabla: "+e.getMessage());
  55.         }
  56.     }
  57.    
  58.     private static void registrarClientes()
  59.     {
  60.         String nombreCliente;
  61.         String dniCliente;
  62.         String telefonoCliente;
  63.         String domicilioCliente;
  64.        
  65.         System.out.println("");
  66.         System.out.print("Introduzca el nombre del cliente: ");
  67.         nombreCliente = teclado.nextLine();
  68.         System.out.print("Introduzca el DNI de " + nombreCliente + ": ");
  69.         dniCliente = teclado.nextLine();
  70.         System.out.print("Introduzca el teléfono de " + nombreCliente + ": ");
  71.         telefonoCliente = teclado.nextLine();
  72.         System.out.print("Introduzca el domicilio de " + nombreCliente + ": ");
  73.         domicilioCliente = teclado.nextLine();
  74.        
  75.         System.out.println("");
  76.        
  77.         try
  78.         {
  79.             st.execute("INSERT INTO registro VALUES ( '"+ nombreCliente +"', '"+ dniCliente +"', '"+ telefonoCliente +"', '"+ domicilioCliente +"' );");
  80.             System.out.println("Usuario guradado con éxito.");
  81.            
  82.         }
  83.         catch (Exception e)
  84.         {
  85.             System.out.println("No se han podido guardar los valores en la base de datos :"+e.getMessage());
  86.         }
  87.        
  88.         registrarMasUsuarios();
  89.        
  90.     }
  91.    
  92.     public static void registrarMasUsuarios()
  93.     {
  94.         String respuesta;
  95.         System.out.print("¿Desea registrar más clientes?: ");
  96.         respuesta = teclado.nextLine();
  97.         respuesta = respuesta.toLowerCase();
  98.        
  99.         if (respuesta.contains("si"))
  100.         {
  101.             registrarClientes();
  102.         }
  103.         else
  104.         {
  105.             cerrarConexión();
  106.         }
  107.     }
  108.    
  109.     public static void cerrarConexión()
  110.     {
  111.         try
  112.         {
  113.             st.close();
  114.             System.out.println("Conexión cerrada.");
  115.         }
  116.         catch (Exception e)
  117.         {
  118.             System.out.println("Ha ocurrido un error al cerrar sesión con la base de datos: "+e.getMessage());
  119.         }
  120.     }
  121.    
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement