Advertisement
Guest User

123xx123

a guest
Nov 4th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.74 KB | None | 0 0
  1. package xxxxxx;
  2. import java.awt.event.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.Statement;
  6. import javax.swing.*;
  7. import java.sql.*;
  8.  
  9.  
  10. public class Xxxxxx extends JFrame{
  11.  JLabel JL_TYTUL,JL_REZ,JL_CZAS,JL_id,JL_REZIMIE,JL_ROK,JL_DUB;
  12.  JTextField JT_TYTUL,JT_REZ,JT_CZAS,JT_id,JT_REZIMIE,JT_ROK,JT_DUB;
  13.  JButton btn_insert,btn_update,btn_delete,btn_next,btn_prev,btn_first,btn_last;
  14.  int liczba=0,lastone=0;
  15.  public Xxxxxx(){
  16.      super("INSERT UPDATE DELETE");
  17.      JL_id = new JLabel("Id:");
  18.      JL_TYTUL = new JLabel("TYTUL:");
  19.      JL_REZ = new JLabel("REZNAZ:");
  20.      JL_CZAS = new JLabel("CZAS:");
  21.    
  22.      JL_id.setBounds(20, 20, 100, 20);
  23.      JL_TYTUL.setBounds(20, 50, 100, 20);
  24.      JL_REZ.setBounds(20, 80, 100, 20);
  25.      JL_CZAS.setBounds(20, 110, 100, 20);
  26.      
  27.      
  28.      JT_id = new JTextField(20);
  29.      JT_TYTUL = new JTextField(20);
  30.      JT_REZ = new JTextField(20);
  31.      JT_CZAS = new JTextField(20);
  32.      
  33.      JT_id.setBounds(130,20,150,20);
  34.      JT_TYTUL.setBounds(130, 50, 150, 20);
  35.      JT_REZ.setBounds(130, 80, 150, 20);
  36.      JT_CZAS.setBounds(130, 110, 150, 20);
  37.      
  38.      btn_insert = new JButton("Insert");
  39.      btn_update = new JButton("Update");
  40.      btn_delete = new JButton("Delete");
  41.      btn_next = new JButton("Next");  
  42.      btn_prev = new JButton("Prev");  
  43.      btn_first = new JButton("First");  
  44.      btn_last = new JButton("Last");
  45.      
  46.      
  47.      btn_insert.setBounds(300, 50, 80, 20);
  48.      btn_update.setBounds(300, 80, 80, 20);
  49.      btn_delete.setBounds(300, 110, 80, 20);
  50.      btn_next.setBounds(300, 140, 80, 20);
  51.      btn_prev.setBounds(300, 170, 80, 20);  
  52.      btn_first.setBounds(300, 20, 80, 20);
  53.      btn_last.setBounds(300, 200, 80, 20);  
  54.      
  55.      
  56.      JL_REZIMIE = new JLabel("REZImie:");    
  57.      JL_REZIMIE.setBounds(20, 140, 100, 20);
  58.      JT_REZIMIE = new JTextField(20);    
  59.      JT_REZIMIE.setBounds(130,140,150,20);  
  60.      
  61.      JL_ROK = new JLabel("ROK PROD:");    
  62.      JL_ROK.setBounds(20, 170, 100, 20);
  63.      JT_ROK = new JTextField(20);    
  64.      JT_ROK.setBounds(130,170,150,20);        
  65.      
  66.      JL_DUB = new JLabel("DUBBING:");    
  67.      JL_DUB.setBounds(20, 200, 100, 20);
  68.      JT_DUB = new JTextField(20);    
  69.      JT_DUB.setBounds(130,200,150,20);      
  70.      
  71.      
  72.      
  73.      
  74.      setLayout(null);
  75.      add(JL_id);
  76.      add(JL_TYTUL);
  77.      add(JL_REZ);
  78.      add(JL_CZAS);
  79.      add(JL_REZIMIE);
  80.      add(JL_ROK);
  81.      add(JL_DUB);
  82.      
  83.      add(JT_id);
  84.      add(JT_TYTUL);
  85.      add(JT_REZ);
  86.      add(JT_CZAS);
  87.      add(JT_REZIMIE);
  88.      add(JT_ROK);
  89.      add(JT_DUB);
  90.      
  91.      add(btn_first);
  92.      add(btn_insert);
  93.      add(btn_update);
  94.      add(btn_delete);
  95.      add(btn_next);
  96.      add(btn_prev);
  97.      add(btn_last);
  98.      
  99.      
  100.  
  101.    //button insert
  102.     btn_insert.addActionListener(new  ActionListener() {
  103.  
  104.          public void actionPerformed(ActionEvent e) {
  105.          try{
  106.              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()+")");
  107.              liczba++;
  108.              lastone++;
  109.          }
  110.          
  111.          catch(Exception ex){}
  112.          }
  113.      });
  114.    
  115.         //button update
  116.         btn_update.addActionListener(new  ActionListener() {
  117.  
  118.          public void actionPerformed(ActionEvent e) {
  119.          try{
  120.            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());
  121.          }
  122.          catch(Exception ex){}
  123.          }
  124.      });
  125.        
  126.         btn_next.addActionListener(new  ActionListener() {
  127.  
  128.          public void actionPerformed(ActionEvent e) {
  129.          try{
  130.         Connection con = null;
  131.         Statement st = null;          
  132.         Class.forName("org.apache.derby.jdbc.ClientDriver");
  133.         con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");  
  134.         st = con.createStatement();
  135.         if(liczba>=0)
  136.         {
  137.         ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=liczba+1");
  138.         while(rs.next()){
  139.          int id  = rs.getInt("id");
  140.          String tytul = rs.getString("TYTUL");
  141.          int czas  = rs.getInt("CZAS");
  142.          boolean dubb = rs.getBoolean("DUBBING");
  143.          String rezn = rs.getString("REZ_N");
  144.          String rezi = rs.getString("REZ_I");
  145.          int rok  = rs.getInt("ROK");
  146.            
  147.          JT_id.setText(Integer.toString(id));
  148.          JT_TYTUL.setText(tytul);
  149.          JT_CZAS.setText(Integer.toString(czas));  
  150.          JT_DUB.setText(Boolean.toString(dubb));  
  151.          JT_REZ.setText(rezn);  
  152.          JT_REZIMIE.setText(rezi);
  153.          JT_ROK.setText(Integer.toString(rok));        
  154.          }
  155.          }
  156.         else if (liczba<0)
  157.         {
  158.              JT_id.setText("ERROR");    
  159.         }
  160.        
  161.         }
  162.          
  163.          catch(Exception ex){}
  164.          }
  165.      });
  166.         btn_prev.addActionListener(new  ActionListener() {
  167.  
  168.          public void actionPerformed(ActionEvent e) {
  169.         try{
  170.         Connection con = null;
  171.         Statement st = null;          
  172.         Class.forName("org.apache.derby.jdbc.ClientDriver");
  173.         con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");  
  174.         st = con.createStatement();
  175.         if(liczba-1>=0)
  176.         {
  177.         ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=liczba-1;");
  178.         while(rs.next()){
  179.          int id  = rs.getInt("id");
  180.          String tytul = rs.getString("TYTUL");
  181.          int czas  = rs.getInt("CZAS");
  182.          boolean dubb = rs.getBoolean("DUBBING");
  183.          String rezn = rs.getString("REZ_N");
  184.          String rezi = rs.getString("REZ_I");
  185.          int rok  = rs.getInt("ROK");
  186.            
  187.          JT_id.setText(Integer.toString(id));
  188.          JT_TYTUL.setText(tytul);
  189.          JT_CZAS.setText(Integer.toString(czas));  
  190.          JT_DUB.setText(Boolean.toString(dubb));  
  191.          JT_REZ.setText(rezn);  
  192.          JT_REZIMIE.setText(rezi);
  193.          JT_ROK.setText(Integer.toString(rok));        
  194.          }
  195.          }
  196.         else if (liczba-1<=0)
  197.         {
  198.              JT_id.setText("ERROR");    
  199.         }
  200.        
  201.         }
  202.          
  203.          catch(Exception ex){}
  204.          }
  205.      });
  206.         btn_first.addActionListener(new  ActionListener() {
  207.  
  208.          public void actionPerformed(ActionEvent e) {
  209.          try{
  210.         Connection con = null;
  211.         Statement st = null;          
  212.         Class.forName("org.apache.derby.jdbc.ClientDriver");
  213.         con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");  
  214.         st = con.createStatement();
  215.        
  216.         ResultSet rs = st.executeQuery("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=1");
  217.         //JT_id.setText(Integer.toString(rs.getInt("id")));  
  218.         //JT_TYTUL.setText(rs.getString("tytul"));
  219.         //JT_CZAS.setText(Integer.toString(rs.getInt("czas")));
  220.         //JT_DUB.setText(Boolean.toString(rs.getBoolean("dubbing")));
  221.         //JT_REZ.setText(rs.getString("REZ_N"));
  222.         //JT_REZIMIE.setText(rs.getString("REZ_I"));
  223.        // JT_ROK.setText(Integer.toString(rs.getInt("rok")));            
  224.        
  225.        
  226.         while(rs.next()){
  227.          int id  = rs.getInt("id");
  228.          String tytul = rs.getString("TYTUL");
  229.          int czas  = rs.getInt("CZAS");
  230.          boolean dubb = rs.getBoolean("DUBBING");
  231.          String rezn = rs.getString("REZ_N");
  232.          String rezi = rs.getString("REZ_I");
  233.          int rok  = rs.getInt("ROK");
  234.          
  235.          
  236.          JT_id.setText(Integer.toString(id));
  237.          JT_TYTUL.setText(tytul);
  238.          JT_CZAS.setText(Integer.toString(czas));  
  239.          JT_DUB.setText(Boolean.toString(dubb));  
  240.          JT_REZ.setText(rezn);  
  241.          JT_REZIMIE.setText(rezi);
  242.          JT_ROK.setText(Integer.toString(rok));        
  243.          
  244.          }
  245.          }
  246.          catch(Exception ex){}
  247.          }
  248.      });
  249.         btn_last.addActionListener(new  ActionListener() {
  250.  
  251.          public void actionPerformed(ActionEvent e) {
  252.          try{
  253.          Connection con = null;
  254.         Statement st = null;          
  255.         Class.forName("org.apache.derby.jdbc.ClientDriver");
  256.         con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");  
  257.         st = con.createStatement();
  258.        
  259.         PreparedStatement ps = con.prepareStatement("select ID,TYTUL,CZAS,DUBBING,REZ_N,REZ_I,ROK from filmy where ID=?");
  260.         ps.setInt(1,lastone);
  261.  
  262.         while(ps.next()){
  263.          int id  = ps.getInt("id");
  264.          String tytul = ps.getString("TYTUL");
  265.          int czas  = ps.getInt("CZAS");
  266.          boolean dubb = ps.getBoolean("DUBBING");
  267.          String rezn = ps.getString("REZ_N");
  268.          String rezi = ps.getString("REZ_I");
  269.          int rok  = ps.getInt("ROK");
  270.          
  271.          
  272.          JT_id.setText(Integer.toString(id));
  273.          JT_TYTUL.setText(tytul);
  274.          JT_CZAS.setText(Integer.toString(czas));  
  275.          JT_DUB.setText(Boolean.toString(dubb));  
  276.          JT_REZ.setText(rezn);  
  277.          JT_REZIMIE.setText(rezi);
  278.          JT_ROK.setText(Integer.toString(rok));        
  279.          
  280.          }
  281.          }
  282.          catch(Exception ex){}
  283.          }
  284.      });
  285.  
  286.        
  287.          //button delete
  288.         btn_delete.addActionListener(new  ActionListener() {
  289.          public void actionPerformed(ActionEvent e) {
  290.          try{  
  291.              theQuery("delete from filmy where id = "+JT_id.getText());
  292.              liczba--;
  293.          }
  294.          
  295.          catch(Exception ex){}
  296.          }
  297.      });
  298.      
  299.      
  300.      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  301.      setVisible(true);
  302.      setLocationRelativeTo(null);
  303.      setSize(500,500);
  304.    
  305.  }
  306.  
  307.  
  308.  
  309.  
  310.  //function to execute the insert update delete query
  311.   public void theQuery(String query){
  312.       Connection con = null;
  313.       Statement st = null;
  314.       try{
  315.           Class.forName("org.apache.derby.jdbc.ClientDriver");
  316.           con = DriverManager.getConnection("jdbc:derby://localhost:1527/films");
  317.           System.out.println("połączono DB");
  318.           st = con.createStatement();
  319.           st.executeUpdate(query);
  320.           JOptionPane.showMessageDialog(null,"Query Executed");
  321.       }catch(Exception ex){
  322.           JOptionPane.showMessageDialog(null,ex.getMessage());
  323.       }
  324.   }
  325.  
  326.  
  327.      public static void main(String[] args){
  328.      
  329.          new Xxxxxx();
  330.      }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement