Java高手詳解使用JDBC的步驟
JDBC(Java Data Base Connectivity,java數據庫連接)是一種用于執行SQL語句的Java API,可以為多種關系數據庫提供統一訪問,它由一組用Java語言編寫的類和接口組成。JDBC為工具/數據庫開發人員提供了一個標準的API,據此可以構建更高級的工具和接口,使數據庫開發人員能夠用純 Java API 編寫數據庫應用程序,
使用JDBC的步驟分為6步
使用JDBC的步驟1. load the driver
(1)Class.forName()|Class.forName().newlnstance()|new DriverName()
(2)實例化時自動向DriverManager注冊,不需要顯示調用DriverManager.registerDriver
使用JDBC的步驟2. Connect to the DataBase
DriverManager.getConnection()
使用JDBC的步驟3.Excute the SQL
(1)connection.CreateStatement()
(2)Statement.excuteQuery()
(3)Statement.executeUpdate()
使用JDBC的步驟4. Retrieve the result data
循環取得結果 while(rs.next())
使用JDBC的步驟5. show the result data
將數據庫中的各種類型轉換為java中的類型(getXXX)方法
使用JDBC的步驟6. close
close the resultset / close the statement /close the connection
實際例子 Java代碼
- package DB;
- import java.sql.*;
- class Jdbc
- {
- public static void main(String[] args)throws Exception
- {
- //只有下面2句話就可以連接到數據庫中
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234"); //Class.forName("com.mysql.jdbc.Driver");
- //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
- //Class.forName("oracal.jdbc.driver.OracalDriver");
- //new oracal.jdbc.driver.OracalDriver();
- //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
- //jdbc.driverClassName=com.mysql.jdbc.Driver;
- //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
- }
- }
- package DB;
- import java.sql.*;
- class Jdbc
- {
- public static void main(String[] args)throws Exception
- {
- //只有下面2句話就可以連接到數據庫中
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");
- //Class.forName("com.mysql.jdbc.Driver");
- //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
- //Class.forName("oracal.jdbc.driver.OracalDriver");
- //new oracal.jdbc.driver.OracalDriver();
- //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
- //jdbc.driverClassName=com.mysql.jdbc.Driver;
- //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
- }
- }
還有另外的一個用try catch 的方法
下面就實際去操作一下
首先把mysql驅動mysql-connector-java-3.1.10-bin.jar 或者其它版本的驅動copy到WebRoot 下面的WEB-INF下面的lib里面
Java代碼
- package db;
- //一定要注意類名字要相同!!
- import java.sql.*;
- class Jdbc
- {
- public static void main(String[] args)throws Exception
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");
- System.out.print("ok");//如果連接成功顯示ok
- }
- }
- package db;
- //一定要注意類名字要相同!!
- import java.sql.*;
- class Jdbc
- {
- public static void main(String[] args)throws Exception
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");
- System.out.print("ok");//如果連接成功顯示ok
- }
- }
- 然后接著看下面的升級版
- Java代碼
- package db;
- import java.sql.*;
- import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
- class Jdbc2 {
- public static void main(String[] args) throws Exception {
- //1.先new 一個driver 然后向managerDriver注冊
- Class.forName("com.mysql.jdbc.Driver");
- //2.通過DriverManager.getConnection傳遞個方法,嚴格的說是jdbc的url
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/test", "root", "1234");
- //3.創建個statement對象,執行sql語句
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select * from test.admin");
- //4.取得結果集 5.對其進行便利
- while (rs.next()) {
- System.out.println(rs.getString("username"));
- System.out.println(rs.getInt("id"));
- }
- //6.關閉(要遵循后執行的先閉,先執行的后閉的原則)
- rs.close();
- stmt.close();
- conn.close();
- }
- }
- /**
- * 此例子需要注意的是:
- * 1.驅動是否在lib文件夾下面。
- * 2.數據庫里面的庫名以及表是否存在
- * 3."jdbc:mysql://localhost:3306/test", "root", "1234");
- * 分別對應的是地址、端口、庫名、數據庫的管理員名字、管理員密碼。
- * 4."select * from test.admin" sql語句建議一定寫的時候用 庫名.表名。
- */
- /*
以上是jdbc 一個簡單的例子,了解連接jdbc 的步驟。
這段代碼的統一出現的問題是在執行System.out.println(rs.getString("name"));的時候會出現exception,這樣的話后面的close就不再被執行,占用緩存,最后導致服務器死機 (河北電信視頻點擊系統)改進的代碼請看TESTHdbc3.java
- */
- //Class.forName("com.mysql.jdbc.Driver");
- //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
- //Class.forName("oracal.jdbc.driver.OracalDriver");
- //new oracal.jdbc.driver.OracalDriver();
- //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
- //jdbc.driverClassName=com.mysql.jdbc.Driver;
- //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
- /*
以上是jdbc 一個簡單的例子,了解連接jdbc 的步驟。
這段代碼的統一出現的問題是在執行System.out.println(rs.getString("name"));的時候會出現exception,這樣的話后面的close就不再被執行,占用緩存,最后導致服務器死機(河北電信視頻點擊系統)改進的代碼請看TESTHdbc3.java
- //Class.forName("com.mysql.jdbc.Driver");
- //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");
- //Class.forName("oracal.jdbc.driver.OracalDriver");
- //new oracal.jdbc.driver.OracalDriver();
- //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"
- //jdbc.driverClassName=com.mysql.jdbc.Driver;
- //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;
最后讓我們看個使用JDBC的步驟成熟版
Java代碼
- package db;
- import java.sql.*;
- class Jdbc3 {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/test", "root", "1234");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select * from test.admin");
- while (rs.next()) {
- System.out.println(rs.getString("username"));
- System.out.println(rs.getInt("id"));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch(SQLException e){
- e.printStackTrace();}
- }
- finally{
- rs.close();
- stmt.close();
- conn.close();
- }
- }
【編輯推薦】
- 使用JDBC的五個精華功能
- Tomcat5+MySQL JDBC連接池配置
- 在Weblogic中實現JDBC的功能
- 詳解JDBC與Hibernate區別
- JDBC連接MySQL數據庫關鍵四步
- 淺談JDBC的概念理解與學習
【責任編輯:彭凡 TEL:(010)68476606】