I have Class called ConnectionDB and in this class I have Main method and another function in the class ConnectionDB,That is

private static Connection DBConnectionstring()

In this function I have Connection URL ,Drivers inside this function .

I want this function to be used in another class (GUILogin) with main function in it and Login button on it.
I want ActionListner of the Login button to get that class from ConnectionDB.
I click on the button I want to get connected to the Database.

Dani AI

Generated

Short answer: don’t keep a single Connection object in main and share it from the UI thread. Acquire a fresh Connection when you need it, close it promptly, and perform network/DB work off the Swing EDT. ’s idea (create or call the DB helper) and ’s helper approach are fine; is right to push for correct terminology and for avoiding long‑lived Connection fields.

Example pattern (interface + injectable provider + background work):

public interface ConnectionProvider {
  Connection get() throws SQLException;
}

public class ConnectionDB implements ConnectionProvider {
  private final String url, user, pass;
  public ConnectionDB(String url, String user, String pass) { ... }
  @Override
  public Connection get() throws SQLException {
    return DriverManager.getConnection(url, user, pass);
  }
}

/* In your GUI: */
new SwingWorker<Boolean,Void>() {
  protected Boolean doInBackground() {
    try (Connection c = provider.get();
         PreparedStatement ps = c.prepareStatement("SELECT 1")) {
      // check credentials safely
      return true;
    } catch (SQLException ex) {
      // log and handle
      return false;
    }
  }
}.execute();

If you run into TLS/SSL trust problems when connecting to SQL Server: the JVM likely does not trust the server certificate. Two practical fixes:

  • Import the server certificate into the JVM truststore (use keytool -list -keystore "$JAVA_HOME/jre/lib/security/cacerts" -storepass changeit to inspect, then keytool -importcert -alias sqlserver -file server.cer -keystore "$JAVA_HOME/jre/lib/security/cacerts" -storepass changeit).
  • For short-term testing with the Microsoft driver, add encrypt=true;trustServerCertificate=true to the JDBC URL (not recommended for production). Alternatively point the JVM at a custom truststore with -Djavax.net.ssl.trustStore=... and -Djavax.net.ssl.trustStorePassword=....

Final notes: use try-with-resources, PreparedStatement for user input, move DB calls off the EDT, and prefer a DataSource/pool for real apps. Good troubleshooting pointers from about certificate parameters are relevant here.

Recommended Answers

All 10 Replies

I believe you need to create an object of the class you want to use in main.
More detail here.

yourClassName objectName = new yourClassName();

also, get your terminology straight: Java doesn't have functions, it has methods.

if I'm not mistaken, Connection is not a class, it's an interface. (I assume your ConnectionDB implements Connection?)

private static Connection myConnection = new ConnectionDB();

hai chdboy,

as above experts said

you may also try like this

import java.sql.DriverManager;
import java.sql.Connection;

public class connectionDB 
{
    public static Connection getCon()throws Exception 
    {
       Class.forName("your_database_driver_name_goes_here");
       Connection con = DriverManager.getConnection("your_connection_string_here","user_name_here","password_here");
       return con;
    }
}

call the getCon() method wherever you want by using its class name directly as follows

Conncetion con = connectionDB.getCon()

let me know the status

any comments are much appreciated

The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "RSA premaster secret error". ClientConnectionId:9ffcc3af-96ed-4f55-b7f4-a49b958a8ac4

I get this error

check this

Now what after creating SSL certificate?

Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at java.security.cert.PKIXParameters.setTrustAnchors(Unknown Source)
    at java.security.cert.PKIXParameters.<init>(Unknown Source)
    at java.security.cert.PKIXBuilderParameters.<init>(Unknown Source)

No one is going to do your work for you, bro. :|

you just have passed pass wrong parameters in certificate... so just check it out and run your project again.

ok jalpesh_007 I will do that ,thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.