1. Create Connection
Connection is session between database and JDBC, before accessing database it is established. This connection helps to send SQL query to database and return back to client with output. Connection is interface java.sql.Connection
<%
Connection conn=null;
%>
2. Load JDBC Driver
JDBC Driver class is load into java virtual machine (JVM). The Class.forName() method is used to load vendor database class into JVM, it has return type of class. It is registering Driver class.
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
%>
In JDBC 4.0, it is no longer needed to load vendor class manually. JDBC 4.0 automatically loads Driver class and registers it.
3. Open Database connection
After loading and registering Driver class, have to make database connection with method of DriverManager.getConnection(), returns as Connection. JDBC 4.0 version only need DriverManager.getConnection() method, it finds automatically suitable Driver from list for itself. It is in java.sql.Driver package.
<%
Connection conn =DriverManager.getConnection(jdbcURL, dbUser, dbPassword);
%>
e.g for MySql database we can use jdbcURL string.
<%
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root", "");
%>
project is database name, root is database’s user name with empty password.
|