A simple JDBC connection tester
Having installed the needed softwares I wanted to test them. To configure the MySQL and JDBC driver please read this article.
For running the below file, do not forget to start your MySQL Database.
Also do replace the <DATABASE_NAME> & <MySQL_DB_SERVER_PASSWORD>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcConTest {
public static void main(String args[]) {
Connection con = null;
Statement stmt = null;
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
con = DriverManager.getConnection(“jdbc:mysql:///<DATABASE_NAME>”, “root”,
“<MySQL_DB_SERVER_PASSWORD>”);
if (!con.isClosed()) {
String dbPdtName = con.getMetaData().getDatabaseProductName();
String dbPdtVer = con.getMetaData().getDatabaseProductVersion();
int dbMajVerKey = con.getMetaData().getDatabaseMajorVersion();
int dbMinVerKey = con.getMetaData().getDatabaseMinorVersion();
String dbURL = con.getMetaData().getURL();
String dbUserName = con.getMetaData().getUserName();
String qryDB = “select * from user”;
stmt = con.createStatement();
System.out.println(“Successfully connected to MySQL server…”);
System.out.println(“Database PdtName : ” + dbPdtName);
System.out.println(“Database PdtVer : ” + dbPdtVer);
System.out.println(“Database MaxVK : ” + dbMajVerKey);
System.out.println(“Database MinVK : ” + dbMinVerKey);
System.out.println(“Database URL : ” + dbURL);
System.out.println(“Database UsN : ” + dbUserName);
stmt.executeQuery(qryDB);
ResultSet recSet = stmt.getResultSet();
while (recSet.next()) {
System.out.println(“The host is : “
+ recSet.getString(“Host”));
System.out.println(“The User is : “
+ recSet.getString(“User”));
}
}
} catch (Exception e) {
System.err.println(“Exception: ” + e.getMessage());
} finally {
try {
if (con != null)
con.close();
} catch (SQLException e) {
System.err.println(“Exception: ” + e.getMessage());
}
}
}
}





























