Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Clientes
- {
- private static Scanner teclado = new Scanner(System.in);
- private static String db_host = "178.63.95.145";
- private static String db_user = "minecraft";
- private static String db_pass = "2030xdc9h";
- private static String db_name = "clientes";
- private static Connection conexion;
- private static Statement st;
- public static void main(String[] args)
- {
- conectar();
- crearTablasDB();
- registrarClientes();
- }
- private static void conectar()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conexion = DriverManager.getConnection("jdbc:mysql://"+db_host+"/"+db_name,db_user, db_pass);
- st = conexion.createStatement();
- System.out.println("Conexión establecida con éxito.");
- }
- catch (Exception ex)
- {
- System.out.println("No hemos podido conectar con MySQL: "+ ex.getMessage());
- }
- }
- private static void crearTablasDB()
- {
- try
- {
- 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;");
- System.out.println("Tabla creada en caso de no existir.");
- }
- catch (SQLException e)
- {
- System.out.println("No se ha podido crear la tabla: "+e.getMessage());
- }
- }
- private static void registrarClientes()
- {
- String nombreCliente;
- String dniCliente;
- String telefonoCliente;
- String domicilioCliente;
- System.out.println("");
- System.out.print("Introduzca el nombre del cliente: ");
- nombreCliente = teclado.nextLine();
- System.out.print("Introduzca el DNI de " + nombreCliente + ": ");
- dniCliente = teclado.nextLine();
- System.out.print("Introduzca el teléfono de " + nombreCliente + ": ");
- telefonoCliente = teclado.nextLine();
- System.out.print("Introduzca el domicilio de " + nombreCliente + ": ");
- domicilioCliente = teclado.nextLine();
- System.out.println("");
- try
- {
- st.execute("INSERT INTO registro VALUES ( '"+ nombreCliente +"', '"+ dniCliente +"', '"+ telefonoCliente +"', '"+ domicilioCliente +"' );");
- System.out.println("Usuario guradado con éxito.");
- }
- catch (Exception e)
- {
- System.out.println("No se han podido guardar los valores en la base de datos :"+e.getMessage());
- }
- registrarMasUsuarios();
- }
- public static void registrarMasUsuarios()
- {
- String respuesta;
- System.out.print("¿Desea registrar más clientes?: ");
- respuesta = teclado.nextLine();
- respuesta = respuesta.toLowerCase();
- if (respuesta.contains("si"))
- {
- registrarClientes();
- }
- else
- {
- cerrarConexión();
- }
- }
- public static void cerrarConexión()
- {
- try
- {
- st.close();
- System.out.println("Conexión cerrada.");
- }
- catch (Exception e)
- {
- System.out.println("Ha ocurrido un error al cerrar sesión con la base de datos: "+e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement