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

JDBC Functions

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

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

Share on Google+Share on Google+

JDBC Functions

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

Advertisements

 

Discuss: JDBC Functions  

Post your Comment


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

Ads

 

Ads