JDBC Basics - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

JDBC Basics

Description:

Oracle Driver. Put this in your /lib ... Class.forName ('sun.jdbc.odbc.JdbcOdbcDriver'); Oracle ... { String driver = 'oracle.jdbc.driver.OracleDriver' ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 8
Provided by: jeffch8
Category:
Tags: jdbc | basics

less

Transcript and Presenter's Notes

Title: JDBC Basics


1
JDBCBasics
  • Not "Java DataBase Connectivity"

2
Overview
  • Establishing a Connection
  • Querying the Database
  • Updating Tables

3
Loading Drivers
  • Make sure you've loaded the drivers that came
    with the database!
  • Oracle Driver
  • Put this in your /lib directory
  • Load driver in Java using the Class class
  • Involves one line of code
  • Examples
  • Microsoft Access
  • Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver")
  • Oracle
  • Class.forName ("oracle.jdbc.driver.OracleDriver")

4
Getting a Connection
  • Use the DriverManager class to get the
    connection
  • Connection connect
  • String username "Bob"
  • String passwd "6543216!"
  • // Access
  • String url "jdbcodbcmyDataBase"
  • // Oracle
  • String url "jdbcoraclethin_at_gandolf.ccsunet.cla
    yton.edu1521testDB"
  • connect DriverManager.getConnection (url,
    username, passwd)

5
Creating a Statement
  • A statement object is what sends your SQL
    statements to the DBMS
  • Use the connection to create the statement
  • Statement stmt connect.createStatement()
  • stmt.executeUpdate ("CREATE TABLE STUDENTS (NAME
    VARCHAR(32), ID INTEGER, GRADE INTEGER)" )

6
Querying the DBSelect Statements
  • A query returns a ResultSet
  • Need to iterate through the results
  • ResultSet rs stmt.executeQuery ( "SELECT NAME,
    ID FROM STUDENTS")
  • while (rs.next( ))
  • String s rs.getString ("Name")
  • int i rs.getInt ("ID")

7
  • import java.sql.
  • class JDBKiller
  • public static void main (String args)
  • String driver "oracle.jdbc.driver.OracleDriver
    "
  • String url "jdbcoraclethin_at_gandolf.ccsunet.
    clayton.edu1521test"
  • String user "Bob"
  • String passwd "theBuilder"
  • Connection con null
  • try
  • Class.forName(driver)
  • System.out.println("Driver loaded")
  • con DriverManager.getConnection(url, user,
    passwd)
  • con.close()
  • catch (Exception e)
  • System.out.println ("Failed...")
  • System.out.println (e.getMessage())
Write a Comment
User Comments (0)
About PowerShow.com