Java Database Connections | JDBC Tutorial

375 views
optmiize_your_resume_1

JDBC (‘Java Database Connectivity’) allows multiple implementations to exist and be used by the same application. 

Steps for connection

Let have a look at how an application is connected to the database in java.

  1. Loading a Driver

    A program can also explicitly load JDBC drivers at any time. The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.
    Syntax of forName() method
    public static void forName(String className)
    throws ClassNotFoundException
    Example to register the mySql.Driver class
    Class.forName(“mysql.Driver”);

  2. Establishing connection

    The getConnection() method of DriverManager class is used to establish connection with the database.
    Syntax of getConnection() method
     DriverManager.getConnection (url,user,password);
    Example to establish connection with the MySql database
    Connection con=DriverManager.getConnection (“jdbc:mysql://localhost:3306/dbl”,”root”,”password”);  

  3. Preparing Statement

    The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.
    Syntax of createStatement() method
    public Statement createStatement()throws SQLException  
    Example to create the statement object
    Statement stmt=con.createStatement();  

  4. Executing Statement

    PreparedStatement has several method to execute query.
    1.execute()
    2.executeQuery
    3.executeUpdate

  5. Getting results

    The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.
    Syntax of executeQuery() method
    public ResultSet executeQuery(String sql)throws SQLException  
    Example to execute query
    ResultSet rs=stmt.executeQuery(“select * from emp”);  
    while(rs.next()){  
    System.out.println(rs.getInt(1)+” “+rs.getString(2));  
    }  

  6. Closing Database Connection

    By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.
    Syntax of close() method
    public void close()throws SQLException  
    Example to close connection
    con.close();  

JDBC Connection Example

Let’s have a look at the below example program.We are connecting to an Oracle database and getting data from emp table. Here, system and oracle are the username and password of the Oracle database.

import java.sql.*; class Oracleconnection{ public static void main(String args[]){ try{ //step1 load the driver class Class.forName("oracle.jdbc.driver.OracleDriver"); //step2 create the connection object Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); //step3 create the statement object Statement stmt=con.createStatement(); //step4 execute query ResultSet que=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(que.getInt(1)+" "+que.getString(2)+" "+que.getString(3)); //step5 close the connection object con.close(); }catch(Exception e){ System.out.println(e);} } }
Code language: Java (java)

20%off for Ink 280 281

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More