commit 9af9ebc2ae1cd08151532771ebbd24cd325ee22f Author: Joan Moncho Date: Fri Oct 14 18:00:58 2022 +0200 no funciona diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..14a2c62 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..dffc690 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Prueba_BaseDeDatos + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8aabe8d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=16 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=16 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=16 diff --git a/bin/Coche.class b/bin/Coche.class new file mode 100644 index 0000000..9877bf2 Binary files /dev/null and b/bin/Coche.class differ diff --git a/bin/Conexion.class b/bin/Conexion.class new file mode 100644 index 0000000..32d6ae6 Binary files /dev/null and b/bin/Conexion.class differ diff --git a/bin/EjemploJDBC.class b/bin/EjemploJDBC.class new file mode 100644 index 0000000..3780cfd Binary files /dev/null and b/bin/EjemploJDBC.class differ diff --git a/bin/Ventana$1.class b/bin/Ventana$1.class new file mode 100644 index 0000000..cfc2667 Binary files /dev/null and b/bin/Ventana$1.class differ diff --git a/bin/Ventana$2.class b/bin/Ventana$2.class new file mode 100644 index 0000000..f3716bc Binary files /dev/null and b/bin/Ventana$2.class differ diff --git a/bin/Ventana.class b/bin/Ventana.class new file mode 100644 index 0000000..efaf80a Binary files /dev/null and b/bin/Ventana.class differ diff --git a/bin/Ventana2$1.class b/bin/Ventana2$1.class new file mode 100644 index 0000000..8634a62 Binary files /dev/null and b/bin/Ventana2$1.class differ diff --git a/bin/Ventana2$2.class b/bin/Ventana2$2.class new file mode 100644 index 0000000..aa6fef5 Binary files /dev/null and b/bin/Ventana2$2.class differ diff --git a/bin/Ventana2$3.class b/bin/Ventana2$3.class new file mode 100644 index 0000000..d7ecf71 Binary files /dev/null and b/bin/Ventana2$3.class differ diff --git a/bin/Ventana2$4.class b/bin/Ventana2$4.class new file mode 100644 index 0000000..71f0a26 Binary files /dev/null and b/bin/Ventana2$4.class differ diff --git a/bin/Ventana2$5.class b/bin/Ventana2$5.class new file mode 100644 index 0000000..42639a6 Binary files /dev/null and b/bin/Ventana2$5.class differ diff --git a/bin/Ventana2$6.class b/bin/Ventana2$6.class new file mode 100644 index 0000000..8625b55 Binary files /dev/null and b/bin/Ventana2$6.class differ diff --git a/bin/Ventana2$7.class b/bin/Ventana2$7.class new file mode 100644 index 0000000..25db439 Binary files /dev/null and b/bin/Ventana2$7.class differ diff --git a/bin/Ventana2$8.class b/bin/Ventana2$8.class new file mode 100644 index 0000000..451e06b Binary files /dev/null and b/bin/Ventana2$8.class differ diff --git a/bin/Ventana2.class b/bin/Ventana2.class new file mode 100644 index 0000000..4c6c3ed Binary files /dev/null and b/bin/Ventana2.class differ diff --git a/src/Coche.java b/src/Coche.java new file mode 100644 index 0000000..f5d06df --- /dev/null +++ b/src/Coche.java @@ -0,0 +1,86 @@ +import java.util.ArrayList; + +public class Coche { + + private String matricula; + private String marca; + private String modelo; + private String color; + private double km; + static ArrayList arr = new ArrayList(); + //public static String[] marca = {"Ferrari", "Ford", "Lamborghini", "Audi", "Seat", "Mercedes", "Volkswagen"}; + //public static String[] modelo = {"3P", "AX", "4P", "8P", "AX"}; + //public static String[] color = {"Rojo", "Blanco", "Azul", "Verde", "Negro"}; + + + public Coche(String matricula, String marca, String modelo, String color, double km) { + this.matricula = matricula; + this.marca = marca; + this.modelo = modelo; + this.color = color; + this.km = km; + } + + @Override + public String toString() { + return matricula + " " + marca + " " + modelo + " " + color + " " + km; + } + + public String modificar(String matricula, String marca, String modelo, String color, int km) { + this.matricula=matricula; + this.marca=marca; + this.modelo=modelo; + this.color=color; + this.km=km; + return "\n"; + } + + public String datos() { + return "'" + this.matricula + "','" + + this.marca + "','" + + this.modelo + "','" + + this.color + "'," + + this.km + "\n"; + } + + public String getMatricula() { + return matricula; + } + + public void setMatricula(String matricula) { + this.matricula = matricula; + } + + public String getMarca() { + return marca; + } + + public void setMarca(String marca) { + this.marca = marca; + } + + public String getModelo() { + return modelo; + } + + public void setModelo(String modelo) { + this.modelo = modelo; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public double getKm() { + return km; + } + + public void setKm(int km) { + this.km = km; + } + +} diff --git a/src/Conexion.java b/src/Conexion.java new file mode 100644 index 0000000..660ffb2 --- /dev/null +++ b/src/Conexion.java @@ -0,0 +1,12 @@ +import java.sql.Connection; + +public class Conexion { + + static Connection con=null; + static String url = "jdbc:mysql://localhost:3306/"; + static String db = "parque"; + static String driver = "com.mysql.cj.jdbc.Driver"; + static String user = "root"; + static String pass = ""; + +} diff --git a/src/EjemploJDBC.java b/src/EjemploJDBC.java new file mode 100644 index 0000000..9ff1289 --- /dev/null +++ b/src/EjemploJDBC.java @@ -0,0 +1,39 @@ +import java.sql.*; + +public class EjemploJDBC { + + public static void main(String[] args) { + + System.out.println("Obteniendo registros de una tabla...."); + + System.out.println("\nMATRICULA MARCA MODELO COLOR KM"); + + try { + Class.forName(Conexion.driver); + Conexion.con=DriverManager.getConnection(Conexion.url+Conexion.db, Conexion.user, Conexion.pass); + try { + Statement st = Conexion.con.createStatement(); + ResultSet res = st.executeQuery("SELECT * FROM coche"); + System.out.println(""); + while(res.next()) { + String matricula = res.getString("matricula"); + String marca = res.getString("marca"); + String modelo = res.getString("modelo"); + String color = res.getString("color"); + double km=res.getDouble("km"); + + System.out.println(matricula + "\t\t" + marca + "\t\t" + modelo + "\t\t" + color + "\t\t" + km); + } + + }catch (Exception e) { + e.printStackTrace(); + } + Conexion.con.close(); + }catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/src/Ventana.java b/src/Ventana.java new file mode 100644 index 0000000..6389f7f --- /dev/null +++ b/src/Ventana.java @@ -0,0 +1,130 @@ +import java.awt.BorderLayout; +import java.awt.EventQueue; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.table.DefaultTableModel; +import javax.swing.JButton; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +public class Ventana extends JFrame { + + private JPanel contentPane; + static JTable table; + + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Ventana frame = new Ventana(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + public Ventana() { + setTitle("Tabla"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(null); + + JPanel panel = new JPanel(); + panel.setBounds(0, 0, 434, 261); + contentPane.add(panel); + panel.setLayout(null); + + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBounds(10, 50, 414, 159); + panel.add(scrollPane); + + table = new JTable(); + table.setModel(new DefaultTableModel( + new Object[][] { + }, + new String[] { + "Matricula", "Marca", "Modelo", "Color", "Km" + } + )); + scrollPane.setViewportView(table); + + JButton btnCrearCoche = new JButton("Crear Coche"); + btnCrearCoche.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Ventana2 frame = new Ventana2(); + frame.setVisible(true); + dispose(); + } + }); + btnCrearCoche.setBounds(297, 11, 127, 23); + panel.add(btnCrearCoche); + + Coche.arr.clear(); + + carga_Array(); + + int numCols = table.getModel().getColumnCount(); + + Object[] fila = new Object[numCols]; + + for(int i=0; i arr = new ArrayList(); + static ArrayList numeroCoches = new ArrayList(5); + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Ventana2 frame = new Ventana2(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + public Ventana2() { + addWindowListener((WindowListener) new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent e) { + + //Ventana vmenu = new Ventana(); + //vmenu.setVisible(true); + + } + }); + setTitle("CREACION DE COCHE"); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + setBounds(100, 100, 450, 332); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(null); + + JLabel lblNewLabel = new JLabel("Matricula"); + lblNewLabel.setBounds(27, 97, 63, 14); + contentPane.add(lblNewLabel); + + casilla = new JTextField(); + casilla.setColumns(10); + casilla.setBounds(100, 94, 113, 20); + contentPane.add(casilla); + + JLabel lblNewLabel_1 = new JLabel("Marca"); + lblNewLabel_1.setBounds(27, 129, 46, 14); + contentPane.add(lblNewLabel_1); + + JLabel lblNewLabel_2 = new JLabel("Modelo"); + lblNewLabel_2.setBounds(27, 162, 46, 14); + contentPane.add(lblNewLabel_2); + + JLabel lblNewLabel_3 = new JLabel("Color"); + lblNewLabel_3.setBounds(27, 195, 46, 14); + contentPane.add(lblNewLabel_3); + + comboBox_Marca = new JComboBox(); + comboBox_Marca.setSelectedIndex(-1); + comboBox_Marca.setBounds(100, 125, 113, 22); + contentPane.add(comboBox_Marca); + comboBox_Marca.setModel(new DefaultComboBoxModel(new String[] {"Ferrari", "Ford", "Lamborghini", "Audi", "Seat", "Mercedes", "Volkswagen"})); + comboBox_Marca.setSelectedIndex(-1); + + comboBox_Modelo = new JComboBox(); + comboBox_Modelo.setSelectedIndex(-1); + comboBox_Modelo.setBounds(100, 158, 113, 22); + contentPane.add(comboBox_Modelo); + comboBox_Modelo.setModel(new DefaultComboBoxModel(new String[] {"3P", "AX", "4P", "8P", "AX"})); + comboBox_Modelo.setSelectedIndex(-1); + + comboBox_Color = new JComboBox(); + comboBox_Color.setSelectedIndex(-1); + comboBox_Color.setBounds(100, 191, 113, 22); + contentPane.add(comboBox_Color); + comboBox_Color.setModel(new DefaultComboBoxModel(new String[] {"Rojo", "Blanco", "Azul", "Verde", "Negro"})); + comboBox_Color.setSelectedIndex(-1); + + JButton boton_Aleatorio = new JButton("Random"); + boton_Aleatorio.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + casilla.setText(matAleatoria()); + } + }); + boton_Aleatorio.setBounds(243, 93, 84, 23); + contentPane.add(boton_Aleatorio); + + JPanel panel = new JPanel(); + panel.setBounds(0, 0, 434, 293); + contentPane.add(panel); + panel.setLayout(null); + + JLabel lblNewLabel_3_1 = new JLabel("Kilometros"); + lblNewLabel_3_1.setBounds(23, 227, 70, 14); + panel.add(lblNewLabel_3_1); + + textKm = new JTextField(); + textKm.setColumns(10); + textKm.setBounds(103, 224, 113, 20); + panel.add(textKm); + + boton_FabricaCoche = new JButton("Fabrica coche"); + boton_FabricaCoche.setBackground(Color.GRAY); + boton_FabricaCoche.setBounds(246, 166, 178, 38); + panel.add(boton_FabricaCoche); + + JButton botonRandomKm = new JButton("Random km"); + botonRandomKm.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String n = ""; + n = n + ((int)Math.floor(Math.random()*(10000-0+1) + 0)); + textKm.setText(n); + } + }); + botonRandomKm.setBounds(246, 223, 107, 23); + panel.add(botonRandomKm); + + lblTexto = new JLabel(""); + lblTexto.setBounds(32, 268, 392, 14); + panel.add(lblTexto); + + JButton boton_AleatorioAll = new JButton("Random All"); + boton_AleatorioAll.setBounds(246, 132, 107, 23); + panel.add(boton_AleatorioAll); + + btnModificar = new JButton("Modificar"); + btnModificar.setEnabled(false); + btnModificar.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + String Marca = (String)comboBox_Marca.getSelectedItem(); + String Modelo = (String)comboBox_Modelo.getSelectedItem(); + String Color = (String)comboBox_Color.getSelectedItem(); + int Km = Integer.parseInt(textKm.getText()); + + Coche c = arr.get(Ventana.table.getSelectedRow()); + c.modificar(casilla.getText(), Marca, Modelo, Color, Km); + + } + }); + btnModificar.setBounds(335, 93, 89, 23); + panel.add(btnModificar); + + JButton botonListaCoches = new JButton("MOSTRAR TABLA"); + botonListaCoches.setBounds(138, 21, 168, 47); + panel.add(botonListaCoches); + botonListaCoches.setForeground(Color.WHITE); + botonListaCoches.setBackground(Color.DARK_GRAY); + botonListaCoches.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Ventana frame = new Ventana(); + frame.setVisible(true); + //Navegador.arr.add(frame); + + dispose(); + } + }); + + boton_AleatorioAll.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int random1=(int)(Math.random()*(6-0+1)+0); + int random2=(int)(Math.random()*(4-0+1)+0); + int random3=(int)(Math.random()*(4-0+1)+0); + + comboBox_Marca.setSelectedIndex(random1); + comboBox_Modelo.setSelectedIndex(random2); + comboBox_Color.setSelectedIndex(random3); + + casilla.setText(matAleatoria()); + + String n = ""; + n = n + ((int)Math.floor(Math.random()*(10000-0+1) + 0)); + textKm.setText(n); + } + }); + boton_FabricaCoche.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + String Marca = (String)comboBox_Marca.getSelectedItem(); + String Modelo = (String)comboBox_Modelo.getSelectedItem(); + String Color = (String)comboBox_Color.getSelectedItem(); + int Km = Integer.parseInt(textKm.getText()); + + lblTexto.setVisible(true); + lblTexto.setText("Se ha fabricado un " + Marca); + + Coche c = new Coche(casilla.getText(), Marca, Modelo, Color, Km); + arr.add(c); + + guarda_coche(c); + + } + }); + } + + public static void guarda_coche(Coche c) { + + try { + Class.forName(Conexion.driver); + Conexion.con=DriverManager.getConnection(Conexion.url + Conexion.db, Conexion.user, Conexion.pass); + + Statement st = Conexion.con.createStatement(); + + st.executeUpdate("INSERT INTO COCHE VALUES(" + c.datos() + ")"); + + Conexion.con.close(); + }catch (ClassNotFoundException e1) { + e1.printStackTrace(); + }catch (SQLException e1) { + e1.printStackTrace(); + } + + } + + + public static String matAleatoria() { + String matricula = ""; + String letras = "BCDFGHJKLMNPQRSTVWXYZ"; + + for(int i=0; i<4; i++) { + matricula = matricula + ((int)Math.floor(Math.random()*(9-0+1) + 0)); + } + + matricula = matricula + "-"; + + for(int i=0;i<3; i++) { + matricula = matricula + "" + letras.charAt(((int)Math.floor(Math.random()*(20-0+1) + 0))); + } + + return matricula; + } +}