Advertisement
gay123

Company

Mar 16th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package practical_test;
  7.  
  8. /**
  9.  *
  10.  * @author OtaiPhc
  11.  */
  12. import java.awt.*;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import javax.swing.*;
  16. import java.sql.*;
  17.  
  18. public class Practical_Test extends JFrame{
  19.    
  20.     static final String driver = "com.mysql.jdbc.Driver";
  21.     static final String db = "jdbc:mysql://localhost/company";
  22.     static final String user = "root";
  23.     static final String pass = "";
  24.  
  25.     private Connection con = null;
  26.     private Statement stmt = null;
  27.     private ResultSet rs = null;
  28.     private String sql = null;
  29.     private DatabaseMetaData meta;
  30.    
  31.     private JTextArea txtarea;
  32.     private JScrollPane skrol;
  33.     private JPanel panelA,panelB;
  34.     private JButton btnDisplay,btnReset;
  35.    
  36.     public Practical_Test(){
  37.         super("SZA Enterprise");
  38.         Container cont = getContentPane();
  39.         cont.setLayout(new BorderLayout());
  40.        
  41.         txtarea = new JTextArea(10,20);
  42.         skrol = new JScrollPane(txtarea);
  43.         panelA = new JPanel();
  44.         panelB = new JPanel();
  45.         btnDisplay = new JButton("Display");
  46.         btnReset = new JButton("Clear");
  47.        
  48.         cont.add(panelA,BorderLayout.NORTH);
  49.         panelA.setLayout(new GridLayout());
  50.         panelA.add(skrol);
  51.        
  52.         cont.add(panelB,BorderLayout.CENTER);
  53.         panelB.setLayout(new FlowLayout());
  54.         panelB.add(btnDisplay);
  55.         panelB.add(btnReset);
  56.        
  57.         setSize(500,290);
  58.         setVisible(true);
  59.        
  60.         btnDisplay.addActionListener(new ActionListener() {
  61.    public void actionPerformed(ActionEvent e) {
  62.        
  63.        try{
  64.             Class.forName("com.mysql.jdbc.Driver");
  65.            
  66.             try{
  67.                 con = DriverManager.getConnection(db,user,pass);
  68.                 meta = con.getMetaData();
  69.                 stmt = con.createStatement();
  70.                 sql ="SELECT * FROM staff";
  71.                 rs = stmt.executeQuery(sql);
  72.                
  73.                 while(rs.next()){
  74.                    
  75.                     int id = rs.getInt("no_staff");
  76.                     String name = rs.getString("name");
  77.                     float salary = rs.getFloat("salary");
  78.                    
  79.                     txtarea.append("No.Staff : " + id + " , Name : " + name + " , Salary : " + salary + "\n");
  80.                    
  81.                 }
  82.             }catch(SQLException ex){
  83.                 System.out.println("SQLException: " + ex.getMessage());
  84.             }
  85.            
  86.             rs.close();
  87.             stmt.close();
  88.             con.close();
  89.        
  90.         }catch(SQLException | ClassNotFoundException se){
  91.             System.out.println("SQLException: " + se.getMessage());
  92.         }
  93.    }
  94. });
  95.        
  96.         btnReset.addActionListener(new ActionListener() {
  97.    public void actionPerformed(ActionEvent e) {
  98.        
  99.        txtarea.setText("");
  100.        
  101.        }
  102. });
  103.     }
  104.  
  105.     /**
  106.      * @param args the command line arguments
  107.      */
  108.     public static void main(String[] args) {
  109.         // TODO code application logic here
  110.         Practical_Test pt = new Practical_Test();
  111.         pt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112.     }
  113.    
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement