Lab4 comm
This commit is contained in:
263
src/javaapplication1/ServiceListPanel.java
Normal file
263
src/javaapplication1/ServiceListPanel.java
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user