1 of 11

ADVANCED JAVA PROGRAMMING

java.sql package

Smt.M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 11

OBJECTIVES

The Objective of this lecture is

  • To learn about different classes and interfaces defined in java.sql package.

3 of 11

java.sql package

  • We use the following classes and interfaces that belong to java.sql package.

Classes:

  • DriverManager
  • Date
  • Time Interfaces:
  • Connection
  • Statement
  • PreparedStatement
  • CallableStatement
  • ResultSet

4 of 11

java.sql package

DriverManager class:

  • It is used to load the JDBC driver in the memory.
  • It is used to open a connection with the desired database.

Methods:

  • public static void registerDriver(Driver d): It registers the driver with the DriverManager class.
  • public static Connection getConnection(String URL): It tries to establish the connection to a given database URL.
  • public static void deregisterDriver(Driver driver): It is used to deregister the given driver (drop the driver from the list) with DriverManager.

5 of 11

java.sql package

Date class:

  • The java.sql.Date is used in JDBC because it represents

the date that can be stored in database.

  • It inherits java.util.Date class.

Methods:

  • void setTime(long time): It changes the current sql date

to given time.

  • Instant toInstant(): It converts current sql date into Instant object.
  • LocalDate toLocalDate(): It converts current sql date into LocalDate object.
  • String toString(): It converts this sql date object to a

string.

6 of 11

java.sql package

Time class:

  • It is a part of java.sql package.
  • This class is a thin wrapper around java.util.Date that

allows JDBC API to identify this as a SQL TIME value.

Methods:

  • void setTime(long time): It changes the current sql date

to given time.

  • Instant toInstant(): It converts current sql date into

Instant object.

  • LocalDate toLocalDate(): It converts current sql date into

LocalDate object.

  • String toString(): It converts this sql date object to a

string.

7 of 11

java.sql package

Connection Interface:

  • It is an interface which represents connectivity

Connection

with the database.

Syntax:

con=DriverManager.getConnection(“url”);

  • We can use the connection object for the following things.
  • It creates the Statement, PreparedStatement, CallableStatement objects for executing sql statements.
  • It helps to commit or rollback a JDBC transaction.
  • It helps to close the database connection.

8 of 11

java.sql package

Statement Interface:

  • It is used to execute static SQL statements.

Syntax:

Statement st=con.createStatement();

Methods:

public ResultSet executeQuery(String sql): It is used

for executing only select query. It returns the object of ResultSet interface.

public int executeUpdate(String sql): It is used for executing DML(insert, delete & update) queries. Return type is integer.

public boolean execute(String sql): It is used to execute

any type of sql query. It returns a Boolean value.

9 of 11

java.sql package

PreparedStatement Interface:

  • It is derived from the Statement interface.
  • It is used to execute queries with unknown parameters or where in the parameters are provided at runtime.

Syntax:

PreparedStatement ps=con.prepareStatement(“sql query”);

CallableStatement interface:

  • It is derived from the PreparedStatement interface

and useful in executing stored procedures.

  • The prepareCall() method is used

to create

CallableStatement object.

10 of 11

java.sql package

ResultSet Interface:

  • It represents a set of data in the form of table which is generated by executing certain specific SQL statements.

Syntax:

ResultSet rs=st.executeQuery(“select * from table name”);

11 of 11

THANK YOU