|
UStudy aims to provide Educational Content for Polytechnic College students based on the latest K Scheme syllabus of Tamil Nadu. |
The java program can access the database using JDBC. the seven steps that are needed to access database are
Step1: Import java.sql package
The important classes and interfaces of JDBC are available in the java.sql package . So inorder to access the databases it must be imported in our application.
import java.sql.*;
Step 2: Loading and registering the driver
In order to access database it must have the particular driver for that database. In Java we have to inform the Drive Manager about the required driver this is done with the help of the method forName(String str) available in the class named Class
Class.forName(“path with driver name”);
In an application the user can register more than one driver. The statement used to load and register the JDBC-ODBC bridge driver is
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Step 3: Establishing the connection
Establishing connection means making a connection to access RDBMS through JDBC driver. The connection can be established with the help of getConnection() method available in DriverManager class. The established connection is then set to new connection object.
Connection cn=DriverManager.getConnetion (“jdbc:odbc:DSN”, ”administrator”, “password”);
Where DSN-Data Source Name
When the statement is executed the connection is set to the Connection object cn. For example to connect the ODBC data source named mydata via JDBC –ODBC bridge the statement is
Connection cn= DriverManager.getConnection (“jdbc:odbc:mydata”);
Step 4: Creating the statement
A Statement object is used for executing a static SQL statement and obtaining the results produced by it. The createStatement() method is used to create statements using the established connection.
The methods used for creating the statements are
Returns a new Statement object.Used for general queries
Returns a new PreparedStatement object.For a statement called multiple times with different values (precompiled to reduce parsing time)
Returns a new CallableStatement object for stored procedures
Step 5: Executing the Query
After making connection to the database we are ready to execute the SQL statements. The various methods available in Statement object to execute the query are
Execute a SQL statement that returns a single ResultSet. After executing the SQL statements the requested data is stored in the ResultSet object.
Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.
Execute a SQL statement that may return multiple results.
Step 6: Retrieving the result.
A ResultSet provides access to a table of data generated by executing a Statement.Only one ResultSet per Statement can be open at once.The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The 'next' method moves the cursor to the next row.
Methods:
Moves the cursor to the first row in this ResultSet object.
Moves the cursor to the last row in this ResultSet object.
Moves the cursor down one row from its current position.
The methods used to retrieve the values from the current row
Returns the given field as the given type. Fields indexed starting at 1 (not 0)
Returns the data at the specified column
In the above methods the Type should be replaced by the valid datatypes such as Boolean, String, Int, Long, Float etc as getString(Column name), getInt(column index) etc.
Step 7: Closing the connection and statement
When the client request is completed we have to close the created objects of Connection and Statement using close() method.
st.close();
cn.close();
JDBC ----PowerPoint: