+ Reply to Thread
Results 1 to 2 of 2

Thread: Tutorial: Java Database Connectivity

  1. #1
    Jordan Guest

    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


  2. CODECALL Circuit advertisement

     
  3. #2
    Coldhearth is offline Learning Programmer
    Join Date
    Oct 2008
    Posts
    88
    Rep Power
    0

    Re: Tutorial: Java Database Connectivity

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Database Connectivity Problem?
    By MPL in forum PHP Development
    Replies: 1
    Last Post: 12-16-2010, 10:05 AM
  2. Replies: 1
    Last Post: 11-12-2010, 06:02 AM
  3. JAVA database connectivity
    By Ananta2010 in forum Java Help
    Replies: 3
    Last Post: 07-03-2010, 08:30 AM
  4. PHP Open Database Connectivity (ODBC) Tutorial
    By kailas in forum PHP Tutorials
    Replies: 0
    Last Post: 03-08-2010, 08:17 AM
  5. How to establish database connectivity ??
    By Patrick in forum Database & Database Programming
    Replies: 1
    Last Post: 10-08-2007, 05:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts