Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>what i have tried to do is that, you return the status of connection and print it on the mainWindow status_jlable, here you don't need to pass the reference of main window in the database class your just returning the status of connection and thats all you need.</p> <pre><code>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()); } } </code></pre> <p>/////////////database class</p> <pre><code> 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload