import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JavaMysqlDatabaseConnection {
public static void main(String args[]) {
Connection connection = null;
public String databaseName = "yourDatabaseName";
public String userName = "yourUserName";
public String userPassword = "yourUserPassword";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://"+databaseName+"",
userName , userPassword);
if(!connection.isClosed()) {
System.out.println("Successfully connected to Database");
} else {
System.out.println("Not Connected to Database");
}
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(connection != null)
connection.close();
} catch(SQLException e) {}
}
}
}
Step by Step get a Connection;
1. Open your development platform.
2. Craete a java class.
3. Copy this code to your class.
4. Change parameters according to your database, and get a connection.
In above example, "com.mysql.jdbc.Driver" is the name of the JDBC driver that you want to load.
I wish to be useful.