Browse Source

hoteles

master
Joan Moncho 2 years ago
commit
9e5564529f
11 changed files with 309 additions and 0 deletions
  1. +11
    -0
      .classpath
  2. +17
    -0
      .project
  3. +14
    -0
      .settings/org.eclipse.jdt.core.prefs
  4. BIN
      bin/Conexion.class
  5. BIN
      bin/Hotel.class
  6. BIN
      bin/VentanaHoteles$1.class
  7. BIN
      bin/VentanaHoteles$2.class
  8. BIN
      bin/VentanaHoteles.class
  9. +12
    -0
      src/Conexion.java
  10. +49
    -0
      src/Hotel.java
  11. +206
    -0
      src/VentanaHoteles.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>Hoteles_DB</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/Conexion.class View File


BIN
bin/Hotel.class View File


BIN
bin/VentanaHoteles$1.class View File


BIN
bin/VentanaHoteles$2.class View File


BIN
bin/VentanaHoteles.class View File


+ 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 = "alojamientos";
static String driver = "com.mysql.cj.jdbc.Driver";
static String user = "root";
static String pass = "";
}

+ 49
- 0
src/Hotel.java View File

@ -0,0 +1,49 @@
import java.util.ArrayList;
public class Hotel {
public static ArrayList<Hotel> lista = new ArrayList<Hotel>();
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;
}
}

+ 206
- 0
src/VentanaHoteles.java View File

@ -0,0 +1,206 @@
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 = casillaNombre.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("");
Hotel.lista.clear();
carga_Array();
int numCols = hotel.getModel().getColumnCount();
Object[] fila = new Object[numCols];
for(int i=0; i<Hotel.lista.size();i++) {
fila[0] = i;
fila[1]= Hotel.lista.get(i).getNombre();
fila[2]= Hotel.lista.get(i).getCiudad();
fila[3]= Hotel.lista.get(i).getEstrellas();
((DefaultTableModel) hotel.getModel()).addRow(fila);
}
}
});
botoInsertar.setBounds(53, 373, 89, 23);
panel.add(botoInsertar);
casillaNombre = new JTextField();
casillaNombre.setBounds(128, 230, 86, 20);
panel.add(casillaNombre);
casillaNombre.setColumns(10);
casillaCiudad = new JTextField();
casillaCiudad.setBounds(128, 272, 86, 20);
panel.add(casillaCiudad);
casillaCiudad.setColumns(10);
CasillaEstrellas = new JTextField();
CasillaEstrellas.setBounds(128, 320, 86, 20);
panel.add(CasillaEstrellas);
CasillaEstrellas.setColumns(10);
Hotel.lista.clear();
carga_Array();
int numCols = hotel.getModel().getColumnCount();
Object[] fila = new Object[numCols];
for(int i=0; i<Hotel.lista.size();i++) {
fila[0] = i;
fila[1]= Hotel.lista.get(i).getNombre();
fila[2]= Hotel.lista.get(i).getCiudad();
fila[3]= Hotel.lista.get(i).getEstrellas();
((DefaultTableModel) hotel.getModel()).addRow(fila);
}
}
public static void guarda_hotel(Hotel h) {
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 HOTEL VALUES(DEFAULT," + h.datos() + ")");
Conexion.con.close();
}catch (ClassNotFoundException e1) {
e1.printStackTrace();
}catch (SQLException e1) {
e1.printStackTrace();
}
}
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 hotel");
while(res.next()) {
String nombre = res.getString("Nombre");
String ciudad = res.getString("Ciudad");
int estrellas=res.getInt("Estrellas");
//System.out.println(matricula + "\t\t" + marca + "\t\t" + modelo + "\t\t" + color + "\t\t" + km);
Hotel h = new Hotel(nombre, ciudad, estrellas);
Hotel.lista.add(h);
}
Conexion.con.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
}

Loading…
Cancel
Save