Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WypozyczalniaSamochodow;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
- public class JTableRow {
- public static void main(String[] args){
- // create JFrame and JTable
- JFrame frame = new JFrame();
- JTable table = new JTable();
- // create a table model and set a Column Identifiers to this model
- Object[] columns = {"Marka","Typ","Rocznik","Przebieg", "Dostępność"};
- DefaultTableModel model = new DefaultTableModel();
- model.setColumnIdentifiers(columns);
- // set the model to the table
- table.setModel(model);
- // Change A JTable Background Color, Font Size, Font Color, Row Height
- table.setBackground(Color.LIGHT_GRAY);
- table.setForeground(Color.black);
- Font font = new Font("",1,22);
- table.setFont(font);
- table.setRowHeight(30);
- // create JTextFields
- JTextField textId = new JTextField();
- JTextField textFname = new JTextField();
- JTextField textLname = new JTextField();
- JTextField textAge = new JTextField();
- JTextField txtAge = new JTextField();
- // create JButtons
- JButton btnAdd = new JButton("Add");
- JButton btnDelete = new JButton("Delete");
- JButton btnUpdate = new JButton("Update");
- textId.setBounds(20, 220, 100, 25);
- textFname.setBounds(20, 250, 100, 25);
- textLname.setBounds(20, 280, 100, 25);
- textAge.setBounds(20, 310, 100, 25);
- txtAge.setBounds(20, 340, 100, 25);
- btnAdd.setBounds(150, 220, 100, 25);
- btnUpdate.setBounds(150, 265, 100, 25);
- btnDelete.setBounds(150, 310, 100, 25);
- // create JScrollPane
- JScrollPane pane = new JScrollPane(table);
- pane.setBounds(0, 0, 880, 200);
- frame.setLayout(null);
- frame.add(pane);
- // add JTextFields to the jframe
- frame.add(textId);
- frame.add(textFname);
- frame.add(textLname);
- frame.add(textAge);
- frame.add(txtAge);
- // add JButtons to the jframe
- frame.add(btnAdd);
- frame.add(btnDelete);
- frame.add(btnUpdate);
- // create an array of objects to set the row data
- Object[] row = new Object[5];
- // button add row
- btnAdd.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- row[0] = textId.getText();
- row[1] = textFname.getText();
- row[2] = textLname.getText();
- row[3] = textAge.getText();
- row[4] = txtAge.getText();
- // add row to the model
- model.addRow(row);
- String marka = textId.getText();
- String typ = textFname.getText();
- String rocznik = textLname.getText();
- String przebieg = textAge.getText();
- String dostepnosc= txtAge.getText();
- try {
- // 1. Get connection to database
- Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
- //2. Create a statement
- Statement myStmt = myConn.createStatement();
- //3. Execute SQL query
- String doddaj = "INSERT INTO AASAMOCHOD (MARKA, TYP, ROCZNIK, PRZEBIEG, DOSTEPNOSC)"
- + " values (?, ?, ?, ?, ?)";
- PreparedStatement preparedStmt = myConn.prepareStatement(doddaj);
- preparedStmt.setString(1, marka);
- preparedStmt.setString(2, typ);
- preparedStmt.setString(3, rocznik);
- preparedStmt.setString(4, przebieg);
- preparedStmt.setString(5, dostepnosc);
- preparedStmt.execute();
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- });
- // button remove row
- btnDelete.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- // i = the index of the selected row
- int i = table.getSelectedRow();
- if(i >= 0){
- // remove a row from jtable
- model.removeRow(i);
- String marka = textId.getText();
- try {
- // 1. Get connection to database
- Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
- //2. Create a statement
- //Statement myStmt = myConn.createStatement();
- //3. Execute SQL query
- String doddaj = "DELETE FROM AASAMOCHOD WHERE"
- + "MARKA = ?;";
- PreparedStatement preparedStmt = myConn.prepareStatement(doddaj);
- preparedStmt.setString(1, marka);
- preparedStmt.executeUpdate();
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- else{
- System.out.println("Delete Error");
- }
- }
- });
- // get selected row data From table to textfields
- table.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e){
- // i = the index of the selected row
- int i = table.getSelectedRow();
- textId.setText(model.getValueAt(i, 0).toString());
- textFname.setText(model.getValueAt(i, 1).toString());
- textLname.setText(model.getValueAt(i, 2).toString());
- textAge.setText(model.getValueAt(i, 3).toString());
- txtAge.setText(model.getValueAt(i, 4).toString());
- }
- });
- // button update row
- btnUpdate.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- // i = the index of the selected row
- int i = table.getSelectedRow();
- if(i >= 0)
- {
- model.setValueAt(textId.getText(), i, 0);
- model.setValueAt(textFname.getText(), i, 1);
- model.setValueAt(textLname.getText(), i, 2);
- model.setValueAt(textAge.getText(), i, 3);
- model.setValueAt(txtAge.getText(), i, 4);
- }
- else{
- System.out.println("Update Error");
- }
- }
- });
- frame.setSize(900,430);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- // String imie, nazwisko, miasto, nrkarty;
- //
- // try {
- // // 1. Get connection to database
- // Connection myConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "Warszawa as sysdba", "Bkbrd1934");
- // //2. Create a statement
- // Statement myStmt = myConn.createStatement();
- // //3. Execute SQL query
- // ResultSet myRs = myStmt.executeQuery("SELECT * FROM AAKLIENT");
- // //4. Process the result set
- // while(myRs.next())
- // {
- // imie = myRs.getString("MARKA");
- // nazwisko = myRs.getString("TYP");
- // miasto = myRs.getString("MIASTO");
- // nrkarty = myRs.getString("NRKARTY");
- // }
- //
- // row[0] = imie;
- // row[1] = textFname.getText();
- // row[2] = textLname.getText();
- // row[3] = textAge.getText();
- // row[4] = txtAge.getText();
- //
- //
- // // add row to the model
- // model.addRow(row);
- //
- // }
- //
- // catch(SQLException e){
- //
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement