CIS 830 (Advanced Topics in AI) Lecture 2 of 45
Download
Report
Transcript CIS 830 (Advanced Topics in AI) Lecture 2 of 45
JDBC
Lecture No. 12
Thursday, September 28, 2000
“JDBC”
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
CONTENTS
• Concept of connectivity
• ODBC
• DAO, RDO & ADO
• JDBC
• JDBC Programming
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
JDBC
The import statement
import java.sql.*
The Objects
static Connection con;
static Statement stmt;
private ResultSet rs;
“Connection is the channel and statement is the transaction”
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
try
{
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(url, user, passwd);
stmt = con.createStatement();
}
catch(Exception e)
{System.err.println("SQLException : " + e.getMessage());
System.err.println("SQLException local : " +
e.getLocalizedMessage());
e.printStackTrace(System.out);
System.exit(0);
}
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
Running a query
String url = "jdbc:oracle:thin:@zaurak:1522:PROD"
String query2 = "select distinct year from time_master";
stmt = con.createStatement();
rs = stmt.executeQuery(query1);
rs = stmt.updateQuery(query1);
“ 1. Register driver
2. Get connection
3. Create statement
4. Execute”
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
Result set fundamentals
•rs.first
•rs.last
•rs.previous
•rs.next
•rs.isFirst
Warning: “Be aware of the jdk version that supports the
features”
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
Path setting
• Include \java\bin and \java\lib to PATH
Eg:.\;x:\java\bin; x:\java\lib
• Include the location of classes111.zip to CLASSPATH
Eg:.\;x:\java\lib\classes.zip;x:\oracle-jdbc\lib\classes111.zip
“This is for Windows … how is unix different?”
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
Exercise
I will put a piece of jdbc code sample on my web
page.
Compile and run the piece of code
Submit a half to one page mail on how you went
about doing this. (Use numbered steps)
Mail to: [email protected]
Sub: JDBC Exercise
(Add your full name in the mail body)
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences
JDBC
Web sites
• http://java.sun.com/products/jdbc/
• http://www.microsoft.com/data/odbc/default.htm
CIS 560: Database System Concepts
Kansas State University
Department of Computing and Information Sciences