commit ec5ed69a37f49d26166f018a0ef3f1b8d631790a Author: Joan Moncho Date: Mon Nov 21 12:17:12 2022 +0100 commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..45eb208 --- /dev/null +++ b/.classpath @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..482e2fc --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Hoteles_BD + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /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=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +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=17 diff --git a/bin/Conexion.class b/bin/Conexion.class new file mode 100644 index 0000000..c40e3e2 Binary files /dev/null and b/bin/Conexion.class differ diff --git a/bin/Hotel.class b/bin/Hotel.class new file mode 100644 index 0000000..015420b Binary files /dev/null and b/bin/Hotel.class differ diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..37b4901 --- /dev/null +++ b/bin/README.md @@ -0,0 +1,5 @@ +# Ejercicio9 + +1. Primero creamos la conexion a la base de datos para que podamos modificar o añadir nuevos registros en la base de datos. +2. Luego usamos la funcion executeUpdate para insertar los datos a la base de datos. +3. Por ultimo usamos la funcion executeQuery (SELECT * FROM hotel) para que nos muestre la tabla. diff --git a/bin/VentanaHoteles$1.class b/bin/VentanaHoteles$1.class new file mode 100644 index 0000000..a3f87e0 Binary files /dev/null and b/bin/VentanaHoteles$1.class differ diff --git a/bin/VentanaHoteles$2.class b/bin/VentanaHoteles$2.class new file mode 100644 index 0000000..3297b3c Binary files /dev/null and b/bin/VentanaHoteles$2.class differ diff --git a/bin/VentanaHoteles.class b/bin/VentanaHoteles.class new file mode 100644 index 0000000..a2e7ace Binary files /dev/null and b/bin/VentanaHoteles.class differ diff --git a/src/Conexion.java b/src/Conexion.java new file mode 100644 index 0000000..4865644 --- /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 = "test"; + static String driver = "com.mysql.cj.jdbc.Driver"; + static String user = "root"; + static String pass = ""; + +} diff --git a/src/Hotel.java b/src/Hotel.java new file mode 100644 index 0000000..2a480d7 --- /dev/null +++ b/src/Hotel.java @@ -0,0 +1,49 @@ +import java.util.ArrayList; + +public class Hotel { + + public static ArrayList lista = new ArrayList(); + + private String nombre; + private String ciudad; + private int estrellas; + + + public Hotel(String nombre, String ciudad, int estrellas) { + this.nombre = nombre; + this.ciudad = ciudad; + this.estrellas = estrellas; + } + + @Override + public String toString() { + return nombre + ciudad + estrellas; + } + + public String datos() { + return "'" + this.nombre + "','" + + this.ciudad + "'," + + this.estrellas + "\n"; + } + + + public String getNombre() { + return nombre; + } + public void setNombre(String nombre) { + this.nombre = nombre; + } + public String getCiudad() { + return ciudad; + } + public void setCiudad(String ciudad) { + this.ciudad = ciudad; + } + public int getEstrellas() { + return estrellas; + } + public void setEstrellas(int estrellas) { + this.estrellas = estrellas; + } + +} diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..37b4901 --- /dev/null +++ b/src/README.md @@ -0,0 +1,5 @@ +# Ejercicio9 + +1. Primero creamos la conexion a la base de datos para que podamos modificar o añadir nuevos registros en la base de datos. +2. Luego usamos la funcion executeUpdate para insertar los datos a la base de datos. +3. Por ultimo usamos la funcion executeQuery (SELECT * FROM hotel) para que nos muestre la tabla. diff --git a/src/VentanaHoteles.java b/src/VentanaHoteles.java new file mode 100644 index 0000000..a4cf345 --- /dev/null +++ b/src/VentanaHoteles.java @@ -0,0 +1,207 @@ +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 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.JLabel; +import javax.swing.JButton; +import javax.swing.JTextField; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +public class VentanaHoteles extends JFrame { + + private JPanel contentPane; + private JTable hotel; + private JLabel lblNewLabel; + private JLabel lblNewLabel_1; + private JLabel lblNewLabel_2; + private JButton botoInsertar; + private JTextField casillaNombre; + private JTextField casillaCiudad; + private JTextField CasillaEstrellas; + + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + VentanaHoteles frame = new VentanaHoteles(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + + public VentanaHoteles() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 451); + 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, 412); + contentPane.add(panel); + panel.setLayout(null); + + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBounds(53, 48, 324, 151); + panel.add(scrollPane); + + hotel = new JTable(); + hotel.setModel(new DefaultTableModel( + new Object[][] { + }, + new String[] { + "ID", "Nombre", "Ciudad", "Estrellas" + } + )); + scrollPane.setViewportView(hotel); + + lblNewLabel = new JLabel("Nombre"); + lblNewLabel.setBounds(53, 233, 46, 14); + panel.add(lblNewLabel); + + lblNewLabel_1 = new JLabel("Ciudad"); + lblNewLabel_1.setBounds(53, 275, 46, 14); + panel.add(lblNewLabel_1); + + lblNewLabel_2 = new JLabel("Estrellas"); + lblNewLabel_2.setBounds(53, 323, 65, 14); + panel.add(lblNewLabel_2); + + botoInsertar = new JButton("Insertar"); + botoInsertar.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + + String Nombre = casillaNombre.getText(); + String Ciudad = casillaCiudad.getText(); + int Estrellas = Integer.parseInt(CasillaEstrellas.getText()); + + Hotel h = new Hotel(Nombre, Ciudad, Estrellas); + Hotel.lista.add(h); + + guarda_hotel(h); + + casillaNombre.setText(""); + casillaCiudad.setText(""); + CasillaEstrellas.setText(""); + + carga_Array(); + + //Hotel.lista.clear(); + + int numCols = hotel.getModel().getColumnCount(); + + Object[] fila = new Object[numCols]; + + for(int i=0; i