Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Tutorial: Java Database Connectivity

runtime

  • Please log in to reply
1 reply to this topic

#1 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 May 2008 - 01:28 PM

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.

Posted Image


How to use it

1) Declaring a Connection reference variable which will store the connection details. You do this by
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.

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 ip:port number where the database server listens.
[I]variable_name=DriverManager.getConnection ("type_of_connectivity:database://ip_address:port_number/db_name", "username",  "password"); 
[/I]
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.

[I]package com.mycomputer.connectDB;

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

public class DBConnection 
[/I]{[I]
    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(); 
    }
[/I]}
Output
Connected to the database

Posted Image

  • 0

#2 Coldhearth

Coldhearth

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 88 posts

Posted 08 October 2008 - 01:26 AM

So let's say you want to connect to the MySQL database in another class (one that might represent let's say the GUI of a login window). How would you do it then?
Is it creating a new reference variable of the type Connection and do:
Connection conn = new DBConnection();

And then use that reference variable to call some other methods for let's say querying the database...?
  • 0





Also tagged with one or more of these keywords: runtime

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download