Guest User

Untitled

a guest
Jul 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public void setStatusLabel(String statusEntered) {
  2. //Shows the variable statusEntered has been received
  3. System.out.println(statusEntered);
  4.  
  5. //Not working
  6. status_label.setText(statusEntered);
  7.  
  8. //Used this to check if the label receives the data. It does.
  9. String status = status_label.getText();
  10. System.out.println(status);
  11. }
  12.  
  13. //GUI Class reference
  14. MainWindow mainwindow = new MainWindow();
  15.  
  16. public void connect(){
  17. Connection conn = null;
  18. try {
  19. String userName = "root";
  20. String password = "";
  21. String url = "jdbc:mysql://localhost:3306";
  22. Class.forName("com.mysql.jdbc.Driver").newInstance();
  23. conn = DriverManager.getConnection(url, userName, password);
  24.  
  25. //This works
  26. System.out.println("Connection Established");
  27. //The issue is with this guy
  28. mainwindow.setStatusLabel("Connection");
  29. }
  30. catch(Exception e) {
  31. System.err.println("Failed to connect to database");
  32. mainwindow.setStatusLabel("No connection");
  33. }
  34. }
  35.  
  36. public static void main(String[] args) {
  37. java.awt.EventQueue.invokeLater(new Runnable() {
  38. public void run() {
  39. new MainWindow().setVisible(true);
  40. }
  41. });
  42. }
  43.  
  44. public class DatabaseConnection {
  45. // MainWindow mainwindow = new MainWindow(); *** don't do this ***
  46. MainWindow mainwindow;
  47.  
  48. public DatabaseConnection(MainWindow mainwindow) {
  49. this.mainwindow = mainwindow; // pass in the reference in the constructor
  50. }
  51.  
  52. public void connect() {
  53. Connection conn = null;
  54.  
  55. // ... etc
  56.  
  57. // now we can call methods on the actual visualized object
  58. mainwindow.setStatusLabel("Connection");
  59. }
  60.  
  61. public class MainWindows extends JFrame {
  62.  
  63. private JLabel status_label;
  64. public MainWindows(){
  65. status_label = new JLabel("Status");
  66. }
  67.  
  68. public void setStatusLabel(String staus){
  69. status_label.setText(staus);
  70. }
  71. /*.
  72. *
  73. * your code for visualizing
  74. .
  75. .
  76. .
  77. .
  78. .
  79. .*/
  80.  
  81. public static void main(String args[]){
  82. MainWindows mw = new MainWindows();
  83.  
  84. //this will return true if the connection will estblished and will false if not
  85. mw.setStatusLabel("Connection established "+ new DatabaseConnection().connect());
  86.  
  87. }
  88.  
  89. }
  90.  
  91. public class DatabaseConnection{
  92.  
  93.  
  94. public boolean connect(){
  95. Connection conn = null;
  96.  
  97.  
  98. // adding this new variable to get connection status;
  99. boolean returnStatus = false; //default return false if connection established it will true .
  100. try {
  101. String userName = "root";
  102. String password = "";
  103. String url = "jdbc:mysql://localhost:3306";
  104. Class.forName("com.mysql.jdbc.Driver").newInstance();
  105. conn = DriverManager.getConnection(url, userName, password);
  106.  
  107. //This works
  108. System.out.println("Connection Established");
  109. //The issue is with this guy
  110. returnStatus = true; // making status true if connection is established
  111.  
  112. return returnStatus;
  113. }
  114. catch(Exception e) {
  115. System.err.println("Failed to connect to database");
  116. return returnStatus;
  117. }
  118. }
  119.  
  120. }
Add Comment
Please, Sign In to add comment