commit 7933a8aff7c179c7363ead3aef2df0d0b83f052d Author: Arklightning Date: Sun May 14 13:05:00 2023 +0400 Lab4 comm diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..9a33201 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project JavaApplication1. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..f5b0a60 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1771 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..79f9dd7 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=3569ad16 +build.xml.script.CRC32=360ae6d5 +build.xml.stylesheet.CRC32=f85dc8f2@1.106.0.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=3569ad16 +nbproject/build-impl.xml.script.CRC32=53035f8f +nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.106.0.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..3dd870e --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,98 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=JavaApplication1 +application.vendor=oleg +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.modulepath=\ + ${run.modulepath} +debug.test.classpath=\ + ${run.test.classpath} +debug.test.modulepath=\ + ${run.test.modulepath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/JavaApplication1.jar +dist.javadoc.dir=${dist.dir}/javadoc +dist.jlink.dir=${dist.dir}/jlink +dist.jlink.output=${dist.jlink.dir}/JavaApplication1 +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath=\ + ${libs.PostgreSQLDriver.classpath} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.modulepath= +javac.processormodulepath= +javac.processorpath=\ + ${javac.classpath} +javac.source=18 +javac.target=18 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.modulepath=\ + ${javac.modulepath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.html5=false +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +# The jlink additional root modules to resolve +jlink.additionalmodules= +# The jlink additional command line parameters +jlink.additionalparam= +jlink.launcher=true +jlink.launcher.name=JavaApplication1 +main.class=javaapplication1.MainFrame +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.modulepath=\ + ${javac.modulepath} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +run.test.modulepath=\ + ${javac.test.modulepath} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..b466dd5 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + JavaApplication1 + + + + + + + + + diff --git a/src/javaapplication1/DBConnection.java b/src/javaapplication1/DBConnection.java new file mode 100644 index 0000000..7c897d8 --- /dev/null +++ b/src/javaapplication1/DBConnection.java @@ -0,0 +1,17 @@ +package javaapplication1; +import java.sql.*; + +public class DBConnection { + public static Connection getConnection() throws SQLException { + Connection connection = null; + try { + Class.forName("org.postgresql.Driver"); + connection = DriverManager.getConnection( + "jdbc:postgresql://192.168.1.44:5432/hotel", "hotel_admin", "1"); + System.out.println("Connected to PostgreSQL database!"); + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + return connection; + } +} \ No newline at end of file diff --git a/src/javaapplication1/GuestToRoom.form b/src/javaapplication1/GuestToRoom.form new file mode 100644 index 0000000..4f0b3a4 --- /dev/null +++ b/src/javaapplication1/GuestToRoom.form @@ -0,0 +1,84 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jTable1MouseClicked"/> + </Events> + </Component> + </SubComponents> + </Container> + </SubComponents> +</Form> diff --git a/src/javaapplication1/GuestToRoom.java b/src/javaapplication1/GuestToRoom.java new file mode 100644 index 0000000..c77c655 --- /dev/null +++ b/src/javaapplication1/GuestToRoom.java @@ -0,0 +1,149 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import javax.swing.JTable; +import javax.swing.table.DefaultTableModel; + +/** + * + * @author oleg + */ +public class GuestToRoom extends javax.swing.JFrame { + + private Statement stmt; + private Connection conn; + private int roomid; + + /** + * Creates new form NewRoomEdit + */ + public GuestToRoom(int rid) { + initComponents(); + roomid = rid; + + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + + private void refreshTable() { + try { + ResultSet rs = stmt.executeQuery("SELECT id, fullname FROM guests ORDER BY id ASC;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "Full Name"}, 0){ + @Override + public boolean isCellEditable(int i, int i1) { + return false; + } + }; + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getString("fullname")}); + } + jTable1.setModel(model); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + {null, null}, + {null, null}, + {null, null}, + {null, null} + }, + new String [] { + "id", "fullname" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.String.class + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + }); + jTable1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jTable1MouseClicked(evt); + } + }); + jScrollPane1.setViewportView(jTable1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(13, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(13, Short.MAX_VALUE)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked + if (evt.getClickCount() == 2) { // обработка двойного клика + JTable target = (JTable)evt.getSource(); + int row = target.getSelectedRow(); // получаем номер строки + Object[] rowData = new Object[target.getColumnCount()]; + // получаем значения ячеек выбранной строки + for (int i = 0; i < target.getColumnCount(); i++) { + rowData[i] = target.getValueAt(row, i); + } + int guestid = (int) target.getModel().getValueAt(row, 0); + try { + stmt.executeUpdate("INSERT INTO settlement (guestid, roomid) VALUES (" + guestid + ", " + roomid + ")"); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } +// String fullname = (String) target.getModel().getValueAt(row, 1); + dispose(); + } + }//GEN-LAST:event_jTable1MouseClicked + + /** + * @param args the command line arguments + */ + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/GuestsPanel.form b/src/javaapplication1/GuestsPanel.form new file mode 100644 index 0000000..c9f4e6a --- /dev/null +++ b/src/javaapplication1/GuestsPanel.form @@ -0,0 +1,162 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" pref="609" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="65" max="-2" attributes="0"/> + <Component id="AddButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="UpdateButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="70" max="-2" attributes="0"/> + <Component id="RefreshButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="CloseButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="0" max="32767" attributes="0"/> + </Group> + </Group> + <EmptySpace max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" min="-2" pref="302" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="27" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="AddButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="UpdateButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="CloseButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="RefreshButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace pref="52" max="32767" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> + <AuxValues> + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> + <SubComponents> + <Component class="javax.swing.JTable" name="jTable1"> + <Properties> + <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> + <Table columnCount="6" rowCount="0"> + <Column editable="true" title="id" type="java.lang.Integer"/> + <Column editable="true" title="fullname" type="java.lang.String"/> + <Column editable="true" title="sex" type="java.lang.String"/> + <Column editable="true" title="address" type="java.lang.String"/> + <Column editable="true" title="birthdate" type="java.lang.Integer"/> + <Column editable="true" title="passport_number" type="java.lang.String"/> + </Table> + </Property> + <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor"> + <TableColumnModel selectionModel="0"> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jTable1MouseClicked"/> + </Events> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JButton" name="AddButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Add"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="AddButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="UpdateButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Update"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="UpdateButtonMouseClicked"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="CloseButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Delete"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CloseButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="RefreshButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Refresh"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="RefreshButtonMouseClicked"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/src/javaapplication1/GuestsPanel.java b/src/javaapplication1/GuestsPanel.java new file mode 100644 index 0000000..15e8a22 --- /dev/null +++ b/src/javaapplication1/GuestsPanel.java @@ -0,0 +1,337 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * + * @author oleg + */ +public class GuestsPanel extends javax.swing.JFrame { + private Statement stmt; + private Connection conn; + /** + * Creates new form NewJFrame1 + */ + public GuestsPanel() { + initComponents(); + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + AddButton = new javax.swing.JButton(); + UpdateButton = new javax.swing.JButton(); + CloseButton = new javax.swing.JButton(); + RefreshButton = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + + }, + new String [] { + "id", "fullname", "sex", "address", "birthdate", "passport_number" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + }); + jTable1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jTable1MouseClicked(evt); + } + }); + jScrollPane1.setViewportView(jTable1); + + AddButton.setLabel("Add"); + AddButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AddButtonActionPerformed(evt); + } + }); + + UpdateButton.setLabel("Update"); + UpdateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpdateButtonMouseClicked(evt); + } + }); + + CloseButton.setLabel("Delete"); + CloseButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + CloseButtonActionPerformed(evt); + } + }); + + RefreshButton.setLabel("Refresh"); + RefreshButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RefreshButtonMouseClicked(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(AddButton) + .addGap(71, 71, 71) + .addComponent(UpdateButton) + .addGap(70, 70, 70) + .addComponent(RefreshButton) + .addGap(71, 71, 71) + .addComponent(CloseButton) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(AddButton) + .addComponent(UpdateButton) + .addComponent(CloseButton) + .addComponent(RefreshButton)) + .addContainerGap(52, Short.MAX_VALUE)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed + try { + String fullname = JOptionPane.showInputDialog("Enter room Full Name:"); + String sex = JOptionPane.showInputDialog("Enter Sex:"); + String address = JOptionPane.showInputDialog("Enter Address:"); + String datestr = JOptionPane.showInputDialog("Enter Birth Date:"); + DateFormat format = new SimpleDateFormat("yyyy-mm-dd"); + Date birthdate = format.parse(datestr); + String passport_Number = JOptionPane.showInputDialog("Enter Passport Number:"); + stmt.executeUpdate("INSERT INTO guests (fullname, sex, address, birthdate, passport_number) VALUES ('" + fullname + "', '" + sex + "', '" + address + "', " + birthdate + ", '" + passport_Number + "')"); + refreshTable(); + } catch (SQLException | ParseException ex) { + ex.printStackTrace(); + } + // обновление таблицы + updateTable(); + refreshTable(); + + }//GEN-LAST:event_AddButtonActionPerformed + + private void UpdateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateButtonMouseClicked + updateTable(); + }//GEN-LAST:event_UpdateButtonMouseClicked + + private void refreshTable() { + try { + ResultSet rs = stmt.executeQuery("SELECT * FROM guests ORDER BY id ASC;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "Full Name", "Sex", "Address", "Birthdate", "Passport Number"}, 0){ + @Override + public boolean isCellEditable(int i, int i1) { + return false; + } + }; + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getString("fullname"), rs.getString("sex"), rs.getString("address"), rs.getDate("birthdate"), rs.getString("passport_number")}); + } + jTable1.setModel(model); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void updateTable() { + try { + String query = "SELECT * FROM guests"; + ResultSet resultSet = stmt.executeQuery(query); + // очистка модели таблицы + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + model.setRowCount(0); + // заполнение модели таблицы данными из базы данных + while (resultSet.next()) { + int id = resultSet.getInt("id"); + int fullname = resultSet.getInt("fullname"); + int sex = resultSet.getInt("sex"); + int address = resultSet.getInt("address"); + int birthdate = resultSet.getInt("birthdate"); + int passport_number = resultSet.getInt("passport_number"); + + Object[] row = {id, fullname, sex, address, birthdate, passport_number}; + model.addRow(row); + } + + resultSet.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed + int row = jTable1.getSelectedRow(); + if (row >= 0) { + try { + int id = (int) jTable1.getModel().getValueAt(row, 0); + stmt.executeUpdate("DELETE FROM guests WHERE id=" + id); + refreshTable(); + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + JOptionPane.showMessageDialog(this, "Please select a guest to delete."); + } + }//GEN-LAST:event_CloseButtonActionPerformed + + private void RefreshButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RefreshButtonMouseClicked + refreshTable(); + }//GEN-LAST:event_RefreshButtonMouseClicked + + private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked + if (evt.getClickCount() == 2) { // обработка двойного клика + JTable target = (JTable)evt.getSource(); + int row = target.getSelectedRow(); // получаем номер строки + Object[] rowData = new Object[target.getColumnCount()]; + // получаем значения ячеек выбранной строки + for (int i = 0; i < target.getColumnCount(); i++) { + rowData[i] = target.getValueAt(row, i); + } + + + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + JPanel panel = new JPanel(new GridLayout(6, 2)); + + JTextField FullNameTextField = new JTextField(); + JLabel FullNameLabel = new JLabel("Full Name: "); + FullNameTextField.setText(rowData[1].toString()); + + JTextField SexTextField = new JTextField(); + JLabel SexLabel = new JLabel("Sex: "); + SexTextField.setText(rowData[2].toString()); + + JTextField AddressTextField = new JTextField(); + JLabel AddressLabel = new JLabel("Address: "); + AddressTextField.setText(rowData[3].toString()); + + JTextField BirthdateTextField = new JTextField(); + JLabel BirthdateLabel = new JLabel("Birthdate: "); + BirthdateTextField.setText(rowData[4].toString()); + + JTextField PassportNumberTextField = new JTextField(); + JLabel PassportNumberLabel = new JLabel("Passport Number: "); + PassportNumberTextField.setText(rowData[5].toString()); + + panel.add(FullNameLabel); + panel.add(FullNameTextField); + panel.add(SexLabel); + panel.add(SexTextField); + panel.add(AddressLabel); + panel.add(AddressTextField); + panel.add(BirthdateLabel); + panel.add(BirthdateTextField); + panel.add(PassportNumberLabel); + panel.add(PassportNumberTextField); + + JButton okButton = new JButton("Ок"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // передаем значения ячеек в модель данных таблицы + try { + int id = (int) target.getModel().getValueAt(row, 0); + + target.setValueAt(FullNameTextField.getText(), row, 1); + String FullName = FullNameTextField.getText(); + + target.setValueAt(SexTextField.getText(), row, 2); + String Sex = SexTextField.getText(); + + target.setValueAt(AddressTextField.getText(), row, 3); + String Address = (AddressTextField.getText()); + + target.setValueAt(BirthdateTextField.getText(), row, 4); + DateFormat format = new SimpleDateFormat("yyyy-mm-dd"); + String dateString = BirthdateTextField.getText(); + Date Birthdate = format.parse(dateString); + + target.setValueAt(PassportNumberTextField.getText(), row, 5); + String PassportNumber = (PassportNumberTextField.getText()); + stmt.executeUpdate("UPDATE guests SET fullname='" + FullName + "', sex='" + Sex + "', address='" + Address + "', birthdate='" + Birthdate + "', passport_number='" + PassportNumber + "' WHERE id=" + id); + frame.dispose(); // закрываем форму + } catch (SQLException | ParseException ev) { + ev.printStackTrace(); + } + } + }); + panel.add(okButton); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + +// передаем значения ячеек в модель данных таблицы + } + }//GEN-LAST:event_jTable1MouseClicked + + /** + * @param args the command line arguments + */ + + public String str = new String(); + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton AddButton; + private javax.swing.JButton CloseButton; + private javax.swing.JButton RefreshButton; + private javax.swing.JButton UpdateButton; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/MainFrame.form b/src/javaapplication1/MainFrame.form new file mode 100644 index 0000000..5ada9e9 --- /dev/null +++ b/src/javaapplication1/MainFrame.form @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <NonVisualComponents> + <Menu class="javax.swing.JMenuBar" name="jMenuBar1"> + <SubComponents> + <Menu class="javax.swing.JMenu" name="jMenu1"> + <Properties> + <Property name="label" type="java.lang.String" value="Form1"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jMenu1MouseClicked"/> + </Events> + </Menu> + <Menu class="javax.swing.JMenu" name="jMenu2"> + <Properties> + <Property name="label" type="java.lang.String" value="Form2"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jMenu2MouseClicked"/> + </Events> + </Menu> + <Menu class="javax.swing.JMenu" name="jMenu3"> + <Properties> + <Property name="label" type="java.lang.String" value="Form3"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jMenu3MouseClicked"/> + </Events> + </Menu> + <Menu class="javax.swing.JMenu" name="jMenu4"> + <Properties> + <Property name="label" type="java.lang.String" value="Form4"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jMenu4MouseClicked"/> + </Events> + </Menu> + <Menu class="javax.swing.JMenu" name="jMenu5"> + <Properties> + <Property name="text" type="java.lang.String" value="Form5"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jMenu5MouseClicked"/> + </Events> + </Menu> + </SubComponents> + </Menu> + </NonVisualComponents> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <EmptySpace min="0" pref="373" max="32767" attributes="0"/> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <EmptySpace min="0" pref="277" max="32767" attributes="0"/> + </Group> + </DimensionLayout> + </Layout> +</Form> diff --git a/src/javaapplication1/MainFrame.java b/src/javaapplication1/MainFrame.java new file mode 100644 index 0000000..a57bf95 --- /dev/null +++ b/src/javaapplication1/MainFrame.java @@ -0,0 +1,234 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +/** + * + * @author oleg + */ +public class MainFrame extends javax.swing.JFrame { + + /** + * Creates new form MainFrame + */ + public MainFrame() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jMenuBar1 = new javax.swing.JMenuBar(); + jMenu1 = new javax.swing.JMenu(); + jMenu2 = new javax.swing.JMenu(); + jMenu3 = new javax.swing.JMenu(); + jMenu4 = new javax.swing.JMenu(); + jMenu5 = new javax.swing.JMenu(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jMenu1.setLabel("Form1"); + jMenu1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jMenu1MouseClicked(evt); + } + }); + jMenuBar1.add(jMenu1); + + jMenu2.setLabel("Form2"); + jMenu2.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jMenu2MouseClicked(evt); + } + }); + jMenuBar1.add(jMenu2); + + jMenu3.setLabel("Form3"); + jMenu3.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jMenu3MouseClicked(evt); + } + }); + jMenuBar1.add(jMenu3); + + jMenu4.setLabel("Form4"); + jMenu4.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jMenu4MouseClicked(evt); + } + }); + jMenuBar1.add(jMenu4); + + jMenu5.setText("Form5"); + jMenu5.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jMenu5MouseClicked(evt); + } + }); + jMenuBar1.add(jMenu5); + + setJMenuBar(jMenuBar1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 373, Short.MAX_VALUE) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 277, Short.MAX_VALUE) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseClicked + RoomsPanel form1 = new RoomsPanel(); + form1.setVisible(true); + form1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + form1.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + int confirm = JOptionPane.showOptionDialog(form1, + "Close?", "Exit", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + if (confirm == JOptionPane.YES_OPTION) { + //System.exit(1); + form1.dispose(); + } + } + }); + }//GEN-LAST:event_jMenu1MouseClicked + + private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu2MouseClicked + GuestsPanel form2 = new GuestsPanel(); + form2.setVisible(true); + form2.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + form2.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + int confirm = JOptionPane.showOptionDialog(form2, + "Close?", "Exit", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + if (confirm == JOptionPane.YES_OPTION) { + //System.exit(1); + form2.dispose(); + } + } + }); + }//GEN-LAST:event_jMenu2MouseClicked + + private void jMenu3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu3MouseClicked + ServicePanel form3 = new ServicePanel(); + form3.setVisible(true); + form3.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + form3.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + int confirm = JOptionPane.showOptionDialog(form3, + "Close?", "Exit", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + if (confirm == JOptionPane.YES_OPTION) { + //System.exit(1); + form3.dispose(); + } + } + }); + }//GEN-LAST:event_jMenu3MouseClicked + + private void jMenu4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu4MouseClicked +/* + GuestToRoom form4 = new GuestToRoom(); + form4.setVisible(true); + form4.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + form4.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + int confirm = JOptionPane.showOptionDialog(form4, + "Close?", "Exit", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + if (confirm == JOptionPane.YES_OPTION) { + //System.exit(1); + form4.dispose(); + } + } + }); +*/ + }//GEN-LAST:event_jMenu4MouseClicked + + private void jMenu5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu5MouseClicked + SettlementPanel form5 = new SettlementPanel(); + form5.setVisible(true); + form5.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + form5.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + int confirm = JOptionPane.showOptionDialog(form5, + "Close?", "Exit", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, null, null, null); + if (confirm == JOptionPane.YES_OPTION) { + //System.exit(1); + form5.dispose(); + } + } + }); + }//GEN-LAST:event_jMenu5MouseClicked + + + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + //</editor-fold> + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new MainFrame().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JMenu jMenu1; + private javax.swing.JMenu jMenu2; + private javax.swing.JMenu jMenu3; + private javax.swing.JMenu jMenu4; + private javax.swing.JMenu jMenu5; + private javax.swing.JMenuBar jMenuBar1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/RoomsPanel.form b/src/javaapplication1/RoomsPanel.form new file mode 100644 index 0000000..a13f0ba --- /dev/null +++ b/src/javaapplication1/RoomsPanel.form @@ -0,0 +1,172 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" pref="653" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="65" max="-2" attributes="0"/> + <Component id="AddButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="UpdateButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="70" max="-2" attributes="0"/> + <Component id="RefreshButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="CloseButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="0" max="32767" attributes="0"/> + </Group> + </Group> + <EmptySpace max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" min="-2" pref="302" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="27" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="AddButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="UpdateButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="CloseButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="RefreshButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace pref="14" max="32767" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> + <AuxValues> + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> + <SubComponents> + <Component class="javax.swing.JTable" name="jTable1"> + <Properties> + <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> + <Table columnCount="7" rowCount="0"> + <Column editable="true" title="id" type="java.lang.Integer"/> + <Column editable="true" title="room_num" type="java.lang.Integer"/> + <Column editable="true" title="floor" type="java.lang.Integer"/> + <Column editable="true" title="number_of_places" type="java.lang.Integer"/> + <Column editable="true" title="nulloccupied_places" type="java.lang.Integer"/> + <Column editable="true" title="room_price_per_day" type="java.lang.Integer"/> + <Column editable="false" title="Guest" type="java.lang.Object"/> + </Table> + </Property> + <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor"> + <TableColumnModel selectionModel="0"> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jTable1MouseClicked"/> + </Events> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JButton" name="AddButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Add"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="AddButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="UpdateButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Update"/> + </Properties> + <AccessibilityProperties> + <Property name="AccessibleContext.accessibleName" type="java.lang.String" value="Edit"/> + </AccessibilityProperties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="UpdateButtonMouseClicked"/> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="UpdateButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="CloseButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Delete"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CloseButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="RefreshButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Refresh"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="RefreshButtonMouseClicked"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/src/javaapplication1/RoomsPanel.java b/src/javaapplication1/RoomsPanel.java new file mode 100644 index 0000000..ff8d73a --- /dev/null +++ b/src/javaapplication1/RoomsPanel.java @@ -0,0 +1,438 @@ +package javaapplication1; + +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ + + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +/** + * + * @author oleg + */ +public class RoomsPanel extends javax.swing.JFrame { + private Statement stmt; + private Connection conn; + public int nameid; + /** + * Creates new form NewJFrame + */ + public RoomsPanel() { + initComponents(); + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + JPopupMenu popupMenu = new JPopupMenu(); + JMenuItem freeRoomMenuItem = new JMenuItem("Освободить комнату"); + popupMenu.add(freeRoomMenuItem); + jTable1.setComponentPopupMenu(popupMenu); + freeRoomMenuItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ev) { + // Получаем выбранную строку +// int selectedRow = jTable1.getSelectedRow(); +// int roomId = (int) jTable1.getValueAt(selectedRow, 0); +// try { +// String sql = "DELETE FROM settlement WHERE roomid = " + roomId; +// stmt.executeUpdate(sql); +// refreshTable(); +// } catch (SQLException ex) { +// ex.printStackTrace(); +// } + } + }); + + JMenuItem populateRoomMenuItem = new JMenuItem("Заселить комнату"); + popupMenu.add(populateRoomMenuItem); + jTable1.setComponentPopupMenu(popupMenu); + populateRoomMenuItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ev) { + // Получаем выбранную строку + + int selectedRow = jTable1.getSelectedRow(); + int roomid = (int) jTable1.getValueAt(selectedRow, 0); + + GuestToRoom form4 = new GuestToRoom(roomid); + form4.setVisible(true); + form4.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + +/* + try { + String sql = "DELETE FROM settlement WHERE roomid = " + roomId; + stmt.executeUpdate(sql); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } +*/ + } + }); + + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + AddButton = new javax.swing.JButton(); + UpdateButton = new javax.swing.JButton(); + CloseButton = new javax.swing.JButton(); + RefreshButton = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + + }, + new String [] { + "id", "room_num", "floor", "number_of_places", "nulloccupied_places", "room_price_per_day", "Guest" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class + }; + boolean[] canEdit = new boolean [] { + true, true, true, true, true, true, false + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + + public boolean isCellEditable(int rowIndex, int columnIndex) { + return canEdit [columnIndex]; + } + }); + jTable1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jTable1MouseClicked(evt); + } + }); + jScrollPane1.setViewportView(jTable1); + + AddButton.setLabel("Add"); + AddButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AddButtonActionPerformed(evt); + } + }); + + UpdateButton.setLabel("Update"); + UpdateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpdateButtonMouseClicked(evt); + } + }); + UpdateButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + UpdateButtonActionPerformed(evt); + } + }); + + CloseButton.setLabel("Delete"); + CloseButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + CloseButtonActionPerformed(evt); + } + }); + + RefreshButton.setLabel("Refresh"); + RefreshButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RefreshButtonMouseClicked(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(AddButton) + .addGap(71, 71, 71) + .addComponent(UpdateButton) + .addGap(70, 70, 70) + .addComponent(RefreshButton) + .addGap(71, 71, 71) + .addComponent(CloseButton) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(AddButton) + .addComponent(UpdateButton) + .addComponent(CloseButton) + .addComponent(RefreshButton)) + .addContainerGap(14, Short.MAX_VALUE)) + ); + + UpdateButton.getAccessibleContext().setAccessibleName("Edit"); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed + int row = jTable1.getSelectedRow(); + if (row >= 0) { + try { + int id = (int) jTable1.getModel().getValueAt(row, 0); + stmt.executeUpdate("DELETE FROM room WHERE id=" + id); + refreshTable(); + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + JOptionPane.showMessageDialog(this, "Please select a room to delete."); + } + }//GEN-LAST:event_CloseButtonActionPerformed + + private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed + int roomNum = Integer.parseInt(JOptionPane.showInputDialog("Enter room number:")); + int floor = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + int numberOfPlaces = Integer.parseInt(JOptionPane.showInputDialog("Enter number of places:")); + int occupiedPlaces = Integer.parseInt(JOptionPane.showInputDialog("Enter occupied places:")); + int roomPricePerDay = Integer.parseInt(JOptionPane.showInputDialog("Enter room price per day:")); + + try { + stmt.executeUpdate("INSERT INTO room (room_num, floor, number_of_places, occupied_places, room_price_per_day) VALUES (" + roomNum + ", " + floor + ", " + numberOfPlaces + ", " + occupiedPlaces + ", " + roomPricePerDay + ")"); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + // обновление таблицы + updateTable(); + refreshTable(); + + }//GEN-LAST:event_AddButtonActionPerformed + + private void RefreshButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RefreshButtonMouseClicked + refreshTable(); + }//GEN-LAST:event_RefreshButtonMouseClicked + + private void UpdateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateButtonMouseClicked + updateTable(); + }//GEN-LAST:event_UpdateButtonMouseClicked + + private void UpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateButtonActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_UpdateButtonActionPerformed + + public String str = new String(); + private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked + + // TODO add your handling code here: + // TODO add your handling code here: + if (evt.getClickCount() == 2) { // обработка двойного клика + JTable target = (JTable)evt.getSource(); + int row = target.getSelectedRow(); // получаем номер строки + Object[] rowData = new Object[target.getColumnCount()]; + // получаем значения ячеек выбранной строки + for (int i = 0; i < target.getColumnCount(); i++) { + rowData[i] = target.getValueAt(row, i); + } + // создаем отрывшуюся форму и передаем значения ячеек в текстовые поля +// NewRoomEdit editframe = new NewRoomEdit(); +// editframe.setVisible(true); +// editframe.jTextField1.setText(rowData[1].toString()); +// target.setValueAt(str, row, 1); +// target.setValueAt(str, row, 1); + + + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + JPanel panel = new JPanel(new GridLayout(6, 2)); + + JTextField RoomNumTextField = new JTextField(); + JLabel RoomNumLabel = new JLabel("Room Number: "); + RoomNumTextField.setText(rowData[1].toString()); + + JTextField FloorNumTextField = new JTextField(); + JLabel FloorNumLabel = new JLabel("Floor Number: "); + FloorNumTextField.setText(rowData[2].toString()); + + JTextField NumberOfPlacesTextField = new JTextField(); + JLabel NumberOfPlacesLabel = new JLabel("NumberOfPlaces: "); + NumberOfPlacesTextField.setText(rowData[3].toString()); + + JTextField OccupiedPlacesTextField = new JTextField(); + JLabel OccupiedPlacesLabel = new JLabel("OccupiedPlaces: "); + OccupiedPlacesTextField.setText(rowData[4].toString()); + + JTextField RoomPricePerDayTextField = new JTextField(); + JLabel RoomPricePerDayLabel = new JLabel("RoomPricePerDay: "); + RoomPricePerDayTextField.setText(rowData[5].toString()); + + panel.add(RoomNumLabel); + panel.add(RoomNumTextField); + panel.add(FloorNumLabel); + panel.add(FloorNumTextField); + panel.add(NumberOfPlacesLabel); + panel.add(NumberOfPlacesTextField); + panel.add(OccupiedPlacesLabel); + panel.add(OccupiedPlacesTextField); + panel.add(RoomPricePerDayLabel); + panel.add(RoomPricePerDayTextField); + + JButton okButton = new JButton("Ок"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // передаем значения ячеек в модель данных таблицы + try { + int id = (int) target.getModel().getValueAt(row, 0); + target.setValueAt(RoomNumTextField.getText(), row, 1); + int RoomNum = Integer.parseInt(RoomNumTextField.getText()); + target.setValueAt(FloorNumTextField.getText(), row, 2); + int FloorNum = Integer.parseInt(FloorNumTextField.getText()); + target.setValueAt(NumberOfPlacesTextField.getText(), row, 3); + int NumberOfPlaces = Integer.parseInt(NumberOfPlacesTextField.getText()); + target.setValueAt(OccupiedPlacesTextField.getText(), row, 4); + int OccupiedPlaces = Integer.parseInt(OccupiedPlacesTextField.getText()); + target.setValueAt(RoomPricePerDayTextField.getText(), row, 5); + int RoomPricePerDay = Integer.parseInt(RoomPricePerDayTextField.getText()); + stmt.executeUpdate("UPDATE room SET room_num=" + RoomNum + ", floor=" + FloorNum + ", number_of_places=" + NumberOfPlaces + ", occupied_places=" + OccupiedPlaces + ", room_price_per_day=" + RoomPricePerDay + " WHERE id=" + id); + frame.dispose(); // закрываем форму + } catch (SQLException ev) { + ev.printStackTrace(); + } + } + }); + + panel.add(okButton); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + +// передаем значения ячеек в модель данных таблицы + } + }//GEN-LAST:event_jTable1MouseClicked + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void refreshTable() { + + try { +// ResultSet rs = statement.executeQuery("SELECT * FROM room ORDER BY id ASC;"); +// ResultSet rs1 = statement.executeQuery(); + ResultSet rs = stmt.executeQuery("SELECT room.*, CASE WHEN settlement.guestid IS NULL THEN 'free' ELSE guests.fullname END AS fullname FROM room LEFT JOIN settlement ON room.id = settlement.roomid LEFT JOIN guests ON settlement.guestid = guests.id;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "Room_Number", "Floor", "Number of places", "Occupied places", "Price per day", "Guest"}, 0){ + @Override + public boolean isCellEditable(int i, int i1) { + return false; + } + }; + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getInt("room_num"), rs.getInt("floor"), rs.getInt("number_of_places"), rs.getInt("occupied_places"), rs.getInt("room_price_per_day"), rs.getString("fullname")}); + } + jTable1.setModel(model); +/* + JPopupMenu popupMenu = new JPopupMenu(); + JMenuItem freeRoomMenuItem = new JMenuItem("Освободить комнату"); + popupMenu.add(freeRoomMenuItem); + jTable1.setComponentPopupMenu(popupMenu); + freeRoomMenuItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ev) { + + // Получаем выбранную строку + int selectedRow = jTable1.getSelectedRow(); + int roomId = (int) jTable1.getValueAt(selectedRow, 0); + try { + Connection conn = DBConnection.getConnection(); + Statement stmt = conn.createStatement(); + String sql = "DELETE FROM settlement WHERE roomid = " + roomId; + stmt.executeUpdate(sql); + + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + }); +*/ + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void updateTable() { + try { + String query = "SELECT * FROM room"; + ResultSet resultSet = stmt.executeQuery(query); + + // очистка модели таблицы + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + model.setRowCount(0); + + // заполнение модели таблицы данными из базы данных + while (resultSet.next()) { + int id = resultSet.getInt("id"); + int room_num = resultSet.getInt("room_num"); + int floor = resultSet.getInt("floor"); + int numberOfPlaces = resultSet.getInt("number_of_places"); + int occupiedPlaces = resultSet.getInt("occupied_places"); + int pricePerDay = resultSet.getInt("room_price_per_day"); + + Object[] row = {id, room_num, floor, numberOfPlaces, occupiedPlaces, pricePerDay}; + model.addRow(row); + } + + //resultSet.close(); + //statement.close(); + //connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + /** + * @param args the command line arguments + */ + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton AddButton; + private javax.swing.JButton CloseButton; + private javax.swing.JButton RefreshButton; + private javax.swing.JButton UpdateButton; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/ServiceListPanel.form b/src/javaapplication1/ServiceListPanel.form new file mode 100644 index 0000000..84914f3 --- /dev/null +++ b/src/javaapplication1/ServiceListPanel.form @@ -0,0 +1,147 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="65" max="-2" attributes="0"/> + <Component id="AddButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="UpdateButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="70" max="-2" attributes="0"/> + <Component id="RefreshButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="CloseButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="0" max="32767" attributes="0"/> + </Group> + </Group> + <EmptySpace max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" min="-2" pref="302" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="27" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="AddButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="UpdateButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="CloseButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="RefreshButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace max="32767" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Component class="javax.swing.JButton" name="UpdateButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Update"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="UpdateButtonMouseClicked"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="CloseButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Delete"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CloseButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="RefreshButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Refresh"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="RefreshButtonMouseClicked"/> + </Events> + </Component> + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> + <AuxValues> + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> + <SubComponents> + <Component class="javax.swing.JTable" name="jTable1"> + <Properties> + <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> + <Table columnCount="4" rowCount="0"> + <Column editable="true" title="id" type="java.lang.Integer"/> + <Column editable="true" title="number_of_services" type="java.lang.Integer"/> + <Column editable="true" title="guestid" type="java.lang.Integer"/> + <Column editable="true" title="serviceid" type="java.lang.Integer"/> + </Table> + </Property> + <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor"> + <TableColumnModel selectionModel="0"> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JButton" name="AddButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Add"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="AddButtonActionPerformed"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/src/javaapplication1/ServiceListPanel.java b/src/javaapplication1/ServiceListPanel.java new file mode 100644 index 0000000..ebabeb8 --- /dev/null +++ b/src/javaapplication1/ServiceListPanel.java @@ -0,0 +1,263 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +/** + * + * @author oleg + */ +public class ServiceListPanel extends javax.swing.JFrame { + private Statement stmt; + private Connection conn; + /** + * Creates new form ServiceListPanel + */ + public ServiceListPanel() { + initComponents(); + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + UpdateButton = new javax.swing.JButton(); + CloseButton = new javax.swing.JButton(); + RefreshButton = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + AddButton = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + UpdateButton.setLabel("Update"); + UpdateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpdateButtonMouseClicked(evt); + } + }); + + CloseButton.setLabel("Delete"); + CloseButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + CloseButtonActionPerformed(evt); + } + }); + + RefreshButton.setLabel("Refresh"); + RefreshButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RefreshButtonMouseClicked(evt); + } + }); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + + }, + new String [] { + "id", "number_of_services", "guestid", "serviceid" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + }); + jScrollPane1.setViewportView(jTable1); + + AddButton.setLabel("Add"); + AddButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AddButtonActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1)) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(AddButton) + .addGap(71, 71, 71) + .addComponent(UpdateButton) + .addGap(70, 70, 70) + .addComponent(RefreshButton) + .addGap(71, 71, 71) + .addComponent(CloseButton) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(AddButton) + .addComponent(UpdateButton) + .addComponent(CloseButton) + .addComponent(RefreshButton)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void UpdateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateButtonMouseClicked + updateTable(); + }//GEN-LAST:event_UpdateButtonMouseClicked + + private void refreshTable() { + try { + Connection connection = DBConnection.getConnection(); + Statement statement = connection.createStatement(); + ResultSet rs = statement.executeQuery("SELECT * FROM service_list ORDER BY id ASC;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "number_of_services", "guestid", "serviceid"}, 0); + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getString("number_of_services"), rs.getInt("guestid"), rs.getInt("serviceid")}); + } + jTable1.setModel(model); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void updateTable() { + try { + Connection connection = DBConnection.getConnection(); + String query = "SELECT * FROM service_list"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(query); + + // очистка модели таблицы + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + model.setRowCount(0); + + // заполнение модели таблицы данными из базы данных + while (resultSet.next()) { + int id = resultSet.getInt("id"); + int numberOfServices = resultSet.getInt("number_of_services"); + int guestid = resultSet.getInt("guestid"); + int serviceid = resultSet.getInt("serviceid"); + + Object[] row = {id, numberOfServices, guestid, serviceid}; + model.addRow(row); + } + + resultSet.close(); + statement.close(); + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed + try { + stmt.close(); + conn.close(); + } catch (SQLException e){ + e.printStackTrace(); + } + dispose(); // закрыть только эту форму + }//GEN-LAST:event_CloseButtonActionPerformed + + private void RefreshButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RefreshButtonMouseClicked + refreshTable(); + }//GEN-LAST:event_RefreshButtonMouseClicked + + private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed + int number_of_services = Integer.parseInt(JOptionPane.showInputDialog("Enter service number:")); + int guestid = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + int serviceid = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + + try { + stmt.executeUpdate("INSERT INTO service_list (service_name, service_price) VALUES (" + number_of_services + ", " + guestid + ", " + serviceid + ")"); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + // обновление таблицы + updateTable(); + refreshTable(); + }//GEN-LAST:event_AddButtonActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ServiceListPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ServiceListPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ServiceListPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ServiceListPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + //</editor-fold> + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ServiceListPanel().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton AddButton; + private javax.swing.JButton CloseButton; + private javax.swing.JButton RefreshButton; + private javax.swing.JButton UpdateButton; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/ServicePanel.form b/src/javaapplication1/ServicePanel.form new file mode 100644 index 0000000..5917336 --- /dev/null +++ b/src/javaapplication1/ServicePanel.form @@ -0,0 +1,144 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="65" max="-2" attributes="0"/> + <Component id="AddButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="UpdateButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="70" max="-2" attributes="0"/> + <Component id="RefreshButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="CloseButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="0" max="32767" attributes="0"/> + </Group> + </Group> + <EmptySpace max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" min="-2" pref="302" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="27" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="AddButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="UpdateButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="CloseButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="RefreshButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace max="32767" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> + <AuxValues> + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> + <SubComponents> + <Component class="javax.swing.JTable" name="jTable1"> + <Properties> + <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> + <Table columnCount="3" rowCount="0"> + <Column editable="true" title="id" type="java.lang.Integer"/> + <Column editable="true" title="service_name" type="java.lang.String"/> + <Column editable="true" title="service_price" type="java.lang.Integer"/> + </Table> + </Property> + <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor"> + <TableColumnModel selectionModel="0"> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="jTable1MouseClicked"/> + </Events> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JButton" name="AddButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Add"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="AddButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="UpdateButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Update"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="UpdateButtonMouseClicked"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="CloseButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Delete"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CloseButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="RefreshButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Refresh"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="RefreshButtonMouseClicked"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/src/javaapplication1/ServicePanel.java b/src/javaapplication1/ServicePanel.java new file mode 100644 index 0000000..25e895b --- /dev/null +++ b/src/javaapplication1/ServicePanel.java @@ -0,0 +1,347 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +/** + * + * @author oleg + */ +public class ServicePanel extends javax.swing.JFrame { + private Statement stmt; + private Connection conn; + /** + * Creates new form ServicePaanel + */ + public ServicePanel() { + initComponents(); + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + AddButton = new javax.swing.JButton(); + UpdateButton = new javax.swing.JButton(); + CloseButton = new javax.swing.JButton(); + RefreshButton = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + + }, + new String [] { + "id", "service_name", "service_price" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + }); + jTable1.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + jTable1MouseClicked(evt); + } + }); + jScrollPane1.setViewportView(jTable1); + + AddButton.setLabel("Add"); + AddButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AddButtonActionPerformed(evt); + } + }); + + UpdateButton.setLabel("Update"); + UpdateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpdateButtonMouseClicked(evt); + } + }); + + CloseButton.setLabel("Delete"); + CloseButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + CloseButtonActionPerformed(evt); + } + }); + + RefreshButton.setLabel("Refresh"); + RefreshButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RefreshButtonMouseClicked(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1)) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(AddButton) + .addGap(71, 71, 71) + .addComponent(UpdateButton) + .addGap(70, 70, 70) + .addComponent(RefreshButton) + .addGap(71, 71, 71) + .addComponent(CloseButton) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(AddButton) + .addComponent(UpdateButton) + .addComponent(CloseButton) + .addComponent(RefreshButton)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed + char service_name = (char) Integer.parseInt(JOptionPane.showInputDialog("Enter room number:")); + int service_price = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + + try { + stmt.executeUpdate("INSERT INTO service (service_name, service_price) VALUES (" + service_name + ", " + service_price + ")"); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + // обновление таблицы + updateTable(); + refreshTable(); + + }//GEN-LAST:event_AddButtonActionPerformed + + private void UpdateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateButtonMouseClicked + updateTable(); + }//GEN-LAST:event_UpdateButtonMouseClicked + + private void refreshTable() { + try { + Connection connection = DBConnection.getConnection(); + Statement statement = connection.createStatement(); + ResultSet rs = statement.executeQuery("SELECT * FROM service ORDER BY id ASC;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "Service_Name", "Service_Price"}, 0){ + @Override + public boolean isCellEditable(int i, int i1) { + return false; + } + }; + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getString("service_name"), rs.getInt("service_price")}); + } + jTable1.setModel(model); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void updateTable() { + try { + Connection connection = DBConnection.getConnection(); + String query = "SELECT * FROM service"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(query); + + // очистка модели таблицы + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + model.setRowCount(0); + + // заполнение модели таблицы данными из базы данных + while (resultSet.next()) { + int id = resultSet.getInt("id"); + int servicename = resultSet.getInt("servicename"); + int serviceprice = resultSet.getInt("serviceprice"); + + Object[] row = {id, servicename, serviceprice}; + model.addRow(row); + } + + resultSet.close(); + statement.close(); + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed + int row = jTable1.getSelectedRow(); + if (row >= 0) { + try { + int id = (int) jTable1.getModel().getValueAt(row, 0); + stmt.executeUpdate("DELETE FROM service WHERE id=" + id); + refreshTable(); + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + JOptionPane.showMessageDialog(this, "Please select a service to delete."); + } + }//GEN-LAST:event_CloseButtonActionPerformed + + private void RefreshButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RefreshButtonMouseClicked + refreshTable(); + }//GEN-LAST:event_RefreshButtonMouseClicked + + private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked + + // TODO add your handling code here: + // TODO add your handling code here: + if (evt.getClickCount() == 2) { // обработка двойного клика + JTable target = (JTable)evt.getSource(); + int row = target.getSelectedRow(); // получаем номер строки + Object[] rowData = new Object[target.getColumnCount()]; + // получаем значения ячеек выбранной строки + for (int i = 0; i < target.getColumnCount(); i++) { + rowData[i] = target.getValueAt(row, i); + } + // создаем отрывшуюся форму и передаем значения ячеек в текстовые поля +// NewRoomEdit editframe = new NewRoomEdit(); +// editframe.setVisible(true); +// editframe.jTextField1.setText(rowData[1].toString()); +// target.setValueAt(str, row, 1); +// target.setValueAt(str, row, 1); + + + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + JPanel panel = new JPanel(new GridLayout(6, 2)); + + JTextField ServiceNameTextField = new JTextField(); + JLabel ServiceNameLabel = new JLabel("Service Name: "); + ServiceNameTextField.setText(rowData[1].toString()); + + JTextField ServicePriceTextField = new JTextField(); + JLabel ServicePriceLabel = new JLabel("Service Price: "); + ServicePriceTextField.setText(rowData[2].toString()); + + + panel.add(ServiceNameLabel); + panel.add(ServiceNameTextField); + panel.add(ServicePriceLabel); + panel.add(ServicePriceTextField); + + JButton okButton = new JButton("Ок"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // передаем значения ячеек в модель данных таблицы + try { + int id = (int) target.getModel().getValueAt(row, 0); + target.setValueAt(ServiceNameTextField.getText(), row, 1); + String ServiceName =(ServiceNameTextField.getText()); + + target.setValueAt(ServicePriceTextField.getText(), row, 2); + int ServicePrice = Integer.parseInt(ServicePriceTextField.getText()); + + stmt.executeUpdate("UPDATE service SET service_name= '" + ServiceName + "', service_price=" + ServicePrice + " WHERE id=" + id); + frame.dispose(); // закрываем форму + } catch (SQLException ev) { + ev.printStackTrace(); + } + } + }); + panel.add(okButton); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + +// передаем значения ячеек в модель данных таблицы + } + }//GEN-LAST:event_jTable1MouseClicked + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ServicePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ServicePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ServicePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ServicePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + //</editor-fold> + //</editor-fold> + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ServicePanel().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton AddButton; + private javax.swing.JButton CloseButton; + private javax.swing.JButton RefreshButton; + private javax.swing.JButton UpdateButton; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/SettlementPanel.form b/src/javaapplication1/SettlementPanel.form new file mode 100644 index 0000000..d3c1cc5 --- /dev/null +++ b/src/javaapplication1/SettlementPanel.form @@ -0,0 +1,153 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + <SyntheticProperty name="generateCenter" type="boolean" value="false"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + </AuxValues> + + <Layout> + <DimensionLayout dim="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="65" max="-2" attributes="0"/> + <Component id="AddButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="UpdateButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="70" max="-2" attributes="0"/> + <Component id="RefreshButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="71" max="-2" attributes="0"/> + <Component id="CloseButton" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="0" max="32767" attributes="0"/> + </Group> + </Group> + <EmptySpace max="-2" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + <DimensionLayout dim="1"> + <Group type="103" groupAlignment="0" attributes="0"> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace max="-2" attributes="0"/> + <Component id="jScrollPane1" min="-2" pref="302" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="27" max="-2" attributes="0"/> + <Group type="103" groupAlignment="3" attributes="0"> + <Component id="AddButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="UpdateButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="CloseButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="RefreshButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <EmptySpace max="32767" attributes="0"/> + </Group> + </Group> + </DimensionLayout> + </Layout> + <SubComponents> + <Component class="javax.swing.JButton" name="UpdateButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Update"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="UpdateButtonMouseClicked"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="CloseButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Delete"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="CloseButtonActionPerformed"/> + </Events> + </Component> + <Component class="javax.swing.JButton" name="RefreshButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Refresh"/> + </Properties> + <Events> + <EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="RefreshButtonMouseClicked"/> + </Events> + </Component> + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> + <AuxValues> + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> + <SubComponents> + <Component class="javax.swing.JTable" name="jTable1"> + <Properties> + <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor"> + <Table columnCount="5" rowCount="0"> + <Column editable="true" title="id" type="java.lang.Integer"/> + <Column editable="true" title="guestid" type="java.lang.Integer"/> + <Column editable="true" title="roomid" type="java.lang.Integer"/> + <Column editable="true" title="date_in" type="java.lang.Integer"/> + <Column editable="true" title="date_out" type="java.lang.Integer"/> + </Table> + </Property> + <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor"> + <TableColumnModel selectionModel="0"> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JButton" name="AddButton"> + <Properties> + <Property name="label" type="java.lang.String" value="Add"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="AddButtonActionPerformed"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/src/javaapplication1/SettlementPanel.java b/src/javaapplication1/SettlementPanel.java new file mode 100644 index 0000000..93d2623 --- /dev/null +++ b/src/javaapplication1/SettlementPanel.java @@ -0,0 +1,265 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package javaapplication1; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +/** + * + * @author oleg + */ +public class SettlementPanel extends javax.swing.JFrame { + private Statement stmt; + private Connection conn; + + /** + * Creates new form SettlementPanel + */ + public SettlementPanel() { + initComponents(); + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + refreshTable(); + setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + UpdateButton = new javax.swing.JButton(); + CloseButton = new javax.swing.JButton(); + RefreshButton = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + AddButton = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + UpdateButton.setLabel("Update"); + UpdateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpdateButtonMouseClicked(evt); + } + }); + + CloseButton.setLabel("Delete"); + CloseButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + CloseButtonActionPerformed(evt); + } + }); + + RefreshButton.setLabel("Refresh"); + RefreshButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RefreshButtonMouseClicked(evt); + } + }); + + jTable1.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + + }, + new String [] { + "id", "guestid", "roomid", "date_in", "date_out" + } + ) { + Class[] types = new Class [] { + java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + }); + jScrollPane1.setViewportView(jTable1); + + AddButton.setLabel("Add"); + AddButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + AddButtonActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1)) + .addGroup(layout.createSequentialGroup() + .addGap(65, 65, 65) + .addComponent(AddButton) + .addGap(71, 71, 71) + .addComponent(UpdateButton) + .addGap(70, 70, 70) + .addComponent(RefreshButton) + .addGap(71, 71, 71) + .addComponent(CloseButton) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(AddButton) + .addComponent(UpdateButton) + .addComponent(CloseButton) + .addComponent(RefreshButton)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void UpdateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpdateButtonMouseClicked + updateTable(); + }//GEN-LAST:event_UpdateButtonMouseClicked + + private void refreshTable() { + try { + Connection connection = DBConnection.getConnection(); + Statement statement = connection.createStatement(); + ResultSet rs = statement.executeQuery("SELECT * FROM settlement ORDER BY id ASC;"); + DefaultTableModel model = new DefaultTableModel(new Object[]{"ID", "guestid", "room_id", "date_in", "dateout"}, 0); + while (rs.next()) { + model.addRow(new Object[]{rs.getInt("id"), rs.getInt("guestid"), rs.getInt("roomid"), rs.getDate("date_in"), rs.getDate("date_out")}); + } + jTable1.setModel(model); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private void updateTable() { + try { + Connection connection = DBConnection.getConnection(); + String query = "SELECT * settlement"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(query); + + // очистка модели таблицы + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + model.setRowCount(0); + + // заполнение модели таблицы данными из базы данных + while (resultSet.next()) { + int id = resultSet.getInt("id"); + int guestid = resultSet.getInt("guestid"); + int roomid = resultSet.getInt("roomid"); + int datein = resultSet.getInt("date_in"); + int dateout = resultSet.getInt("date_out"); + + Object[] row = {id, guestid, roomid, datein, dateout}; + model.addRow(row); + } + + resultSet.close(); + statement.close(); + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed + try { + stmt.close(); + conn.close(); + } catch (SQLException e){ + e.printStackTrace(); + } + dispose(); // закрыть только эту форму + }//GEN-LAST:event_CloseButtonActionPerformed + + private void RefreshButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RefreshButtonMouseClicked + refreshTable(); + }//GEN-LAST:event_RefreshButtonMouseClicked + + private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed + int guestid = Integer.parseInt(JOptionPane.showInputDialog("Enter service number:")); + int roomid = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + int datein = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + int dateout = Integer.parseInt(JOptionPane.showInputDialog("Enter floor:")); + + try { + stmt.executeUpdate("INSERT INTO settlement (guestid, roomid, date_in, date_out) VALUES (" + guestid + ", " + roomid + ", " + datein + ", " + dateout + ")"); + refreshTable(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + // обновление таблицы + updateTable(); + refreshTable(); + }//GEN-LAST:event_AddButtonActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(SettlementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(SettlementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(SettlementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(SettlementPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + //</editor-fold> + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new SettlementPanel().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton AddButton; + private javax.swing.JButton CloseButton; + private javax.swing.JButton RefreshButton; + private javax.swing.JButton UpdateButton; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/javaapplication1/postgresql-42.6.0.jar b/src/javaapplication1/postgresql-42.6.0.jar new file mode 100644 index 0000000..02f902a Binary files /dev/null and b/src/javaapplication1/postgresql-42.6.0.jar differ