Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc JDBC Functions

Related Tutorials


 
 

Share on Google+Share on Google+

JDBC Functions

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2008

Related Tutorials

Discuss: JDBC Functions  

Post your Comment


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