JDBC
CS 124
JDBC
 Java Database Connectivity
 Database Access Interface
 provides access to a relational database (by
allowing SQL statements to be sent and executed
through a Java program)
 JDBC package: set of Java classes that facilitate
this access (java.sql.*)
 Comes with JDK (since 1.1)
JDBC Driver
Need a driver, specific to the DB product, to
mediate between JDBC and the database
 the driver is a Java class that needs to be loaded
first
Relational
DBMS
Java Program
- load driver
- establish connection
- send SQL statements
JDBC-ODBC Bridge
 Driver that interfaces with ODBC (Object
Database Connectivity--also an access
interface)
 Easiest way to access databases created by
Microsoft products
 register database as an ODBC data source
 use JDBC-ODBC bridge as the JDBC driver
(included in JDK distribution)
Key Classes in JDBC
 Connection
 need to create an instance of this class when
establishing a connection to the database
 Statement
 for issuing SQL statements
 ResultSet (interface)
 a ResultSet object represents the table returned
by an SQL select statement
Establishing a Connection
Use the getConnection() method
 under the DriverManager class
 String argument: "jdbc:driver:name”
 returns a Connection object
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
// above line loads the jdbc-odbc driver
String dbname = “jdbc:odbc:MyDB”;
Connection c = DriverManager.getConnection(dbname);
Creating a
Statement Object
Execute the createStatement() method on the
Connection object
 returns a Statement object
 afterwards, run methods on the Statement object
to execute an SQL statement
Statement s = c.createStatement();
Methods of the
Statement Class
 Methods of the Statement class require a string
parameter containing the SQL statement
 executeQuery()
 requires a String argument (a SELECT statement)
 returns a ResultSet object representing the table returned
 executeUpdate()
 requires a String argument
(an INSERT, UPDATE, or DELETE statement)
 returns an int (row count, in most cases)
The ResultSet Interface
 A ResultSet object represents the table
returned by the select statement sent
 Navigation/retrieval methods
 next(): moves to the next row (first row if called
for the first time), returns false if no rows remain
 getXXX() methods return the value of a field for
the current row
ResulSet example
ResultSet rs;
rs = s.executeQuery(“SELECT * FROM [ORDER]”);
rs.next(); // gets the first row (use in a loop for multiple rows)
// suppose the ORDER table has an integer field
// called quantity
int myvar = rs.getInt(“quantity”);
// if you knew that quantity is the 2nd field in the table
myvar = rs.getInt(2);
Need braces because
ORDER is a reserved
word in SQL
Exercise
 Create a Microsoft Access table
 insert sample rows
 Add an ODBC data source
 use the Microsoft Access driver
 associate with the created database
 Create a Java program
 use JDBC-ODBC bridge
 create a loop that lists all rows of the table
executeQuery( ) example
…
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(
“SELECT * FROM STUDENT WHERE QPI > 3.0”
);
while ( rs.next() )
{
String name = rs.getString(“LastName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
executeUpdate( ) example
…
Statement s = con.createStatement();
int result;
result = s.executeUpdate(
“DELETE FROM EMPLOYEE WHERE DeptCode=‘CS’”
);
System.out.println( result + “ rows deleted.” );
The PreparedStatement class
 PreparedStatement: a Statement that
specifies parameters through Java code
 The SQL statements take different forms
when you specify different parameter values
 Useful when query is performed repeatedly
 Formatting of literal values is easier
Version 1 (Statement)
// suppose lastName is a String variable
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(
“SELECT * FROM STUDENT WHERE LastName = ‘” + lastName +”’”
);
while ( rs.next() )
{
String name =
rs.getString(“LastName”) + rs.getString(“FirstName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
Query string is built manually
Version 2 (PreparedStatement)
// suppose lastName is a String variable
PreparedStatement s = con.prepareStatement(
“SELECT * FROM STUDENT WHERE LastName = ?”
);
s.setString( 1, lastName );
ResultSet rs = s.executeQuery();
while ( rs.next() )
{
String name =
rs.getString(“LastName”) + rs.getString(“FirstName”);
int y = rs.getInt(“Year”);
double qpi = rs.getDouble(“QPI”);
System.out.println( name + “ ” + y + “ ” + qpi);
}
the appropriate literal
is “inserted” in the query
Summary
 JDBC allows you to write Java programs that
manipulate a database
 A driver (often a separate product) is required
that facilitates access
 Key classes: Connection, Statement,
PreparedStatement, and ResultSet
 Other features: metadata and stored-proc
invocation

More Related Content

PPT
JDBC Connecticity.ppt
PDF
Java 1-contd
PPTX
Jdbc presentation
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
PPT
statement interface
PPTX
Jdbc Java Programming
PDF
Core Java Programming Language (JSE) : Chapter XIII - JDBC
PPTX
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
JDBC Connecticity.ppt
Java 1-contd
Jdbc presentation
Java OOP Programming language (Part 8) - Java Database JDBC
statement interface
Jdbc Java Programming
Core Java Programming Language (JSE) : Chapter XIII - JDBC
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt

Similar to CS124-L9-JDBC.ppt Add more information to your upload (20)

PPT
3 database-jdbc(1)
PPT
Jdbc oracle
PPT
JDBC – Java Database Connectivity
PPTX
Retrieving data from database using result set (1)
PPT
Jdbc (database in java)
PDF
java4th.pdf bilgisayar mühendisliği bölümü
PPTX
Jdbc ppt
PPT
Jdbc
PPTX
Database Programming Techniques
PPT
Executing Sql Commands
PPT
Executing Sql Commands
PPT
Jdbc day-1
PPTX
PDF
PPT
Java jdbc
PDF
Jdbc 1
PPT
JDBC for CSQL Database
3 database-jdbc(1)
Jdbc oracle
JDBC – Java Database Connectivity
Retrieving data from database using result set (1)
Jdbc (database in java)
java4th.pdf bilgisayar mühendisliği bölümü
Jdbc ppt
Jdbc
Database Programming Techniques
Executing Sql Commands
Executing Sql Commands
Jdbc day-1
Java jdbc
Jdbc 1
JDBC for CSQL Database
Ad

Recently uploaded (20)

PPTX
dDifference Beetween Saving slides And Investment Slides.pptx
PDF
Topic-1-Main-Features-of-Data-Processing.pdf
DOCX
internal and external hardware in Orthopedic Center
PDF
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
PPTX
AI_ML_Internship_WReport_Template_v2.pptx
PDF
2- Physical Layer (06).pdfgshshshbsbshshshhs
PPTX
Pin configuration and project related to
PPTX
Unit-1.pptxgeyeuueueu7r7r7r77r7r7r7uriruru
PPTX
Growth Capital Investment - Espresso Capital.pptx
PPTX
vortex flow measurement in instrumentation
DOCX
Copy-OT LIST 12.8.25.docxjdjfufufufufuuffuf
PPTX
AIR BAG SYStYEM mechanical enginweering.pptx
PDF
Melt Flow Index Tester from Perfect Group India
PPTX
Computer Hardware - Technology and Livelihood Education
PPTX
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
PDF
PakistanCoinageAct-906.pdfdbnsshsjjsbsbb
PPTX
ppt to the world finance to the world in growing
PPTX
Chapter no 8 output devices dpart 2.pptx
PPT
COA______________₹₹_₹₹33₹₹₹33₹₹₹3UNIT1V8.ppt
PPTX
SAI-CAO-24CSEN2021-PPT-UNIT-------1.pptx
dDifference Beetween Saving slides And Investment Slides.pptx
Topic-1-Main-Features-of-Data-Processing.pdf
internal and external hardware in Orthopedic Center
20A LG INR18650HJ2 3.6V 2900mAh Battery cells for Power Tools Vacuum Cleaner
AI_ML_Internship_WReport_Template_v2.pptx
2- Physical Layer (06).pdfgshshshbsbshshshhs
Pin configuration and project related to
Unit-1.pptxgeyeuueueu7r7r7r77r7r7r7uriruru
Growth Capital Investment - Espresso Capital.pptx
vortex flow measurement in instrumentation
Copy-OT LIST 12.8.25.docxjdjfufufufufuuffuf
AIR BAG SYStYEM mechanical enginweering.pptx
Melt Flow Index Tester from Perfect Group India
Computer Hardware - Technology and Livelihood Education
RTS MASTER DECK_Household Convergence Scorecards. Use this file copy.pptx
PakistanCoinageAct-906.pdfdbnsshsjjsbsbb
ppt to the world finance to the world in growing
Chapter no 8 output devices dpart 2.pptx
COA______________₹₹_₹₹33₹₹₹33₹₹₹3UNIT1V8.ppt
SAI-CAO-24CSEN2021-PPT-UNIT-------1.pptx
Ad

CS124-L9-JDBC.ppt Add more information to your upload

  • 2. JDBC  Java Database Connectivity  Database Access Interface  provides access to a relational database (by allowing SQL statements to be sent and executed through a Java program)  JDBC package: set of Java classes that facilitate this access (java.sql.*)  Comes with JDK (since 1.1)
  • 3. JDBC Driver Need a driver, specific to the DB product, to mediate between JDBC and the database  the driver is a Java class that needs to be loaded first Relational DBMS Java Program - load driver - establish connection - send SQL statements
  • 4. JDBC-ODBC Bridge  Driver that interfaces with ODBC (Object Database Connectivity--also an access interface)  Easiest way to access databases created by Microsoft products  register database as an ODBC data source  use JDBC-ODBC bridge as the JDBC driver (included in JDK distribution)
  • 5. Key Classes in JDBC  Connection  need to create an instance of this class when establishing a connection to the database  Statement  for issuing SQL statements  ResultSet (interface)  a ResultSet object represents the table returned by an SQL select statement
  • 6. Establishing a Connection Use the getConnection() method  under the DriverManager class  String argument: "jdbc:driver:name”  returns a Connection object Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); // above line loads the jdbc-odbc driver String dbname = “jdbc:odbc:MyDB”; Connection c = DriverManager.getConnection(dbname);
  • 7. Creating a Statement Object Execute the createStatement() method on the Connection object  returns a Statement object  afterwards, run methods on the Statement object to execute an SQL statement Statement s = c.createStatement();
  • 8. Methods of the Statement Class  Methods of the Statement class require a string parameter containing the SQL statement  executeQuery()  requires a String argument (a SELECT statement)  returns a ResultSet object representing the table returned  executeUpdate()  requires a String argument (an INSERT, UPDATE, or DELETE statement)  returns an int (row count, in most cases)
  • 9. The ResultSet Interface  A ResultSet object represents the table returned by the select statement sent  Navigation/retrieval methods  next(): moves to the next row (first row if called for the first time), returns false if no rows remain  getXXX() methods return the value of a field for the current row
  • 10. ResulSet example ResultSet rs; rs = s.executeQuery(“SELECT * FROM [ORDER]”); rs.next(); // gets the first row (use in a loop for multiple rows) // suppose the ORDER table has an integer field // called quantity int myvar = rs.getInt(“quantity”); // if you knew that quantity is the 2nd field in the table myvar = rs.getInt(2); Need braces because ORDER is a reserved word in SQL
  • 11. Exercise  Create a Microsoft Access table  insert sample rows  Add an ODBC data source  use the Microsoft Access driver  associate with the created database  Create a Java program  use JDBC-ODBC bridge  create a loop that lists all rows of the table
  • 12. executeQuery( ) example … Statement s = con.createStatement(); ResultSet rs = s.executeQuery( “SELECT * FROM STUDENT WHERE QPI > 3.0” ); while ( rs.next() ) { String name = rs.getString(“LastName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); }
  • 13. executeUpdate( ) example … Statement s = con.createStatement(); int result; result = s.executeUpdate( “DELETE FROM EMPLOYEE WHERE DeptCode=‘CS’” ); System.out.println( result + “ rows deleted.” );
  • 14. The PreparedStatement class  PreparedStatement: a Statement that specifies parameters through Java code  The SQL statements take different forms when you specify different parameter values  Useful when query is performed repeatedly  Formatting of literal values is easier
  • 15. Version 1 (Statement) // suppose lastName is a String variable Statement s = con.createStatement(); ResultSet rs = s.executeQuery( “SELECT * FROM STUDENT WHERE LastName = ‘” + lastName +”’” ); while ( rs.next() ) { String name = rs.getString(“LastName”) + rs.getString(“FirstName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); } Query string is built manually
  • 16. Version 2 (PreparedStatement) // suppose lastName is a String variable PreparedStatement s = con.prepareStatement( “SELECT * FROM STUDENT WHERE LastName = ?” ); s.setString( 1, lastName ); ResultSet rs = s.executeQuery(); while ( rs.next() ) { String name = rs.getString(“LastName”) + rs.getString(“FirstName”); int y = rs.getInt(“Year”); double qpi = rs.getDouble(“QPI”); System.out.println( name + “ ” + y + “ ” + qpi); } the appropriate literal is “inserted” in the query
  • 17. Summary  JDBC allows you to write Java programs that manipulate a database  A driver (often a separate product) is required that facilitates access  Key classes: Connection, Statement, PreparedStatement, and ResultSet  Other features: metadata and stored-proc invocation