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: https://www.youtube.com/watch?v=vsv62tuO4LI

                      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- http://www.comrevo.com/2017/07/how-to-run-jdbc-program-in-command-prompt-in-linux.html
         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.
e.g. 
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.
e.g.
rs.close();
st.close();
con.close();

                   Check jdbc code for fetching data from database in this post: http://www.comrevo.com/2017/07/jdbc-code-to-retrieve-data-from-mysql-database.html

                  Check jdbc code for inserting data into mysql database in this post: http://www.comrevo.com/2017/07/jdbc-code-to-insert-data-into-mysql-database.html



No comments:

Post a Comment