Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC access database

JDBC is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC access database

     

JDBC is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections. 

Understand with Example

In this Tutorial we want to describe you a code that helps in understanding JDBC access database. The code illustrates the list of following steps-

  1. The first and foremost step is to import a package import java.sql.*;This package import the list of all classes defined in java.sql.*.
  2. database driver Loading - The second step is to load the driver class by invoking Class.forName( ) with the Driver class name passed as an argument. Once your driver is loaded ,you can connect the front end of the Java application to the backend database. In case there is an exception occurred  in loading a database in the try block. The catch block check the exception and println print the exception.
  3. st = con.createconnection( ) -This is used to create Sql object. Once a connection is established ,you can interact with backend database. A connection object is used to send and execute SQL Statement to a backend database.
  4. rs = st.executequery(sql)   -  This is used to for select query in sql and the value retrieved from it stored in  result set rs.The Result Set enables you to access a table containing data. The table row retrieved in a sequence.
  5. next( ) -  The next method is used to retrieve successively element through the rows obtained from a result set.

Finally the println print the Id and name from the result set obtained from database. lastly all the result set, connection and statement is closed.

JdbcGetInt.java

import java.sql.*;
public class JdbcGetInt {
    public static void main(String args[]) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            st = con.createStatement();
            String sql = "select * from person";
            rs = st.executeQuery(sql);
            
            System.out.println("Id\tName");
            while (rs.next()) {
                System.out.print(rs.getInt(1));
                System.out.println("\t"+rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output

Id	Name
4	Komal
5	Ajay
6	Santosh

Download code

Advertisements

Share on Google+Share on Google+

JDBC access database

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC access database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads