際際滷

際際滷Share a Scribd company logo
舒于舒
丐亠仆仂仍仂亞亳
丕仍舒舒仆弍舒舒舒 亟 弌亞仍
仍亞仆: 舒亞 .舒仆亰仂亳亞
哦哦 弌
丼
Java Database Management System
Java Database Connectivity
About JDBC
JDBC 仆 亢舒于舒 仍 弍仂仍仂仆 勵勵仆亳亶亞 亟仄亢亳亢
从仂仄仗ム亠 仂亶仍亞仂亞亟仂仂仂 弍仂仍亞仂亢 舒亟舒
舒于舒 亳舒仍 仄舒亳仆 仂亞 舒亳亞仍舒仆
唏亞唏亞亟唏仍亟 舒仆亟亟舒亞 舒仆亟舒 舒亞舒 亰舒仄 ミ.
Using JVM with JDBC
 舒于舒 从仂亟 -> 仂仄仗ム亠亳亶仆 从仂亟
 勳亶仍亟仍亳亶仆 亳亠仄 弍仂仍亞仂仆亟 舒亢亳仍仍舒
舒亟于舒舒亶.
 哦唏 哦弌-亟 亞舒亶仍舒仆 亰仂亳仍舒仆
舒仗仗仍亳从亶仆 弍亳亳 舒舒亟仍舒亞舒亞勵亶.
亳 仆: Oracle, Sybase
JDBC some functions !!!
 Query 舒亢亳仍仍仍舒
 勳 亟勵仆亞 弍仂仍仂于仍舒
 哦亞唏亞亟唏仍 舒亟亞舒仍舒
 勳 亟勵仆亞 舒仍舒
亞 仄 哦弌-舒亶 舒亢亳仍仍舒 弍勵 仍
勵亶仍亟仍勵勵亟亳亶亞 舒亞仍舒仆 从仂亟仍仂仍仆 舒仆
弍舒亶亟舒亞.
How does JDBC work?
 哦弌-舒亶 仂仍弍仂亞亟仂 仂仍弍仂仍仂仂
仂亟仂仂亶仍仆仂.
 Query-亞 哦弌- 亟舒仄亢仍仆舒.
 勳 亟勵仆亞 弍仂仍仂于仍仆舒.
How does JDBC work?
JDBC Driver
 哦弌-亟 舒仆亟舒 弍勵 仍 勵亶仍亟仍亳亶亞 舒亞仍舒仆
从仍舒亟仆 亞仍仍亞舒
 亅亟亞 从仍舒亟仆 仍舒仄亢舒亶亞舒舒
亳亶亢 弍亶 仗仂亞舒仄亟舒舒 哦弌-舒舒 从勵亳亞
亟亟舒仆 唏亞唏亞亟仍唏唏 舒亳亞仍舒 弍仂仍仂仄亢仂亶
弍仂仍亢 弍舒亶亞舒舒 ミ.
Main objects of JDBC Driver
 DataSource
 Connection
 Statement
 PreparedStatement/many times with
different values/
 CallableStatement/stored procedure/
 ResultSet
JDBC Drivers type
 JDBC  ODBC bridge
 Partial Java Drivers
 Pure Java driver for DB middleware
 Pure Java driver for direct-to-DB
亞仆 4-仆 唏唏仍亶 弍舒亶亟舒亞.
JDBC Performance
 DatabaseMetaData 仄亠仂亟亞 舒亳亞仍舒
 唏于唏仆 舒舒亟舒亞亟舒仆 唏亞唏亞亟仍亳亶亞 舒舒
 Select 亳亶 仆从亟
 丱仂仍弍仂仍 弍仂仍仂仆 唏唏仍唏仍勵勵亟亳亶亞
亰仂亳仍舒
Catch Exceptions
 MySQL 仗仂亞舒仄 仆 亟仂仂仂 仂 勵仆亟仆
亞亶:
 弌亠于亠 仗仂亞舒仄: 亅仆 仆 勵仆亟仆 亠于亠 仗仂亞舒仄 ミ.
