Lab 1
Lab 1
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”);
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
24
24
Processing data returned by the
DBMS
Example
while(rs.next()) Returns the value of
specified Column
{ number
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
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
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”, “ ”);
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);
}
}
}