Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xxxxxx;
- import java.awt.event.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import javax.swing.*;
- import java.sql.*;
- public class Xxxxxx extends JFrame{
- JLabel JL_TYTUL,JL_REZ,JL_CZAS,JL_id,JL_REZIMIE,JL_ROK,JL_DUB;
- JTextField JT_TYTUL,JT_REZ,JT_CZAS,JT_id,JT_REZIMIE,JT_ROK,JT_DUB;
- JButton btn_insert,btn_update,btn_delete,btn_next,btn_prev,btn_first,btn_last;
- int liczba=0,lastone=0;
- public Xxxxxx(){
- super("INSERT UPDATE DELETE");
- JL_id = new JLabel("Id:");
- JL_TYTUL = new JLabel("TYTUL:");
- JL_REZ = new JLabel("REZNAZ:");
- JL_CZAS = new JLabel("CZAS:");
- JL_id.setBounds(20, 20, 100, 20);
- JL_TYTUL.setBounds(20, 50, 100, 20);
- JL_REZ.setBounds(20, 80, 100, 20);
- JL_CZAS.setBounds(20, 110, 100, 20);
- JT_id = new JTextField(20);
- JT_TYTUL = new JTextField(20);
- JT_REZ = new JTextField(20);
- JT_CZAS = new JTextField(20);
- JT_id.setBounds(130,20,150,20);
- JT_TYTUL.setBounds(130, 50, 150, 20);
- JT_REZ.setBounds(130, 80, 150, 20);
- JT_CZAS.setBounds(130, 110, 150, 20);
- btn_insert = new JButton("Insert");
- btn_update = new JButton("Update");
- btn_delete = new JButton("Delete");
- btn_next = new JButton("Next");
- btn_prev = new JButton("Prev");
- btn_first = new JButton("First");
- btn_last = new JButton("Last");
- btn_insert.setBounds(300, 50, 80, 20);
- btn_update.setBounds(300, 80, 80, 20);
- btn_delete.setBounds(300, 110, 80, 20);
- btn_next.setBounds(300, 140, 80, 20);
- btn_prev.setBounds(300, 170, 80, 20);
- btn_first.setBounds(300, 20, 80, 20);
- btn_last.setBounds(300, 200, 80, 20);
- JL_REZIMIE = new JLabel("REZImie:");
- JL_REZIMIE.setBounds(20, 140, 100, 20);
- JT_REZIMIE = new JTextField(20);
- JT_REZIMIE.setBounds(130,140,150,20);
- JL_ROK = new JLabel("ROK PROD:");
- JL_ROK.setBounds(20, 170, 100, 20);
- JT_ROK = new JTextField(20);
- JT_ROK.setBounds(130,170,150,20);
- JL_DUB = new JLabel("DUBBING:");
- JL_DUB.setBounds(20, 200, 100, 20);
- JT_DUB = new JTextField(20);
- JT_DUB.setBounds(130,200,150,20);
- setLayout(null);
- add(JL_id);
- add(JL_TYTUL);
- add(JL_REZ);
- add(JL_CZAS);
- add(JL_REZIMIE);
- add(JL_ROK);
- add(JL_DUB);
- add(JT_id);
- add(JT_TYTUL);
- add(JT_REZ);
- add(JT_CZAS);
- add(JT_REZIMIE);
- add(JT_ROK);
- add(JT_DUB);
- add(btn_first);
- add(btn_insert);
- add(btn_update);
- add(btn_delete);
- add(btn_next);
- add(btn_prev);
- add(btn_last);
- //button insert
- btn_insert.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("insert into filmy (ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK) values("+JT_id.getText()+",'"+JT_TYTUL.getText()+"',"+JT_CZAS.getText()+","+JT_DUB.getText()+",'"+JT_REZ.getText()+"','"+JT_REZIMIE.getText()+"',"+JT_ROK.getText()+")");
- liczba++;
- lastone++;
- }
- catch(Exception ex){}
- }
- });
- //button update
- btn_update.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("update filmy set TYTUL = '"+JT_TYTUL.getText()+"',CZAS = "+JT_CZAS.getText()+", DUBBING = "+JT_DUB.getText()+", REZ_N = '"+JT_REZ.getText()+"', REZ_I = '"+JT_REZIMIE.getText()+"', ROK = "+JT_ROK.getText()+" where id = "+JT_id.getText());
- }
- catch(Exception ex){}
- }
- });
- btn_next.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- Connection con = null;
- Statement st = null;
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
- st = con.createStatement();
- if(liczba>=0)
- {
- ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=liczba+1");
- while(rs.next()){
- int id = rs.getInt("id");
- String tytul = rs.getString("TYTUL");
- int czas = rs.getInt("CZAS");
- boolean dubb = rs.getBoolean("DUBBING");
- String rezn = rs.getString("REZ_N");
- String rezi = rs.getString("REZ_I");
- int rok = rs.getInt("ROK");
- JT_id.setText(Integer.toString(id));
- JT_TYTUL.setText(tytul);
- JT_CZAS.setText(Integer.toString(czas));
- JT_DUB.setText(Boolean.toString(dubb));
- JT_REZ.setText(rezn);
- JT_REZIMIE.setText(rezi);
- JT_ROK.setText(Integer.toString(rok));
- }
- }
- else if (liczba<0)
- {
- JT_id.setText("ERROR");
- }
- }
- catch(Exception ex){}
- }
- });
- btn_prev.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- Connection con = null;
- Statement st = null;
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
- st = con.createStatement();
- if(liczba-1>=0)
- {
- ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=liczba-1;");
- while(rs.next()){
- int id = rs.getInt("id");
- String tytul = rs.getString("TYTUL");
- int czas = rs.getInt("CZAS");
- boolean dubb = rs.getBoolean("DUBBING");
- String rezn = rs.getString("REZ_N");
- String rezi = rs.getString("REZ_I");
- int rok = rs.getInt("ROK");
- JT_id.setText(Integer.toString(id));
- JT_TYTUL.setText(tytul);
- JT_CZAS.setText(Integer.toString(czas));
- JT_DUB.setText(Boolean.toString(dubb));
- JT_REZ.setText(rezn);
- JT_REZIMIE.setText(rezi);
- JT_ROK.setText(Integer.toString(rok));
- }
- }
- else if (liczba-1<=0)
- {
- JT_id.setText("ERROR");
- }
- }
- catch(Exception ex){}
- }
- });
- btn_first.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- Connection con = null;
- Statement st = null;
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
- st = con.createStatement();
- ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=1");
- //JT_id.setText(Integer.toString(rs.getInt("id")));
- //JT_TYTUL.setText(rs.getString("tytul"));
- //JT_CZAS.setText(Integer.toString(rs.getInt("czas")));
- //JT_DUB.setText(Boolean.toString(rs.getBoolean("dubbing")));
- //JT_REZ.setText(rs.getString("REZ_N"));
- //JT_REZIMIE.setText(rs.getString("REZ_I"));
- // JT_ROK.setText(Integer.toString(rs.getInt("rok")));
- while(rs.next()){
- int id = rs.getInt("id");
- String tytul = rs.getString("TYTUL");
- int czas = rs.getInt("CZAS");
- boolean dubb = rs.getBoolean("DUBBING");
- String rezn = rs.getString("REZ_N");
- String rezi = rs.getString("REZ_I");
- int rok = rs.getInt("ROK");
- JT_id.setText(Integer.toString(id));
- JT_TYTUL.setText(tytul);
- JT_CZAS.setText(Integer.toString(czas));
- JT_DUB.setText(Boolean.toString(dubb));
- JT_REZ.setText(rezn);
- JT_REZIMIE.setText(rezi);
- JT_ROK.setText(Integer.toString(rok));
- }
- }
- catch(Exception ex){}
- }
- });
- btn_last.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- Connection con = null;
- Statement st = null;
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
- st = con.createStatement();
- PreparedStatement ps = con.prepareStatement("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=?");
- ps.setInt(1,lastone);
- while(ps.next()){
- int id = ps.getInt("id");
- String tytul = ps.getString("TYTUL");
- int czas = ps.getInt("CZAS");
- boolean dubb = ps.getBoolean("DUBBING");
- String rezn = ps.getString("REZ_N");
- String rezi = ps.getString("REZ_I");
- int rok = ps.getInt("ROK");
- JT_id.setText(Integer.toString(id));
- JT_TYTUL.setText(tytul);
- JT_CZAS.setText(Integer.toString(czas));
- JT_DUB.setText(Boolean.toString(dubb));
- JT_REZ.setText(rezn);
- JT_REZIMIE.setText(rezi);
- JT_ROK.setText(Integer.toString(rok));
- }
- }
- catch(Exception ex){}
- }
- });
- //button delete
- btn_delete.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("delete from filmy where id = "+JT_id.getText());
- liczba--;
- }
- catch(Exception ex){}
- }
- });
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- setLocationRelativeTo(null);
- setSize(500,500);
- }
- //function to execute the insert update delete query
- public void theQuery(String query){
- Connection con = null;
- Statement st = null;
- try{
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
- System.out.println("połączono DB");
- st = con.createStatement();
- st.executeUpdate(query);
- JOptionPane.showMessageDialog(null,"Query Executed");
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null,ex.getMessage());
- }
- }
- public static void main(String[] args){
- new Xxxxxx();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement