Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday, 19 January 2018

JDBC Statement Interface in Java with Example

Statement Interface in JDBC

Statement Interface in Java

In this jdbc tutorial, We are gonna to discuss jdbc Statement interface in java with example so that you can understand what is the roll of Statement interface in jdbc.

Jdbc Statement interface is used to execute queries with the database by using some methods which is provided by Statement interface.

After making connection with the database by using Connection interface we can get the object of a Statement interface by using createStatement() method of Connection interface e.g...

Statement stm = con.createStatement();

By using Statement interface methods, You can retrieve, insert, delete and update data into the database.


Mostly Used Methods of Statement Interface

There are some methods of Statement interface which is used to execute queries with the databse.

1) public ResultSet executeQuery(String sql)

It is used to execute the select query and returns the object of ResultSet.

2) public int executeUpdate(String sql)

It is used to execute the query like create, insert, delete and update.

3) public boolean execute(String sql)

It is used to execute query that may return multiple results.

4) public int[] executeBatch()

It is used to execute batch of commands.

Let's understand Statement interface by simple example.

Read this : Database Connectivity in Java with MySql


JDBC Statement Interface Example

In this example, we are going to fetch all the record from database table by executing select query with the help of executeQuery() method. You can execute query like create, insert, update, delete etc. But here we will execute only select query.

Suppose there is a table(student table with age and name) in the oracle database with some records.

import java.sql.*;
class Demo
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "bca");

Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from student");

While(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}

catch(Exception e)
{
System.out.println(e);
}
}
}

Here we saw a simple example of java jdbc Statement interface.

Share:

5 comments:

  1. Thanks for sharing this good blog. It's very interesting and useful for students
    Visit: Best Java Course

    ReplyDelete
  2. Thanks for sharing this informative blog java training in OMR

    ReplyDelete
  3. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  4. Thanks for your post which gathers more knowledge. I read your blog. Everything is helpful and effective.

    Protocloud provides the best digital marketing course in Jaipur from the best-dedicated trainer with 100% Practical and lives according to your strength and lack.
    Protocloud trainers grow knowledge and confidence in every student about digital Marketing.
    Protocloud will provide you a certificate and prepare you for Google Certifications, which will give you many advantages. That has its value in the market.
    Protocloud team grow interview skill to every student Because
    When our students go to any company for interviews. They find it very easy to crack that interview rather than other students appearing there.

    ReplyDelete
  5. As we all know that mobile apps are the biggest requirement of firms. Protocloud Academy is a leading Android Mobile app development institute in Jaipur. Our mentors will guide you to translate software requirements into workable programming code and maintain and develop programs with the help of live project training. They will also give you in-depth concepts knowledge in Android Application Development, JAVA language, and fine use of the Latest Android Studio and deployment on Android Devices like Smart Phone or Android Tablets. After completion of the training program, our experts will give advanced training to guide students with the latest technologies. We will provide the training certificates by approved technology partners to help students crack the interview and get their dream job.

    https://www.protocloudtechnologies.com/android-app-development-course-in-jaipur/

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate