0% found this document useful (0 votes)
5 views30 pages

Lab 1

Uploaded by

Preeti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Lab 1

Uploaded by

Preeti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

LAB 1

Step 1: Loading JDBC Driver


 Create an instance of the driver
 Register driver in the driver manager
 Loading the driver or drivers
for example, you want to use driver for mysql, the
Returns the Class object associated
following code willwith
loadtheit:
class or interface with the
given string name.

Class.forName("com.mysql.jdbc.Driver”);
Class that Main
represent classes is Pakage
Class.forName() It is used to
andused
interfaces
for loading
in a class initiate Driver at
Sub-
running
dynamically Java runtime
Pakage
application.

16
16
Step 2: Connection to DBMS
 After you've loaded the driver, you can establish a
connection using the DriverManager class
(java.sql.DriverManager).
public static
Method: Connection
DriverManager Attempts to establish a connection to
getConnection(String url) throws the given database URL.
SQLException The DriverManager attempts to select
an appropriate driver from the set of
registered JDBC drivers.
public static Connection Attempts to establish a connection to
getConnection(String url, the given database URL.
String user, url - a database url of the
String password form jdbc:subprotocol:subname
) user - the database user on whose
throws SQLException behalf the connection is being made
password - the user's password

17
17
Step 2: Connection to DBMS
Syntax:
Interface of java.sql
package

Connection conn=
DriverManager.getConnection(URL,USER_NM,PASS)
; Class of java.sql
package

Example:
Connection conn = DriverManager.getConnection
Database
Name
("jdbc:mysql://localhost:3306/amity","root", “ ");

18
18
Step 3: Creating statement
 Once a connection is obtained, we can interact with
the database.
 The JDBC Statement interfaces define the methods
and properties that enable you to send SQL or PL/SQL
commands and receive data from your database.

InterfaceStatement st=con.createStatement();
is used for general- Statement createStatement()
purpose access to your throws SQLException
database, when using static Creates a Statement object for
SQL statements at runtime. sending SQL statements to the
database.

19
19
Step 3:Executing Statement
 Once you've created a Statement object, you can
then use it to execute an SQL statement with one of
its three execute methods.
ResultSet executeQuery(Stri Returns a ResultSet object. Use this
ng sql) method when you expect to get a
throws SQLException result set, as you would with a
SELECT statement.
Boolean execute(String sql) Returns a boolean value of true if a
throws SQLException ResultSet object can be retrieved;
otherwise, it returns false.
int executeUpdate(String sql Returns the number of rows affected
) by the execution of the SQL
throws SQLException statement.
for example, an INSERT, UPDATE, or
DELETE statement.

20
20
Step 3: Executing
Statement
Syntax:
ResultSet rs=st.executeQuery(“query”);

It holds data retrieved from a Returns a ResultSet object.


database after you execute Use this method when you
an SQL query using expect to get a result set, as
Statement objects. It acts as you would with a SELECT
an iterator to allow you to statement.
move through its data.

Example
ResultSet rs = stmt.executeQuery
("SELECT * from amity");

21
21
Step 3: Executing
Statement
ResultSet rs = stmt.executeQuery
("SELECT * from amity");
ResultSet rs
Enr_no Name Branch
601 abc ce
602 pqr me
Database
603 rst ec
604 def Ci

22
22
Step 3: Executing
Statement
ResultSet rs = stmt.executeQuery
("SELECT * FROM thirdcse WHERE
rollno = '"+sid+"'");
ResultSet rs
Enr_no Name Branch
Database
601 abc ce
602 pqr me

23
23
Step 4:Processing data returned by
the DBMS
 Method: Resultset
boolean next() Moves the cursor forward one row from its
Throws SQLException current position.
String getString Retrieves the value of the designated column in
(int col_Index) the current row of this ResultSet object as
throws SQLException a String

String getString Retrieves the value of the designated column in


(String col_Label) the current row of this ResultSet object as
throws SQLException a String in the Java programming language.
int getInt Returns the int in the current row in the
(int columnIndex) throws specified column index.
SQLException
int getInt Retrieves the value of the designated column in
(String columnLabel) the current row
throws SQLException

24
24
Processing data returned by the
DBMS
 Example
while(rs.next()) Returns the value of
specified Column
{ number

res="The student name


is"+rs.getString(2)+" mark
is"+rs.getInt(4); }

25
25
Step 5:Terminating Connection with
DBMS
 The connection of DBMS is terminated by using
close() method. Releases
this ResultSet object's
Example database and JDBC
resources immediately
rs.close(); Releases
this Statement object's
database and JDBC
st.close(); resources immediately

con.close(); Releases this


Connection object's
database and JDBC
resources immediately

26
26
JDBC with different RDBMS
RDBMS JDBC driver name URL format
MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/databaseName
ORACLE oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:port
Number:databaseName

DB2 com.ibm.db2.jdbc.net.DB2Driver jdbc:db2:hostname:port Number


/databaseName

Sybase com.sybase.jdbc.SybDriver jdbc:sybase:Tds:<host>:<port>


SQLite org.sqlite.JDBC jdbc:sqlite:C:/sqlite/db/databaseName
SQLServer com.microsoft.sqlserver.jdbc.SQLServ jdbc:microsoft:sqlserver:
erDriver //hostname:1433;DatabaseName

27
27
JDBC Programming

1. Introduction
2. JDBC API
3. The JDBC Connectivity Model
4. JDBC Architecture
5. JDBC Driver
6. JDBC Components
7. JDBC Package
8. JDBC Process
9. JDBC Program
10. Types of Statement
11. ResultSet Interface
12. ResultSetMetaData Interface
13. Executing SQL updates 28
28
JDBC Program
Class.forName("com.mysql.jdbc.Driver");

Connection conn=
DriverManager.getConnection
("jdbc:mysql://localhost:3306/amity",
“root”, “ ”);

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT
* from
amity");

while(rs.next())
System.out.print(rs.getString(1));

stmt.close();
29
conn.close();
29
import java.sql.*;
class SampleOne
{
public static void main(String args[])
{
String res="";
Connection con=null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/amity","root","");
st=con.createStatement();
String sid="A001";
rs=st.executeQuery("select * from thirdcse where rollno = '"+sid+"'");
while(rs.next())
{
res="The student name is"+rs.getString(1)+" mark is"+rs.getInt(4);
}
System.out.println(res);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

You might also like