import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCConnectionExample {
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
// load the jdbc driver. Every database has it's
// own driver.
Class.forName("com.mysql.jdbc.Driver");
// Provide the host url of your database,
// username and password. In case of Oracle
// it is like jdbc:oracle:thin:@127.0.0.1:1521:orcl.
// So depending upon your database location and
// schema name you have to set the configurations
// like above. In case of mysql please refer
// the settings below. Provide valid user
// name and password.
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/database_name",
"dummy_user_name",
"dummy_password");
}
}
I know this URL jdbc:oracle:thin:@127.0.0.1:1521:orcl seem to be little confusing to you. So I would like to take some of your time to explain it to you.
jdbc - prefix used to present that it is a JDBC url.
oracle - which database provider it refers to. In case of Mysql it will be "mysql".
thin - presents which kind of driver to use. There are four type of drivers.
127.0.0.1:1521 - IP address and port number on which the database is listening for connection.
orcl - SID name. It is actually the database instance name.
|