Browse Source

no funciona

master
Joan Moncho 2 years ago
commit
9af9ebc2ae
23 changed files with 585 additions and 0 deletions
  1. +11
    -0
      .classpath
  2. +17
    -0
      .project
  3. +14
    -0
      .settings/org.eclipse.jdt.core.prefs
  4. BIN
      bin/Coche.class
  5. BIN
      bin/Conexion.class
  6. BIN
      bin/EjemploJDBC.class
  7. BIN
      bin/Ventana$1.class
  8. BIN
      bin/Ventana$2.class
  9. BIN
      bin/Ventana.class
  10. BIN
      bin/Ventana2$1.class
  11. BIN
      bin/Ventana2$2.class
  12. BIN
      bin/Ventana2$3.class
  13. BIN
      bin/Ventana2$4.class
  14. BIN
      bin/Ventana2$5.class
  15. BIN
      bin/Ventana2$6.class
  16. BIN
      bin/Ventana2$7.class
  17. BIN
      bin/Ventana2$8.class
  18. BIN
      bin/Ventana2.class
  19. +86
    -0
      src/Coche.java
  20. +12
    -0
      src/Conexion.java
  21. +39
    -0
      src/EjemploJDBC.java
  22. +130
    -0
      src/Ventana.java
  23. +276
    -0
      src/Ventana2.java

+ 11
- 0
.classpath View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="C:/Users/Vesprada/Downloads/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20/mysql-connector-java-8.0.20.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Prueba_BaseDeDatos</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 14
- 0
.settings/org.eclipse.jdt.core.prefs View File

@ -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

BIN
bin/Coche.class View File


BIN
bin/Conexion.class View File


BIN
bin/EjemploJDBC.class View File


BIN
bin/Ventana$1.class View File


BIN
bin/Ventana$2.class View File


BIN
bin/Ventana.class View File


BIN
bin/Ventana2$1.class View File


BIN
bin/Ventana2$2.class View File


BIN
bin/Ventana2$3.class View File


BIN
bin/Ventana2$4.class View File


BIN
bin/Ventana2$5.class View File


BIN
bin/Ventana2$6.class View File


BIN
bin/Ventana2$7.class View File


BIN
bin/Ventana2$8.class View File


BIN
bin/Ventana2.class View File


+ 86
- 0
src/Coche.java View File

@ -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<Coche> arr = new ArrayList<Coche>();
//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;
}
}

+ 12
- 0
src/Conexion.java View File

@ -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 = "";
}

+ 39
- 0
src/EjemploJDBC.java View File

@ -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();
}
}
}

+ 130
- 0
src/Ventana.java View File

@ -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<Coche.arr.size();i++) {
//fila[0] = i;
fila[0]= Coche.arr.get(i).getMatricula();
fila[1]= Coche.arr.get(i).getMarca();
fila[2]= Coche.arr.get(i).getModelo();
fila[3]= Coche.arr.get(i).getColor();
fila[4]= Coche.arr.get(i).getKm();
((DefaultTableModel) table.getModel()).addRow(fila);
}
}
public static void carga_Array() {
try {
Class.forName(Conexion.driver);
Conexion.con=DriverManager.getConnection(Conexion.url + Conexion.db, Conexion.user, Conexion.pass);
Statement st = Conexion.con.createStatement();
//Coche c1 = new Coche("333CCC","Ferrari","F40","Rojo",99999);
//st.executeUpdate("INSERT INTO COCHE VALUES(" + c1.datos() + ")");
ResultSet res = st.executeQuery("SELECT * FROM coche");
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);
Coche c = new Coche(matricula, marca, modelo, color, km);
Coche.arr.add(c);
}
Conexion.con.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
}

+ 276
- 0
src/Ventana2.java View File

@ -0,0 +1,276 @@
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class Ventana2 extends JFrame {
private JPanel contentPane;
static JTextField casilla;
static JButton boton_FabricaCoche;
static JComboBox comboBox_Marca;
static JComboBox comboBox_Modelo;
static JComboBox comboBox_Color;
static JTextField textKm;
static JLabel lblTexto;
static JButton btnModificar;
static ArrayList<Coche> arr = new ArrayList<Coche>();
static ArrayList<Integer> numeroCoches = new ArrayList<Integer>(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;
}
}

Loading…
Cancel
Save