Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package practical_test;
- /**
- *
- * @author OtaiPhc
- */
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import java.sql.*;
- public class Practical_Test extends JFrame{
- static final String driver = "com.mysql.jdbc.Driver";
- static final String db = "jdbc:mysql://localhost/company";
- static final String user = "root";
- static final String pass = "";
- private Connection con = null;
- private Statement stmt = null;
- private ResultSet rs = null;
- private String sql = null;
- private DatabaseMetaData meta;
- private JTextArea txtarea;
- private JScrollPane skrol;
- private JPanel panelA,panelB;
- private JButton btnDisplay,btnReset;
- public Practical_Test(){
- super("SZA Enterprise");
- Container cont = getContentPane();
- cont.setLayout(new BorderLayout());
- txtarea = new JTextArea(10,20);
- skrol = new JScrollPane(txtarea);
- panelA = new JPanel();
- panelB = new JPanel();
- btnDisplay = new JButton("Display");
- btnReset = new JButton("Clear");
- cont.add(panelA,BorderLayout.NORTH);
- panelA.setLayout(new GridLayout());
- panelA.add(skrol);
- cont.add(panelB,BorderLayout.CENTER);
- panelB.setLayout(new FlowLayout());
- panelB.add(btnDisplay);
- panelB.add(btnReset);
- setSize(500,290);
- setVisible(true);
- btnDisplay.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- try{
- con = DriverManager.getConnection(db,user,pass);
- meta = con.getMetaData();
- stmt = con.createStatement();
- sql ="SELECT * FROM staff";
- rs = stmt.executeQuery(sql);
- while(rs.next()){
- int id = rs.getInt("no_staff");
- String name = rs.getString("name");
- float salary = rs.getFloat("salary");
- txtarea.append("No.Staff : " + id + " , Name : " + name + " , Salary : " + salary + "\n");
- }
- }catch(SQLException ex){
- System.out.println("SQLException: " + ex.getMessage());
- }
- rs.close();
- stmt.close();
- con.close();
- }catch(SQLException | ClassNotFoundException se){
- System.out.println("SQLException: " + se.getMessage());
- }
- }
- });
- btnReset.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- txtarea.setText("");
- }
- });
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Practical_Test pt = new Practical_Test();
- pt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement