Java Database Connectivity is an API(Application Program Interface) or platform independent interface which helps to connect java programs with various databases such as Oracle , MySql , MsAccess and Sql Server .
→It provide ways to query and update the database using Structured Query Language (SQL) .
To connect the java program or application with the database , you need 5 steps — — —
1. Load the Driver :- helps to make a connection to the database .
→Method used is Class.forName() —
— Class.forName(“oracle.jdbc.driver.oracleDriver”); you can use mysql also.
Using this, the driver class file is loaded in the memory during run time.No need to create an object.
→Method used is DriverManager.registerDriver() — DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
2. Creating Connection :-
After the driver is loaded , connection is set up.The connection object uses username , password , URL.
Ex.. Connection con = DriverManager.getConnection(Url,User,password);
3.Creating Statement :-
After connection , the user can interact with database . the interfaces such as JDBC stmt , PreparedStatement provide methods that allow a user to send Sql statements and get data from the database.
Ex.. stmt = con.createStatement ());
4. Executing Query :-
A query can be for updating /inserting or retrieving .
Ex.. stmt.executeQuery(); — for retrieving
stmt.executeUpdate(); — for updating or inserting.
5. Closing Connection :-
Now the connection need to be closed.
Let’s see an example
Here , I used mysql database to store my data . Here first I created a class myDriver in which I make a connection using methods Class.forName and DriverManager .
Here, information is my table name in database student, user is root and password is also root.
Then , created a statement using createStatement .
Executed my query using executeUpdate method .
For confirmation that everything went well on console I printed a statement that “insert complete”
just to make sure again in ResultSet I stored and display my data on console using executeQuery(“select*from information”);
Thanks for giving your valuable time