MySQL 仗仂亞舒仄亞 仍亞舒仆 亟舒舒舒 亞 亠于亠 仆
亳亠仄 仍亟 舒舒舒仍舒亞亟亟舒亞. 丐勵勵仆亳亶亞 control panel -
>Administritive tools->Services 亞 仂亢 舒亢
弍仂仍仆仂. (mysql 亞仆 弍亳仍亞 仆亟 弍舒亶 仂亶. 唏仆 仆
亞 亞 亠于亠亳亶亞 亰仂亞仂仂亢, 舒亢亳仍仍仍亢 弍仂仍仆仂.)
1. 丐舒仆亳仍仍亞舒, 舒仆仆 仗仂亞舒仄
Before using JDBC
 sqljdbc4.jar 舒亶仍亞 JVM-亳亶仆 仂亳亞仂仂仆亟
仂仍舒仆 弍舒亶
 丿舒舒亟仍舒亞舒舒亶 亢亳亢亳亞 .exe-仆勵勵亟亳亶亞
仍亞舒舒仆 弍舒亶
 JDBC 4.0
 Java Runtime Environment (JRE) of version 6.0 or
later
 JRE 1.4 or 5.0 will throw an exception.
Connection
Class.forName("com.microsoft.sqlserver.jdb
c.SQLServerDriver");
String connectionUrl =
"jdbc:sqlserver://localhost;database=Adve
ntureWorks; integratedSecurity=true;
Connection con =
DriverManager.getConnection(connection
Url);
Connection URL
String connectionUrl =
"jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;user=UserNa
me;password=";
String connectionUrl =
"jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;IntegratedSec
urity=true;
Some Java codes
public static void executeStatement(Connection con) {
try {
String SQL = "SELECT TOP 1 * FROM Person.Contact; "
Statement stmt = con.createStatement();
boolean results = stmt.execute(SQL);
do {
if(results) {
ResultSet rs = stmt.getResultSet();
System.out.println(rs.getString("LastName") + " " + rs.getString("FirstName"));
rs.close();
}
} while(results);
stmt.close();
} catch (Exception e) {
e.printStackTrace(); } }
JDBC packages
 sun.jdbc.odbc  JDBC-ODBC bridge
 sun.jdbc.odbc.JdbcOdbcDriver
 java.sql.*;
 com.microsoft.sqlserver.jdbc.*;
Lecture data base programming part2

More Related Content

Lecture data base programming part2

  • 2. 哦哦 弌 丼 Java Database Management System Java Database Connectivity
  • 3. About JDBC JDBC 仆 亢舒于舒 仍 弍仂仍仂仆 勵勵仆亳亶亞 亟仄亢亳亢 从仂仄仗ム亠 仂亶仍亞仂亞亟仂仂仂 弍仂仍亞仂亢 舒亟舒 舒于舒 亳舒仍 仄舒亳仆 仂亞 舒亳亞仍舒仆 唏亞唏亞亟唏仍亟 舒仆亟亟舒亞 舒仆亟舒 舒亞舒 亰舒仄 ミ.
  • 4. Using JVM with JDBC 舒于舒 从仂亟 -> 仂仄仗ム亠亳亶仆 从仂亟 勳亶仍亟仍亳亶仆 亳亠仄 弍仂仍亞仂仆亟 舒亢亳仍仍舒 舒亟于舒舒亶. 哦唏 哦弌-亟 亞舒亶仍舒仆 亰仂亳仍舒仆 舒仗仗仍亳从亶仆 弍亳亳 舒舒亟仍舒亞舒亞勵亶. 亳 仆: Oracle, Sybase
  • 5. JDBC some functions !!! Query 舒亢亳仍仍仍舒 勳 亟勵仆亞 弍仂仍仂于仍舒 哦亞唏亞亟唏仍 舒亟亞舒仍舒 勳 亟勵仆亞 舒仍舒 亞 仄 哦弌-舒亶 舒亢亳仍仍舒 弍勵 仍 勵亶仍亟仍勵勵亟亳亶亞 舒亞仍舒仆 从仂亟仍仂仍仆 舒仆 弍舒亶亟舒亞.
  • 6. How does JDBC work? 哦弌-舒亶 仂仍弍仂亞亟仂 仂仍弍仂仍仂仂 仂亟仂仂亶仍仆仂. Query-亞 哦弌- 亟舒仄亢仍仆舒. 勳 亟勵仆亞 弍仂仍仂于仍仆舒.
  • 8. JDBC Driver 哦弌-亟 舒仆亟舒 弍勵 仍 勵亶仍亟仍亳亶亞 舒亞仍舒仆 从仍舒亟仆 亞仍仍亞舒 亅亟亞 从仍舒亟仆 仍舒仄亢舒亶亞舒舒 亳亶亢 弍亶 仗仂亞舒仄亟舒舒 哦弌-舒舒 从勵亳亞 亟亟舒仆 唏亞唏亞亟仍唏唏 舒亳亞仍舒 弍仂仍仂仄亢仂亶 弍仂仍亢 弍舒亶亞舒舒 ミ.
  • 9. Main objects of JDBC Driver DataSource Connection Statement PreparedStatement/many times with different values/ CallableStatement/stored procedure/ ResultSet
  • 10. JDBC Drivers type JDBC ODBC bridge Partial Java Drivers Pure Java driver for DB middleware Pure Java driver for direct-to-DB 亞仆 4-仆 唏唏仍亶 弍舒亶亟舒亞.
  • 11. JDBC Performance DatabaseMetaData 仄亠仂亟亞 舒亳亞仍舒 唏于唏仆 舒舒亟舒亞亟舒仆 唏亞唏亞亟仍亳亶亞 舒舒 Select 亳亶 仆从亟 丱仂仍弍仂仍 弍仂仍仂仆 唏唏仍唏仍勵勵亟亳亶亞 亰仂亳仍舒
  • 12. Catch Exceptions MySQL 仗仂亞舒仄 仆 亟仂仂仂 仂 勵仆亟仆 亞亶: 弌亠于亠 仗仂亞舒仄: 亅仆 仆 勵仆亟仆 亠于亠 仗仂亞舒仄 ミ. MySQL 仗仂亞舒仄亞 仍亞舒仆 亟舒舒舒 亞 亠于亠 仆 亳亠仄 仍亟 舒舒舒仍舒亞亟亟舒亞. 丐勵勵仆亳亶亞 control panel - >Administritive tools->Services 亞 仂亢 舒亢 弍仂仍仆仂. (mysql 亞仆 弍亳仍亞 仆亟 弍舒亶 仂亶. 唏仆 仆 亞 亞 亠于亠亳亶亞 亰仂亞仂仂亢, 舒亢亳仍仍仍亢 弍仂仍仆仂.) 1. 丐舒仆亳仍仍亞舒, 舒仆仆 仗仂亞舒仄
  • 13. Before using JDBC sqljdbc4.jar 舒亶仍亞 JVM-亳亶仆 仂亳亞仂仂仆亟 仂仍舒仆 弍舒亶 丿舒舒亟仍舒亞舒舒亶 亢亳亢亳亞 .exe-仆勵勵亟亳亶亞 仍亞舒舒仆 弍舒亶 JDBC 4.0 Java Runtime Environment (JRE) of version 6.0 or later JRE 1.4 or 5.0 will throw an exception.
  • 15. Connection URL String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;user=UserNa me;password="; String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=AdventureWorks;IntegratedSec urity=true;
  • 16. Some Java codes public static void executeStatement(Connection con) { try { String SQL = "SELECT TOP 1 * FROM Person.Contact; " Statement stmt = con.createStatement(); boolean results = stmt.execute(SQL); do { if(results) { ResultSet rs = stmt.getResultSet(); System.out.println(rs.getString("LastName") + " " + rs.getString("FirstName")); rs.close(); } } while(results); stmt.close(); } catch (Exception e) { e.printStackTrace(); } }
  • 17. JDBC packages sun.jdbc.odbc JDBC-ODBC bridge sun.jdbc.odbc.JdbcOdbcDriver java.sql.*; com.microsoft.sqlserver.jdbc.*;