Type 4 drivers or Native protocol driver is known to be the one with the best features and functions. Type 4 driver does not have any native methods and is a complete Java driver. You need not to install it on the client before use and the can be easily downloaded or configured on a system very easily. One of the best features of Type 4 driver is that it interacts directly with the DBMS server.
Firstly, you need to register the type of driver that you are using by using the string,
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
And then connect to the database by use of the connection string:
Connection con = DriverManager.getConnection(url, uid, password);
Here, url is the JDBC url, uid is the username for the database and its password.
Example for Database connection:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class Hello {
- if( args.length != 4 ) {
- return;
- }
- try { // Loading driver
- Class.forName(args[0]);
- }
- e.printStackTrace();
- return;
- }
- try {
- }
- e.printStackTrace();
- }
- finally {
- try {
- connection.close();
- }
- e.printStackTrace();
- }
- }
- }
So, next time when you are planning for database programming then use JDBC in your application to make it simple and easy to use.
- Add new comment
- 31 views