1、數據持久化
l 持久化(persistence):把數據保存到可掉電式存儲設備中以供之后使用,也稱為“固化”。在大多數情況下,服務器或客戶端應用中數據的持久化是通過關系型數據庫來實現
l 存儲設備:磁盤、硬盤, U盤,光盤等
l 存儲形式:數據庫、xml文件、txt文件等
2、知識點2: Java 中的數據存儲技術
l 在Java中,數據庫存取技術可分為如下幾類:
l JDBC直接訪問數據庫
l 第三方O/R工具,如Hibernate, ibatis 等
l JDBC是java訪問數據庫的基石,其他技術都是對jdbc的封裝
3、什么是JDBC
l JDBC(Java Database Connectivity)是一個獨立于特定數據庫管理系統、通用的操作數據庫的Interface(一組API),定義了用來訪問數據庫的標準Java類庫,而接口的實現有各個數據庫廠商來完成
l JDBC驅動程序(jar包):數據庫廠商對JDBC接口的一組實現類(jar包)
4、什么是ODBC
l ODBC(Open Database Connectivity,開放數據庫連接)是微軟公司開放服務結構中有關數據庫的一個組成部分,它建立了一組規范,并提供了一組對數據庫訪問的標準API
5、JDBC的分類
目前有四種可供使用的JDBC驅動程序,不同類型的的驅動程序有著不一樣的使用方法,所以當我們在連接數據庫之前,必須先依照我們的需求選擇一個適當的驅動程序,這四種不同類型的驅動程序分別是:
1 JDBC-ODBC橋 :橋接器型的驅動程序,
2 部分本地API部分Java的驅動程序,也是橋接器型驅動程序之一
3 JDBC網絡純Java驅動程序
4 本地協議的純 Java 驅動程序:這類型的驅動程序是最成熟的JDBC驅動程序,不但無需在使用者計算機上安裝任何額外的驅動程序,也不需在服務器端安裝任何的中介程序(middleware),所有存取數據庫的操作,都直接由驅動程序來完成。
6、JDBC API接口介紹

7、java初體驗
// 1.數據庫服務開啟。 2.需要將數據庫驅動導入.
//JDBC體驗
public class Test1 { public static void main(String[] args) throws Exception { //1.將驅動進行注冊(可以進行與數據庫的鏈接) DriverManager.registerDriver(new Driver()); //2.得到一個與數據庫連接的對象 Connection con=DriverManager.getConnection("jdbc:MySQL://127.0.0.1:3306/mydb","root","root"); //3.得到一個執行sql對象 Statement stm=con.createStatement(); //4.執行sql語句. 以查詢為例,執行后會返回一個ResultSet結果集。(其實就是包含查詢到的信息) ResultSet rs=stm.executeQuery("select * from emp"); //查表名 //5.將結果集中的內容迭代出來. while(rs.next()){ Object obj=rs.getObject("ename"); //查列名 System.out.PRintln(obj); } //6.需要將資源釋放 rs.close(); stm.close(); con.close(); }} 8、對以上代碼進行異常處理:
//JDBC異常處理public class Test3 { public static void main(String[] args) { Connection con = null; Statement stm = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/mydb", "root", "root"); stm = con.createStatement(); rs = stm.executeQuery("select * from emp"); while (rs.next()) { String name = rs.getString("ename"); int no = rs.getInt("empno"); Date date = rs.getDate("hiredate"); System.out.println(name + " " + no + " " + date); } } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("加載驅動失敗"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("SQL操作失敗"); } finally { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉rs失敗的"); } try { if (stm != null) stm.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉stm失敗的"); } try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉con失敗的"); } } }}9、對以上代碼進行優化(1)
public class Test4 { @Test public void jdbcTest() { //定義在外面,同時將處理異常方法定義在外面 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://127.0.0.1:3306/mydb"; String username = "root"; String passWord = "root"; Connection con = null; Statement stm = null; ResultSet rs = null; try { Class.forName(driver); con = DriverManager.getConnection(url, username, password); stm = con.createStatement(); rs = stm.executeQuery("select * from emp"); while (rs.next()) { String name = rs.getString("ename"); int no = rs.getInt("empno"); Date date = rs.getDate("hiredate"); System.out.println(name + " " + no + " " + date); } } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("加載驅動失敗"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("SQL操作失敗"); } finally { closeResultSet(rs); closeStatment(stm); closeConnection(con); } } // 關閉Connection // 關閉的Statement // 關閉ResultSet方法 public void closeConnection(Connection con) { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉con失敗的"); } } public void closeStatment(Statement stm) { try { if (stm != null) stm.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉stm失敗的"); } } public void closeResultSet(ResultSet rs) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉rs失敗的"); } }}10 對以上代碼再次優化
10.1 定義一個mysqldb.properties配置文件
定義好配置變量
driver=com.mysql.jdbc.Driverurl=jdbc/:mysql/://127.0.0.1/:3306/testguestusername=rootpassword=1234abcd
10.2 新建jdbcUtil工具類 JdbcUtil.cs
獲取配置文件中的值
private static String driver; private static String url; private static String username; private static String password; // 對driver,url,username, password 進行賦值 static { // 通過類加載器獲得資源,并以流的方式進行操作 InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream( "包名/ db.properties"); Properties prop = new Properties(); try { prop.load(is); driver = prop.getProperty("driver"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("讀取數據庫配置文件失敗"); } }10.2.1 加載驅動
static { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("加載驅動失敗"); } }10.2.2獲得連接的方法
public static Connection getConnection() { Connection con = null; try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("連接數據庫操作失敗"); } finally { } return con; }10.2.3 關閉的方法
// 關閉Connection // 關閉的Statement // 關閉ResultSet方法 public static void close(Connection con) { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉con失敗的"); } } public static void close(Statement stm) { try { if (stm != null) stm.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉stm失敗的"); } } public static void close(ResultSet rs) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("關閉rs失敗的"); } }10.3 新建Test.CS類
public class Test { @Test public void select() { Connection con = null; Statement st = null; ResultSet rs = null; try { con = JdbcUtil.getConnection(); st = con.createStatement(); rs = st.executeQuery("select * from tg_user"); while (rs.next()) { System.out.println(rs.getInt(1)); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.close(rs); JdbcUtil.close(st); JdbcUtil.close(con); } }}新聞熱點
疑難解答