JDBC course objectives:
1. Design a multi-layer database application architecture
2. Create a multi-level database application using Java programming language content
And jdbc1.0 APIs
3. Relational Database Design for objects
4. Introduce jdbc2.0 API and new JDBC Functions
Course content:
1. Overview of JDBC
2. Use of JDBC
3. OOAD and database design
4. JDBC advanced Section
1. Overview of JDBC
1) ODBC (Open Database interconnection)
Microsoft and C
2) JDBC (Java database connection)
Sun, written in pure Java language
JDBC connection method:
1. JDBC-ODBC bridge driver and ODBC driver
Advantages:
Only the corresponding ODBC driver needs to be installed on the client.
Disadvantages:
1. Low execution efficiency, not suitable for connecting large data volumes;
2. the ODBC driver must be installed on the client.
2. Java drivers for local APIs
Implementation: 1. Install the Java driver
2. Install the database Client
3. JDBC-net pure Java driver OCI
Advantage: it can be used for network connections;
Disadvantage: More Middleware
4. Local protocol pure Java driver thin
Advantages: fast speed and network connection
Disadvantage: Load different database drivers based on different databases.
1995 sun javaone Conference
1. the pointer is developed in C language.
2. ODBC configurations are complex and not suitable for Java features;
3, JDBC-ODBC bridge drive most of only suitable for testing, and cannot carry out product operations.
2. Use of JDBC
Using java. SQL: it is mainly used to connect to the database;
Javax. SQL: advanced database operations for Java applications.
JDBC connection steps:
1. Registration driver
Create a driver-type object;
Register this object in the corresponding program.
Reflection (get a class ):
1. Class C = Class Name. Class
2. Class Name. getclass ();
3. Class. forname (full name of the class );
Implementation Method:
1), Class. forname ("class full name ");
2) driver = new Oracle. JDBC. Driver. oracledriver ();
2. Establish a connection
Connection interface.
Connection conn = drivermanager. getconnection (URL );
URL of the Oracle database:
JDBC: oracle: thin: @ 127.0.0.1: 1521: XE
JDBC: connection protocol-jdbc connection;
ORACLE: connected database (sub-Protocol );
Thin: connection mode (OCI)
@ 172.0.0.1 = localhost: IP Address
1521: Port Number
Xe: Database Name (Select name from V $ database)
Connect MySQL to the database
Class Full name: COM. MySQL. JDBC. Driver
URL: JDBC: mysql: // localhost: 3306/Database Name
3. Create a statement
Statement creation method: conn. createstatement ();
Purpose: Execute static SQL statements;
Execute (string SQL );
Different results are returned based on different SQL statements;
Executequery (string SQL );
// Select, returns a result set resultset
Executeupdate (string SQL );
// Update (insert)
// Return int type data
4. Execute SQL statements
5. processing result set
6. Close the connection
The class or interface used:
Driver: interfaces that must be implemented by all drivers, similar
The driver must be installed in the hardware.
Drivermanager: Mainly used to manage database connection drivers
Program.
Connection: used to establish a connection between a Java application and a database.
Example: -----------------"
Testjdbc. Java
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Import java. SQL. statement;
/*
* This is an example of connecting Oracle with Java.
*/
Public class testjdbc {
Public static void main (string [] ARGs ){
Connection conn = NULL;
Statement ST = NULL;
Try {
// Register the driver
Class. forname ("oracle. JDBC. Driver. oracledriver ");
// Establish a connection
Try {
// Xe is the database name
String url = "JDBC: oracle: thin: @ FIG: 1521: XE ";
// System is the username 123456 is the password
Conn = drivermanager. getconnection (URL, "system", "123456 ");
// Create a statement
St = conn. createstatement ();
// Define an SQL statement
String SQL = "insert into test values (1, 'name ')";
// Execute the SQL statement to obtain the int type
Int num cmdst.exe cuteupdate (SQL );
System. Out. println ("inserted successfully" + num + "Data! ");
} Catch (sqlexception e ){
E. printstacktrace ();
}
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Finally {// exception handling ..
Try {
If (Conn! = NULL) {// close the connection
Conn. Close ();
}
If (st! = NULL ){
St. Close ();
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
}
}