Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void setStatusLabel(String statusEntered) {
- //Shows the variable statusEntered has been received
- System.out.println(statusEntered);
- //Not working
- status_label.setText(statusEntered);
- //Used this to check if the label receives the data. It does.
- String status = status_label.getText();
- System.out.println(status);
- }
- //GUI Class reference
- MainWindow mainwindow = new MainWindow();
- public void connect(){
- Connection conn = null;
- try {
- String userName = "root";
- String password = "";
- String url = "jdbc:mysql://localhost:3306";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, userName, password);
- //This works
- System.out.println("Connection Established");
- //The issue is with this guy
- mainwindow.setStatusLabel("Connection");
- }
- catch(Exception e) {
- System.err.println("Failed to connect to database");
- mainwindow.setStatusLabel("No connection");
- }
- }
- public static void main(String[] args) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MainWindow().setVisible(true);
- }
- });
- }
- public class DatabaseConnection {
- // MainWindow mainwindow = new MainWindow(); *** don't do this ***
- MainWindow mainwindow;
- public DatabaseConnection(MainWindow mainwindow) {
- this.mainwindow = mainwindow; // pass in the reference in the constructor
- }
- public void connect() {
- Connection conn = null;
- // ... etc
- // now we can call methods on the actual visualized object
- mainwindow.setStatusLabel("Connection");
- }
- public class MainWindows extends JFrame {
- private JLabel status_label;
- public MainWindows(){
- status_label = new JLabel("Status");
- }
- public void setStatusLabel(String staus){
- status_label.setText(staus);
- }
- /*.
- *
- * your code for visualizing
- .
- .
- .
- .
- .
- .*/
- public static void main(String args[]){
- MainWindows mw = new MainWindows();
- //this will return true if the connection will estblished and will false if not
- mw.setStatusLabel("Connection established "+ new DatabaseConnection().connect());
- }
- }
- public class DatabaseConnection{
- public boolean connect(){
- Connection conn = null;
- // adding this new variable to get connection status;
- boolean returnStatus = false; //default return false if connection established it will true .
- try {
- String userName = "root";
- String password = "";
- String url = "jdbc:mysql://localhost:3306";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, userName, password);
- //This works
- System.out.println("Connection Established");
- //The issue is with this guy
- returnStatus = true; // making status true if connection is established
- return returnStatus;
- }
- catch(Exception e) {
- System.err.println("Failed to connect to database");
- return returnStatus;
- }
- }
- }
Add Comment
Please, Sign In to add comment