NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class laba {


private JFrame frame;
private JFrame frf;
private JTable table;
private DefaultTableModel dtm;
private JScrollPane jsp;
private String qry;
JTextField j ;
JTextField j1 ;
JTextField j2 ;
JTextField j3;
JLabel i ;
JLabel i1 ;
JLabel i2;
JLabel i3 ;
JButton btn ;
JComboBox books;
JComboBox clients;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
laba window = new laba();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
* @throws SQLException
*/
public laba() throws SQLException {
initialize();
}

/**
* Initialize the contents of the frame.
* @throws SQLException
*/
private static Connection con;
private static java.sql.Statement stmt;
private static ResultSet rs;

private void initialize() throws SQLException {
String host = "jdbc:ucanaccess://C:\Users\Egorr\Desktop\DB.mdb";
con=DriverManager.getConnection(host);
stmt = con.createStatement();

frame = new JFrame();
frame.setBounds(100, 100, 481, 157);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton button = new JButton("Взять кредит");
button.setBounds(10, 11, 140, 23);
frame.getContentPane().add(button);
button.addActionListener(new takeCred());

JButton button_1 = new JButton("Положить на депозит книгу");
button_1.setBounds(10, 53, 140, 23);
frame.getContentPane().add(button_1);
button_1.addActionListener(new getDep());

JButton button_2 = new JButton("Добавить клиента");
button_2.setBounds(160, 53, 140, 23);
frame.getContentPane().add(button_2);
button_2.addActionListener(new addClient());

JButton button_3 = new JButton("Добавить счет");
button_3.setBounds(160, 11, 140, 23);
frame.getContentPane().add(button_3);
button_3.addActionListener(new addBill());

JButton btnNewButton = new JButton("Удалить счет");
btnNewButton.setBounds(315, 11, 140, 23);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new delBook());

JButton button_4 = new JButton("Удалить клиента");
button_4.setBounds(315, 53, 140, 23);
frame.getContentPane().add(button_4);
button_4.addActionListener(new delClient());

JLabel lblNewLabel = new JLabel("Бондарев Р.А. КН-2-2");
lblNewLabel.setBounds(10, 100, 94, 23);
frame.getContentPane().add(lblNewLabel);

JButton btnNewButton_1 = new JButton("Просмотр клиентов");
btnNewButton_1.setBounds(160, 87, 140, 23);
frame.getContentPane().add(btnNewButton_1);
btnNewButton_1.addActionListener(new visionBook());

JButton button_5 = new JButton("Просмотр Счетов");
button_5.setBounds(315, 87, 140, 23);
frame.getContentPane().add(button_5);
button_5.addActionListener(new visionClient());
}
public class Accс implements ActionListener {
public void actionPerformed(ActionEvent e) {

qry = "INSERT INTO cred VALUES ('" + clients.getSelectedItem()+ "', '" + j.getText() + "', '" + j1.getText() + "');";
try {
stmt.executeUpdate(qry);
} catch (SQLException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frf.setVisible(false);
qry = "SELECT *FROM cred";
look();
}

}
public class Acccс implements ActionListener {
public void actionPerformed(ActionEvent e) {

qry = "INSERT INTO dep VALUES ('" + clients.getSelectedItem()+ "', '" + j.getText() + "', '" + j1.getText() + "');";
try {
stmt.executeUpdate(qry);
} catch (SQLException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frf.setVisible(false);
qry = "SELECT *FROM dep";
look();
}

}
public class takeCred implements ActionListener {
public void actionPerformed(ActionEvent e) {

btn = new JButton("OK");
btn.setBounds(100, 100, 140, 23);
j = new JTextField();
j1 = new JTextField();
i = new JLabel("Размер:");
i1 = new JLabel("Процент:");
i.setBounds(180, 20, 140, 23);
i1.setBounds(180, 20, 140, 23);
j.setBounds(180, 50, 140, 23);
j1.setBounds(330, 50, 140, 23);
btn.addActionListener(new Accс());
try {
qry = "SELECT * FROM clients";
fill("firstname");
}

catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
clients.setBounds(30, 50, 140, 23);
frf = new JFrame("Cred");
frf.getContentPane().setLayout(null);
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(clients);
frf.add(j);
frf.add(j1);
frf.add(i);
frf.add(i1);
frf.add(btn);
frf.setVisible(true);
}
}

public void fill (String colName) throws SQLException
{
rs = stmt.executeQuery(qry);
ResultSetMetaData rsmd = rs.getMetaData();
ResultSetMetaData rsmdTmp = rsmd;

int i=0;
while (rs.next()){
i++;
}
rs = stmt.executeQuery(qry);
int col;
for(col = 1; col <= rsmd.getColumnCount(); col++)
{
String name = rsmd.getColumnName(col);
if(colName.equals(name))
break;
}
String[] clStrings = new String[i];
i=0;
while (rs.next()) {
int type = rsmd.getColumnType(col);

switch(type) {
case Types.VARCHAR :
String name = rs.getString(col);
clStrings[i] = name;
break;
}
i++;
}
clients = new JComboBox(clStrings);
}
public class getDep implements ActionListener {
public void actionPerformed(ActionEvent e) {
btn = new JButton("OK");
j = new JTextField();
j1 = new JTextField();
i = new JLabel("Размер:");
i1 = new JLabel("Процент:");
i.setBounds(180, 20, 140, 23);
i1.setBounds(330, 20, 140, 23);
j.setBounds(180, 50, 140, 23);
j1.setBounds(330, 50, 140, 23);
btn.setBounds(100, 100, 140, 23);
btn.addActionListener(new Acccс());
try {
qry = "SELECT clients.lastname FROM clients;";
fillLib();
}

catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

clients.setBounds(30, 50, 140, 23);
frf = new JFrame("Dep");
frf.getContentPane().setLayout(null);
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(clients);
frf.add(j);
frf.add(j1);
frf.add(i);
frf.add(i1);
frf.add(btn);
frf.setVisible(true);
}

}
public void fillLib () throws SQLException
{
rs = stmt.executeQuery(qry);
ResultSetMetaData rsmd = rs.getMetaData();
int i=0;
while (rs.next()){
i++;
}
rs = stmt.executeQuery(qry);
String[] clStrings = new String[i];
i=0;
while (rs.next()) {
{
int type = rsmd.getColumnType(1);

switch(type) {
case Types.VARCHAR :
String name = rs.getString(1);
clStrings[i] = name;
break;
}
i++;
}
}
clients = new JComboBox(clStrings);
}
public class addBill implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {

frf = new JFrame("Index");
i1 = new JLabel("Введите Книгу:");
i2 = new JLabel("Введите автора:");
i3 = new JLabel("");
i3.setVisible(false);
i1.setBounds(20, 11, 140, 23);
i2.setBounds(180, 11, 140, 23);

btn = new JButton("OK");

j1 = new JTextField();
j2 = new JTextField();
j3 = new JTextField();
j1.setBounds(20, 50, 140, 23);
j2.setBounds(180, 50, 140, 23);
j3.setVisible(false);
j3.setText("");
frf.getContentPane().setLayout(null);
btn.setBounds(100, 100, 140, 23);
btn.addActionListener(new Acc());
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(btn);
frf.add(i1);
frf.add(j1);
frf.add(i2);
frf.add(j2);
frf.add(i3);
frf.add(j3);

frf.setVisible(true);

rs = stmt.executeQuery("SELECT * FROM book");
int i=0;
while (rs.next()){
i++;
}

qry = "INSERT INTO book VALUES ('" + i;
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
public class addClient implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {

frf = new JFrame("Index");
i1 = new JLabel("Введите имя:");
i2 = new JLabel("Введите фамлию:");
i3 = new JLabel("Серия паспорта:");
i1.setBounds(20, 11, 140, 23);
i2.setBounds(180, 11, 140, 23);
i3.setBounds(350, 11, 140, 23);

btn = new JButton("OK");


j1 = new JTextField();
j2 = new JTextField();
j3 = new JTextField();
j1.setBounds(20, 50, 140, 23);
j2.setBounds(180, 50, 140, 23);
j3.setBounds(350, 50, 140, 23);
frf.getContentPane().setLayout(null);
btn.setBounds(100, 100, 140, 23);
btn.addActionListener(new Acc());
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(btn);
frf.add(i1);
frf.add(j1);
frf.add(i2);
frf.add(j2);
frf.add(i3);
frf.add(j3);

frf.setVisible(true);

rs = stmt.executeQuery("SELECT * FROM client");
int i=0;
while (rs.next()){
i++;
}

qry = "INSERT INTO client VALUES ('" + i;
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
public class Acc implements ActionListener {
public void actionPerformed(ActionEvent e) {
add();
}
}
public void add ()
{
if (j3.getText().length()>0)
qry += "', '"+ j1.getText() +"', '"+ j2.getText() +"', '"+ j3.getText() +"')";
else
qry += "', '"+ j1.getText() +"', '"+ j2.getText() +"')";
try {
stmt.executeUpdate(qry);
} catch (SQLException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frf.setVisible(false);
if(qry.contains("client"))
qry = "SELECT *FROM client";
else
qry = "SELECT *FROM book";
look();
}
public class delBook implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
j = new JTextField();
frf = new JFrame("Index");
i = new JLabel("Введите индекс:");
btn = new JButton("OK");
i.setBounds(100, 11, 140, 23);
j.setBounds(20, 50, 140, 23);
frf.getContentPane().setLayout(null);
btn.setBounds(100, 100, 140, 23);
btn.addActionListener(new Ac());
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(btn);
frf.add(i);
frf.add(j);

frf.setVisible(true);
qry = "DELETE FROM book WHERE (((book.index)=";
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
public class Ac implements ActionListener {
public void actionPerformed(ActionEvent e) {
del();
}
}
public void del ()
{
qry += j.getText() +"));";
try {
stmt.executeUpdate(qry);
} catch (SQLException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frf.setVisible(false);
if(qry.contains("client"))
qry = "SELECT *FROM client";
else
qry = "SELECT *FROM book";
look();
}
public class delClient implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
j = new JTextField();
frf = new JFrame("Index");
i = new JLabel("Введите индекс:");
btn = new JButton("OK");
i.setBounds(100, 11, 140, 23);
j.setBounds(20, 50, 140, 23);
frf.getContentPane().setLayout(null);
btn.setBounds(100, 100, 140, 23);
btn.addActionListener(new Ac());
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frf.add(btn);
frf.add(i);
frf.add(j);

frf.setVisible(true);
qry = "DELETE FROM client WHERE (((client.index)=";
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
public class visionBook implements ActionListener {
public void actionPerformed(ActionEvent e) {
qry = "SELECT *FROM book";
look();
}
}
public class visionClient implements ActionListener {
public void actionPerformed(ActionEvent e) {

qry = "SELECT *FROM client";
look();
}
}

public void look ()
{
try {
dtm = new DefaultTableModel();
rs = stmt.executeQuery(qry);

//Формируем заголовки столбцов из названия полей -->
ResultSetMetaData rsmd = rs.getMetaData();

for(int col = 1; col <= rsmd.getColumnCount(); col++)
dtm.addColumn(rsmd.getColumnName(col));
// <--

while (rs.next()) {

Vector<String> row = new Vector<String>(); //Строка таблицы
/*Снова с помощью метаданных узнаем количество столбцов
* в результате запроса и ниже в switch'е в зависимости
* от типа текущего столбца забираем данные из ResultSet`a и пишем в
* текущую строку - row.
* */
for(int col = 1; col <= rsmd.getColumnCount(); col++) {

int type = rsmd.getColumnType(col);

switch(type) {

case Types.INTEGER :
row.add(new Integer(rs.getInt(col)).toString());
break;

case Types.VARCHAR :
row.add(rs.getString(col));
break;

default :
/*В этой моей тестовой таблице всего два типа полей: целое и строка.
*Соответственно если в таблице/запросе типов больше, то этот switch
*нужно расширить соответствующими типами, а сейчас, в случае если мы
*встретим неизвестный коду тип поля, я для наглядности бросаю исключение.
* */
throw new Exception("Неподдерживаемый тип");
}

}

dtm.addRow(row);
}
}
catch (SQLException ex) {
System.out.println(ex.toString());


} catch (Exception e1) {
e1.printStackTrace();
}


table = new JTable();

table.setModel(dtm);

jsp = new JScrollPane(table);

frf = new JFrame("Библиотека");
frf.setSize(640, 480);
frf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

frf.add(jsp);
frf.setVisible(true);
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.