JDBC (Java Database Connectivity) : Minshuo LI
JDBC (Java Database Connectivity) : Minshuo LI
JDBC (Java Database Connectivity) : Minshuo LI
Connectivity)
Minshuo LI
Why JDBC?
API for
Oracle
Oracle
API for
C SqlServer
SqlServer
C++
Java API for
Mysql Mysql
API for
DB2 DB2
Why JDBC?
API for
Oracle Oracle
ODBC
API for
C SqlServer SqlServer
C++
Java
API for
JDBC Mysql Mysql
API for
DB2 DB2
Tow points of JDBC
Different
Uniform database
interface interface
JDBC Oracle
JDBC SqlServer
Java
JDBC Mysql
JDBC DB2
Connecting to the Database
Core interface and class of JDBC
java.sql package
Javax.sql package
The basic classes and interfaces of the JDBC
API
javax.sql package
Important interface :
javax.sql.DataSource
javax.sql.RowSet
JDBC programming steps
1.Load the Driver
Class.forName()|Class.forName().new Instance()|new DriverName()
instance driver and automatically register to DriverManager
call DriverManager.registerDriver()
2. Connect to the Database
DriverManager.getConnection(url,username,password)
3. Execute the SQL
Connection.createStatement()
Statement.executeQuery()
Statement.executeUpdate()
4. Retrieve the result date
while(rs.next())
5. Show the result data
change database type to Java type (getxxx) 6. Close
Close the resultset | close the Statement | close the Connection
JDBC API core structure
MySQL JDBC
driver
Download
1.Mysql
http://dev.mysql.com/downloads/mysql/
mysql-installer-community-5.7.12.0.msi
mysql-installer-web-community-5.7.12.0.msi
2.Mysql driver(Standardized database driver for
Java platforms and development)
Site:http://dev.mysql.com/downloads/connector/
j/
mysql-connector-java-5.1.39.zip
MySQL database installation
MySQL database
installation(1)
click
13
MySQL database
installation(2)
click
choose
14
MySQL database
installation(3)
choose
click
15
MySQL database
installation(4)
choose
click
16
MySQL database
installation(5)
click
17
MySQL database
installation(6)
click
18
MySQL database
installation(7)
click
19
MySQL database
installation(8)
click
20
MySQL database installation(9)
choose
MySQL database installation(10)
“1234”
MySQL database
installation(11)
click
23
MySQL database installation(12)
Create database
MySQL driver installation
Copy mysql-connector-java-5.1.18-bin.jar to:
1.server‟s lib directory.
2.project‟s lib directory.
key work step(1)
1.Load and register database driver.
Class.forName(DRIVER)
PreparedStatement ps = conn.prepareStatement(sql);
CallableStatement cs=conn.prepareCall(sql)
while (rs.next()==true){
userName = rs.getString( "user_name" ); String
userPassword = rs.getString( 2 );
System.out.println( userName + " : " +
userPassword ); }
key work step(6)
Close result set
Close statement
Close connection
Example:
rs.close();
stmt.close();
conn.close();
task
Realize CRUD:insert,select,update,delete.