從這篇開始學習Spring的JDBC,爲了瞭解Spring對於JDBC的做用,先經過JDBC傳統的流程,實現一個數據庫的插入和讀取。java
從這篇你能夠了解到:mysql
1 傳統的JDBC插入和讀取的過程。spring
2 如何經過JDBC鏈接Mysqlsql
首先看一下下面這張圖:數據庫
應用程序須要經過mysql的驅動程序,才能與數據鏈接。編程
驅動程序下載地址:mysql-connector-java-5.1.13-bin.jar學習
在設計程序的時候,應該採用接口編程的方式,這樣可以減少數據操做與應用業務邏輯代碼之間的耦合。測試
使用傳統的JDBC須要通過以下的幾個步驟:url
1 加載驅動程序spa
Class.forName("com.mysql.jdbc.Driver");
2 建立鏈接
Connection conn = (Connection) DriverManager.getConnection(url, userName,passwrod);
3 建立執行計劃
PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement("select * from persons");
4 執行查詢獲取結果
ResultSet rs = pstmt.executeQuery(); while (rs.next()) { System.out.println("id : " + rs.getString(1) + " name : "+ rs.getString(2) + " age : " + rs.getInt(3)); }
5 關閉查詢結果
if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } }
6 關閉執行計劃
if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } }
7 關閉鏈接
if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
首先是接口部分:
package com.spring.chap5.dao; public interface OldJdbc { /** * 插入數據 */ public void insertPerson(String id,String name,int age); /** * 查詢全部結果 */ public void findAllPerson(); }
而後是實現部分
package com.spring.chap5.dao; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; public class OldJdbcImpl implements OldJdbc{ String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String passwrod = "123qwe"; String url = "jdbc:mysql://localhost:3306/test"; public void insertPerson(String id,String name,int age) { try { Class.forName(driver); Connection conn = (Connection) DriverManager.getConnection(url, userName,passwrod); PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement("insert into persons (id,name,age) values (?,?,?)"); pstmt.setString(1, id); pstmt.setString(2, name); pstmt.setInt(3, age); pstmt.executeUpdate(); if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } public void findAllPerson() { try { Class.forName(driver); Connection conn = (Connection) DriverManager.getConnection(url, userName,passwrod); PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement("select * from persons"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { System.out.println("id : " + rs.getString(1) + " name : "+ rs.getString(2) + " age : " + rs.getInt(3)); } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }
能夠看到,光是兩個簡單的插入和查詢,就有如此多的代碼,並且大部分都是相同的過程。所以spring的模板,就派上用場了。
測試類以下:
public class test { public static void main(String[] args) { OldJdbc oldjdbc = new OldJdbcImpl(); oldjdbc.insertPerson("001", "xingoo1", 20); oldjdbc.insertPerson("002", "xingoo2", 20); oldjdbc.findAllPerson(); } }
/* SQLyog v4.05 Host - 4.1.11-nt : Database - test ********************************************************************* Server version : 4.1.11-nt */ create database if not exists `test`; USE `test`; /*Table structure for table `test`.`persons` */ drop table if exists `test`.`persons`; CREATE TABLE `persons` ( `id` varchar(20) NOT NULL default '', `name` varchar(20) default NULL, `age` int(10) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*Data for the table `test`.`persons` */ insert into `test`.`persons` values ('001','xingoo1',20),('002','xingoo2',20);