Advertisement
Guest User

Untitled

a guest
May 1st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package learning_jdbc;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. public class Accounts extends JFrame {
  8. private Connection connection;
  9.  
  10. private void buildGUI() {
  11. //Do Account List
  12. DefaultListmodel model = new DefaultListModel();
  13.  
  14. try (Statement statement = connection.createStatement();
  15. ResultSet rs = statement.executeQuery("SELECT acc_id FROM acc");) {
  16. while(rs.next()) {
  17. model.addElement(rs.getString("acc_id"));
  18. }
  19. rs.close();
  20. } catch(SQLException e) {
  21. displaySQLErrors(e);
  22. }
  23.  
  24. }
  25.  
  26. public void connectToDB() {
  27. try {
  28. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounts","root","thejoker");
  29. } catch(SQLException e) {
  30. System.out.println("Unable to connect to database");
  31. System.exit(1);
  32. }
  33. }
  34.  
  35. private void displaySQLErrors(SQLException e) {
  36. System.out.println("SQLException: " + e.getMessage());
  37. System.out.println("SQLState: " + e.getSQLState());
  38. System.out.println("VendorError: " + e.getErrorCode());
  39. }
  40.  
  41. public Accounts() {
  42. initComponents();
  43.  
  44. try {
  45. Class.forName("com.mysql.jdbc.Driver");
  46. } catch (Exception e) {
  47. System.err.println("Unable to find and load driver");
  48. System.exit(1);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement