Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package debt;
  6.  
  7. import java.sql.*;
  8. import javax.swing.*;
  9. import java.awt.*;
  10.  
  11. public class Main {
  12.  
  13.     /**
  14.      * @param args the command line arguments
  15.      */
  16.     public static void main(String[] args) {
  17.         JFrame frame = new JFrame("ButtonDemo");
  18.         frame.setTitle("Five Highest Debts");
  19.         frame.setVisible(true);
  20.         frame.setResizable(false);
  21.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.  
  23.         //Create and set up the content pane.
  24.         JTextArea txtArea = new JTextArea();
  25.         buttonEvent elisten = new buttonEvent(txtArea);
  26.         txtArea.setEditable(false);
  27.         elisten.setOpaque(true); //content panes must be opaque
  28.         frame.setContentPane(elisten);
  29.  
  30.         //Display the window.
  31.         frame.pack();
  32.         frame.setVisible(true);
  33.         try {
  34.             Class.forName("com.mysql.jdbc.Driver");
  35.  
  36.             String dbUser = "root";
  37.             String dbPass = "";
  38.  
  39.             String dbHost = "localhost";
  40.             String dbPort = "3306";
  41.             String dbName = "debtlist";
  42.             String url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
  43.  
  44.             Connection con = DriverManager.getConnection(url, dbUser, dbPass);
  45.             System.out.println("Connection established successfully");
  46.  
  47.             Statement stmt = con.createStatement();
  48.  
  49.             String query = "SELECT Debt FROM customer ORDER BY Debt DESC";
  50.  
  51.             ResultSet rs = stmt.executeQuery(query);
  52.  
  53.             float[] debt;
  54.             debt = new float[5];
  55.             String[] name, surname;
  56.             name = new String[5];
  57.             surname = new String[5];
  58.             int i = 0;
  59.  
  60.             while (rs.next()) {
  61.                 if (i < 5) {
  62.                     name[i] = rs.getString("First_Name");
  63.                     surname[i] = rs.getString("Surname");
  64.                     debt[i] = rs.getFloat("Debt");
  65.                     i++;
  66.                 }
  67.             }
  68.             stmt.close();
  69.  
  70.         } catch (ClassNotFoundException e) {
  71.             System.out.println("Driver not found");
  72.             e.printStackTrace(System.out);
  73.         } catch (SQLException ex) {
  74.             System.out.println("ERROR:Connection not established");
  75.             ex.printStackTrace(System.out);
  76.         }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement