Lost Password?

  #1 (permalink)  
Old 05-07-2008, 04:28 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,512
Last Blog:
Zend: PHP Security Web...
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: Java Database Connectivity

Java Database Connectivity

What is JDBC?
JDBC is nothing but Java DataBase Connectivity. As the name suggests, it deals with connecting you code business logic to a database.

Types of Connections
There are basically 4 types of drivers which you can use to connect to the database:

Type 1 : JDBC-ODBC bridge
Type 2 : Partial Java driver
Type 3 : pure Java driver for database middleware
Type 4 : pure Java driver for direct-to-database


The most widely used and supported by different vendors are the type 2 and type 4 drivers. They are easy to use and portable across various databases, and we will discuss the same here.




How to use it

1) Declaring a Connection reference variable which will store the connection details. You do this by
Code:
 Connection  variable_name;
2) Then you search the Driver provided by the database designer/creator, and sometimes by Java, which will be used to connect to the database.
The class is loaded by using Class class's forname method.

Code:
Class.forname("Path_of_the_class");

3)
Once the driver is loaded, it is time to connect to the database. But to connect to it, you need the username and password and the iport number where the database server listens.
Code:
variable_name=DriverManager.getConnection ("type_of_connectivity:database://ip_address:port_number/db_name", "username",  "password"); 
This connection is referenced by the reference variable of the Connections class.
The various objects that are used for SQL are:
  • PreparedStatement: Used when your application is going to reuse a statement multiple times. Once prepared, the you can specify the values at runtime.
  • CallableStatement: A CallableStatement is used to call stored procedures that return values. It has methods for retrieving the return values of the stored procedure.
  • ResultSet: Contains the results of a query. You can iterate through all the rows obtained by the various methods provided by the ResultSet class.

The Code
Note :: We are connecting to MySQL database. The database name is test and username and password is root.

Code:
package com.mycomputer.connectDB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection 
{
    Connection conn;
    public DBConnection() 
    {
        try 
        {
            Class.forName  ("com.mysql.jdbc.Driver");
            conn =  DriverManager.getConnection ("jdbc:mysql://localhost:3306/test", "root",  "root");
             System.out.println("\nConnected to the database");
        }
        catch(ClassNotFoundException e)
        {
             System.out.println("\nClass not found. Check Path");
             e.printStackTrace();    
        }
        catch (SQLException e) 
        {
             System.out.println("Not connected");
             e.printStackTrace(); 
        }
    } 
    public static void main(String[] args) 
    {
        new DBConnection(); 
    }
}
Output
Code:
Connected to the database

__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
database, java, jdbc, tutorial



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Storing Images in MySQL with PHP Jordan PHP Tutorials 10 06-22-2008 07:02 PM
Tutorial: Starting Java Using Netbeans Jordan Java Tutorials 0 04-05-2008 02:45 PM
Java tutorial : my sql with java Arkie Java Tutorials 2 04-05-2008 12:51 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 03:14 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads