CSI 2132 Lab5 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CSI 2132 Lab5

Description:

JDBC Driver (download and use) Java programming with JDBC. Dynamic ... Java Database ... It is a jar file. JDBC Enabled Project in eclipse. Create a new Java ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 15
Provided by: Bru91
Category:
Tags: csi | ajar | lab5

less

Transcript and Presenter's Notes

Title: CSI 2132 Lab5


1
CSI 2132 Lab5
  • JDBC Installation and Use
  • Saeid Nourian

2
Outline
  • What is JDBC?
  • JDBC Driver (download and use)
  • Java programming with JDBC
  • Dynamic SQL queries with Java

3
What is JDBC?
  • Java Database Connectivity (JDBC)
  • It is an API by Sun Microsystems to allow Java
    programmers to access SQL databases
  • Available since JDK 1.1
  • JDBC is an API not a library. It needs to be
    implemented (as drivers) for a particular DB.
    i.e. PostgreSQL and MySQL have different JDBC
    drivers
  • In this course we use PostgreSQL so we download
    PostgreSQL JDBC driver

4
Download JDBC Driver
  • Download JDBC from postgreSQL.org
  • JDBC4 PostgreSQL driver version 8.3-603
  • It is a jar file

5
JDBC Enabled Project in eclipse
  • Create a new Java Project in eclipse
  • From the Project Properties, click on
  • Java Build Path gt Libraries gt Add External JARs
  • Find the .jar file for JDBC driver and click Open

6
JDBC Coding in Java
  • Import the JDBC driver
  • Load the driver
  • Connect to a Database
  • Issue a Query and process the result

7
JDBC Coding in Java
  • Import the JDBC driver
  • import java.sql.
  • It is NOT appropriate to import org.postgresql
    directly
  • Load the driver
  • Connect to a Database
  • Issue a Query and process the result

8
JDBC Coding in Java
  • Import the JDBC driver
  • Load the driver
  • Class.forName("org.postgresql.Driver")
  • or
  • java -Djdbc.driversorg.postgresql.Driver
    example.MyJavaProgram
  • Connect to a Database
  • Issue a Query and process the result

9
JDBC Coding in Java
  • Import the JDBC driver
  • Load the driver
  • Connect to a Database
  • Connection db DriverManager.getConnection(url,
    username, password)
  • URL is in the form of
  • jdbcpostgresqldatabase
  • jdbcpostgresql//host/database
  • jdbcpostgresql//hostport/database
  • jdbcpostgresql//web0.site.uottawa.ca15432/snour
    ian
  • Issue a Query and process the result

10
JDBC Coding in Java
  • Import the JDBC driver
  • Load the driver
  • Connect to a Database
  • Issue a Query and process the result
  • Statement st db.createStatement()
  • ResultSet rs st.executeQuery("SELECT FROM
    Artists)
  • while (rs.next())
  • System.out.print("Column 1 returned ")
    System.out.println(rs.getString(1))
  • rs.close()
  • st.close()

11
Your Turn
  • Write a Java program that connects to your own
    database and retrieves the name and birthday of
    all artists. Print the result as a 2D table using
    System.out.print

12
Dynamic Queries
  • String field "AName, Style"
  • String cond "AName"
  • String table "Artist"
  • String value "Caravaggio"
  • Statement st db.createStatement()
  • ResultSet rs st.executeQuery("SELECT " field
    " FROM " table " WHERE " cond " '"
    value "'")

13
Your Turn
  • Write Java code that returns those fields of
    table Artist that are in an array named fields
  • String fields AName, Style, .
  • Try changing the fields array and recompile and
    run your code. It should work for all valid
    fields.

14
REFERENCE
  • http//jdbc.postgresql.org/documentation/83/index.
    html
Write a Comment
User Comments (0)
About PowerShow.com