Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

the dbutils (http://www.oschina.net/project/284) has a bug

we need to use getColumnLabel but getColumnName

Posted by oschina on Sunday, 11.16.08 @ 04:50am | #81713

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.