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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JDBC Functions 
 

In this Tutorial we want to describe you a code that helps you easy to understand Jdbc Functions.

 

JDBC Functions

                         

In this Tutorial we want to describe you a code that helps you easy to understand Jdbc Functions. The code include a class Jdbc Functions, Inside the main method, the list of following method steps is to be carried out given below - 

List of packages include:

  Import java.sql provides you all the list of classes that provides you network interface for communicating between the front end java application   and backend database.

  Import Java.util contain a collection framework that provides you a legacy class, event model,date,time facilities.

  After importing the required packages, we create an object of HashMap.This object store key value pair. The value can be duplicated, but key must be unique.

  map. put ( ) :  This is used to associate the specified value with the specified keys in this object.

  Define a  function void Jdbc ( Hash Map map), that accept map object as parameter and retain all the value from map object.

  map. get ( ) : The method return you the value to the specified key is mapped in identity hash map, or return null if the map do not contains mapping for specified key.

  Loading a driver by calling a class.forname( ),that accept driver class as argument.

  DriverManager.getConnection ( ) : This method return you an object of connection. This is used to built a connection between url and database.

  con.createStatement ( ) : This return you an sql object, An connection object  send and execute the sql query in the backend.

   execute query( ) : This is used to retrieve the record set from a table using select statement from the backend table.

   On the execution of program,  the println print the record set from database using -

    rs.getString( ) :  This Return you the record set on HTML Page.

   Incase there is an exception in try block, the subsequent catch block caught and handle the exception.

JdbcFunctions.java

 

import java.sql.*;
import java.util.*;

public class JdbcFunctions {

    public static void main(String args[]) {

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        HashMap map = new HashMap();
        map.put("driver", driver);
        map.put("url", url);
        map.put("db", db);
        map.put("user", user);
        map.put("pass", pass);

        new JdbcFunctions().jdbc(map);
    }

    void jdbc(HashMap map) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String driver = map.get("driver").toString();
        String url = map.get("url").toString();
        String db = map.get("db").toString();
        String user = map.get("user").toString();
        String pass = map.get("pass").toString();

        try {
            
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);
            st = con.createStatement();

            String sql = "select * from person";
            rs = st.executeQuery(sql);

            System.out.println("no.  \tName      \tDob");
            while (rs.next()) {
                System.out.print(rs.getString("id") + "   \t");
                System.out.print(rs.getString("cname") + "   \t");
                System.out.println(rs.getDate("dob"));
            }
        catch (Exception e) {
        }
    }
}

Output

no.  	Name      	Dob
1   	Girish   	1984-06-02
2   	komal   	1984-10-27

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 
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.