Thursday 20 July 2017

Basic steps in jdbc code

                       In this post, we will see the basic steps in jdbc starting from connection with database to the execution of SQL queries, Basic Steps in JDBC Code | Standard Steps of Developing JDBC Application |Java Database Connectivity | basic steps in jdbc code,standard steps of developing jdbc application,jdbc code,jdbc code in java,java database connectivity,java database connection jdbc and mysql

Watch this video to know about Basic steps in JDBC Code:

Watch on YouTube:

                      Following are the basic steps:

1. Import the package.
 e.g. import java.sql.*;

2. Load and Register the Driver.
          Load: The jdbc driver used for connection should be available in your system. If you are using Eclipse IDE, then you have to provide link in properties while if you are running your jdbc code on terminal, then you have to provide classpath in .bashrc file. Check details here-
         Register: In jdbc code, we need to register a driver for use. A method forName() is provided for the same purpose.
e.g. Class.forName("com.mysql.jdbc.Driver");

3. Establish a Connection to the database
           Create a Connection object. Provide URL (with database port number, database name), user name and password.
e.g. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","username","userpassword");

4. Create a Statement object from the Connection.
           Statement object is used for executing queries on database.
e.g. Statement st=con.createStatement();

5. Use the Statement object to execute query.
           If we are fetching data from database, then we need to define ResultSet object. We can also perform other operations like insert, update, delete on database table.
ResultSet rs=st.executeQuery("select * from student");
int i=st.executeUpdate("insert into student values(1201,'se','vjti','mumbai')");

6. Process Result.
           If we are fetching data from the database, we can get it from ResultSet object. We can process this data as per our requirement.

7. Close/terminate the objects.

                   Check jdbc code for fetching data from database in this post:

                  Check jdbc code for inserting data into mysql database in this post:

No comments:

Post a Comment