Advertisement
Guest User

Untitled

a guest
May 4th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. package laborator7;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Component;
  5. import java.awt.FlowLayout;
  6. import java.awt.HeadlessException;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JFrame;
  24. import javax.swing.JLabel;
  25. import javax.swing.JOptionPane;
  26. import javax.swing.JPanel;
  27. import javax.swing.JTextField;
  28. import javax.swing.JToolBar;
  29.  
  30.  
  31. import com.mysql.jdbc.Statement;
  32.  
  33. public class MyFrame extends JFrame {
  34.     /**
  35.      *
  36.      */
  37.    
  38. //Start declarare variabile utilizate
  39.    
  40.  
  41.     private  JButton  btnFirst,btnPrevious, btnLast,btnAdd,btnEdit,btnDelete,btnSearch,btnSave,btnUndo;
  42.     private JTextField txtVarsta, txtID,txtNume,txtAfis;
  43.     private Connection con;
  44.     private ResultSet rs;
  45.     private Statement sql;
  46.    
  47.    
  48.    
  49.     private static final long serialVersionUID = 1L;
  50.  
  51.     private  Connection stabilireConecsiune(){
  52.         Connection con=null;
  53.         try {
  54.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  55.         } catch (InstantiationException e) {
  56.  
  57.             JOptionPane.showMessageDialog(null,"eroare");
  58.         } catch (IllegalAccessException e) {
  59.             JOptionPane.showMessageDialog(null,"eroare");
  60.         } catch (ClassNotFoundException e) {
  61.             JOptionPane.showMessageDialog(null,"eroare");
  62.         }  
  63.         try {
  64.                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
  65.         } catch (SQLException e) {
  66.         JOptionPane.showMessageDialog(null,e);
  67.         }
  68.          return con;
  69.     }
  70.     private void scrieInceput(){
  71.         try {
  72.             txtID.setText(rs.getInt("Id")+"");
  73.         } catch (SQLException e) {
  74.             JOptionPane.showMessageDialog(null, e);
  75.         }
  76.        
  77.         try {
  78.             txtNume.setText(rs.getString("nume"));
  79.         } catch (SQLException e) {
  80.             JOptionPane.showMessageDialog(null, e);
  81.         }
  82.         try {
  83.             txtVarsta.setText(rs.getInt(3)+" ");
  84.         } catch (SQLException e) {
  85.             JOptionPane.showMessageDialog(null, e);
  86.         }
  87.    
  88.     }
  89.     private void scrieNext(){
  90.       try {
  91.         while( rs.next() ){
  92.              
  93.              
  94.              
  95.           }
  96.     } catch (SQLException e) {
  97.         e.printStackTrace();
  98.     }
  99.      
  100.      
  101.    }
  102.     private void inchideConecsiune(){
  103.         try {
  104.             con.close();
  105.         } catch (SQLException e) {
  106.             e.printStackTrace();
  107.         }
  108.         try {
  109.             rs.close();
  110.         } catch (SQLException e1) {
  111.    
  112.             JOptionPane.showMessageDialog(null,e1);
  113.         }
  114.         try {
  115.             sql.close();
  116.         } catch (SQLException e) {
  117.             e.printStackTrace();
  118.         }
  119.     }
  120.    
  121.     private void scrieCaseteDeText(){
  122.        
  123.    
  124.         if (con!=null) {
  125.             try {
  126.                  sql=(Statement)con.createStatement();
  127.             } catch (SQLException e) {
  128.               JOptionPane.showMessageDialog(null, e);
  129.             }
  130.             try {
  131.                 if(sql!=null)
  132.                     rs=sql.executeQuery("Select * From persoane");
  133.                
  134.             } catch (SQLException e) {
  135.                 JOptionPane.showMessageDialog(null, e);
  136.             }
  137.         }
  138.        
  139.         try {
  140.            
  141.             if(rs.next()) {
  142.                 scrieInceput();
  143.             }
  144.            
  145.         } catch (HeadlessException | SQLException e2) {
  146.             e2.printStackTrace();
  147.         }
  148.        
  149.        
  150.         inchideConecsiune();
  151.        
  152.        
  153.     }
  154.    
  155.    
  156.    
  157.     public MyFrame()
  158.     {
  159.         super("Tabelul MySQL - persoane");
  160.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  161.         setSize(300,300);
  162.         getContentPane().setLayout(new BorderLayout());
  163.            
  164.         JToolBar myBar= new JToolBar();
  165.        
  166.         getContentPane().add(myBar, BorderLayout.NORTH);
  167.        
  168.        btnFirst= new JButton();
  169.        btnPrevious= new JButton();
  170.        btnLast= new JButton();
  171.        btnAdd= new JButton();
  172.        btnEdit= new JButton();
  173.        btnDelete= new JButton();
  174.        btnSearch= new JButton();
  175.        btnSave= new JButton();
  176.        btnUndo= new JButton();
  177.        txtAfis= new JTextField();
  178.        
  179.        
  180.         btnFirst.setIcon(new ImageIcon("MoveFirst.png"));
  181.         btnFirst.setEnabled(false);
  182.         btnPrevious.setIcon(new ImageIcon("MovePrevious.png"));
  183.         btnPrevious.setEnabled(false);
  184.         btnLast.setIcon(new ImageIcon("MoveLast.png"));
  185.         btnAdd.setIcon(new ImageIcon("Add.png"));
  186.         btnEdit.setIcon(new ImageIcon("Edit.png"));
  187.         btnDelete.setIcon(new ImageIcon("Delete.png"));
  188.         btnSearch.setIcon(new ImageIcon("find.JPG"));
  189.         btnSave.setIcon(new ImageIcon("save.JPG"));
  190.         btnSave.setEnabled(false);
  191.         btnUndo.setIcon(new ImageIcon("undo.JPG"));
  192.         btnUndo.setEnabled(false);
  193.        
  194.         myBar.add(btnFirst);
  195.         myBar.add(btnPrevious);
  196.         myBar.add(txtAfis);
  197.         myBar.add(btnLast);
  198.         myBar.add(btnAdd);
  199.         myBar.add(btnEdit);
  200.         myBar.add(btnDelete);
  201.         myBar.add(btnSearch);
  202.         myBar.add(btnSave);
  203.         myBar.add(btnUndo);
  204.        
  205.        
  206.                
  207.        
  208.         JPanel panel= new JPanel();
  209.         getContentPane().add(panel, BorderLayout.CENTER);
  210.         panel.setLayout(new FlowLayout(FlowLayout.CENTER));
  211.     //  panel.setSize(100, 100);
  212.        
  213.         JLabel empty1= new JLabel("                       ");
  214.         JLabel lblID= new JLabel("ID ");
  215.         lblID.setAlignmentX(Component.RIGHT_ALIGNMENT);
  216.         txtID= new JTextField(10);
  217.         txtID.setAlignmentX(Component.LEFT_ALIGNMENT); 
  218.         JLabel empty= new JLabel("                    ");
  219.         panel.add(empty1);
  220.         panel.add(lblID);
  221.         panel.add(txtID);
  222.         panel.add(empty);
  223.        
  224.         JLabel empty2= new JLabel("                 ");
  225.         JLabel lblNume= new JLabel("Nume ");   
  226.         lblNume.setAlignmentX(Component.RIGHT_ALIGNMENT);
  227.         txtNume= new JTextField(10);   
  228.         txtNume.setAlignmentX(Component.LEFT_ALIGNMENT);   
  229.         JLabel empty3= new JLabel("                    ");
  230.         panel.add(empty2);
  231.         panel.add(lblNume);
  232.         panel.add(txtNume);
  233.         panel.add(empty3);
  234.        
  235.        
  236.         JLabel empty4= new JLabel("                 ");
  237.         JLabel lblVarsta= new JLabel("Varsta");
  238.         lblVarsta.setAlignmentX(Component.RIGHT_ALIGNMENT);
  239.         txtVarsta= new JTextField(10);
  240.         txtVarsta.setAlignmentX(Component.LEFT_ALIGNMENT); 
  241.         JLabel empty5= new JLabel("                    ");
  242.         panel.add(empty4);
  243.         panel.add(lblVarsta);
  244.         panel.add(txtVarsta);
  245.         panel.add(empty5);
  246.        
  247.  
  248.        
  249.         con=stabilireConecsiune();
  250.         scrieCaseteDeText();
  251.        
  252.         setLocationRelativeTo(null);
  253.         panel.setVisible(true);
  254.        
  255.        
  256.     }
  257.    
  258.    
  259.     abstract class urmatorul implements MouseListener{
  260.         String btnApasat;
  261.         public urmatorul(String btnApasat){
  262.             this.btnApasat=btnApasat;
  263.         }
  264.    
  265.  
  266.         @Override
  267.         public void mouseClicked(MouseEvent arg0) {
  268.             if( btnApasat.equals("urmatorul") ){
  269.                
  270.             }
  271.            
  272.         }
  273.     }
  274.    
  275.     public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
  276.     {
  277.         MyFrame frm = new MyFrame();
  278.         frm.setVisible(true);
  279.        
  280.        
  281.        
  282.     /*  Statement sql;
  283.         ResultSet rs;
  284.    
  285.        
  286.         sql = (Statement) con.createStatement();
  287.         rs = sql.executeQuery("select * from persoane");
  288.         while(rs.next())
  289.         System.out.println("id="+rs.getInt("Id")+", nume= "+ rs.getString("nume")+", varsta="+rs.getInt(3));
  290.         con.close();
  291.         sql.close();
  292.         rs.close();*/
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement