I am using sql server 2008. I need to check if I can establish connection using server name,port number,db name,user name and password.
Is there any simple method available?
connection. close(); is a normal JDBC connection method; it (usually) closes the associated objects and releases the connection; it's not uncommon to explicitly close statements and resultsets instead of depending on connection.
Connection. close method, JDBC does not close the physical data source connection. Instead, JDBC closes only JDBC resources, such as Statement or ResultSet objects. The data source returns the physical connection to the connection pool for reuse.
Here is the sample JAVA program that checks whether the connection is successfully established to DB server or not.
public class TestDBConnection {
public static void main(String[] args) {
String Url = "jdbc:sqlserver://serverURL;DatabaseName=DBname;user=dbUsername;Password=dbPassword";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Trying to connect");
Connection connection = DriverManager.getConnection(Url);
System.out.println("Connection Established Successfull and the DATABASE NAME IS:"
+ connection.getMetaData().getDatabaseProductName());
} catch (Exception e) {
System.out.println("Unable to make connection with DB");
e.printStackTrace();
}
}
}
Hope this will work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With