只有光頭才能變強。html
文本已收錄至個人GitHub倉庫,歡迎Star:https://github.com/ZhongFuCheng3y/3yjava
.mysql
在講解Web開發模式的時候,曾經寫過XML版的用戶登錄註冊案例!如今在原有的項目上,使用數據庫版來完成用戶的登錄註冊!若是不瞭解的朋友,能夠看看我Web開發模式的博文!git
原本使用的是XML文件做爲小型數據庫,如今使用Mysql數據庫,代碼究竟要改多少呢?咱們拭目以待!github
開發包導入的是這個:c3p0-0.9.2-pre1和mchange-commons-0.2.jar.sql
C3P0不只性能好,並且配置文件可使用XML文檔來配置!數據庫
相似的配置文件能夠在官方文檔上找獲得!編程
咱們來改造一下:設計模式
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/zhongfucheng</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="mysql"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/zhongfucheng</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </named-config> <named-config name="oracle"> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="jdbcUrl">jdbc:oracle:thin:@//localhost:1521/事例名...</property> <property name="user">用戶名</property> <property name="password">密碼</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </named-config> </c3p0-config>
public class DBUtils { private static ComboPooledDataSource comboPooledDataSource = null; static { //它會自動尋找配置文件,節點爲mysql的數據庫【若是沒有指定,就使用默認的!】 comboPooledDataSource = new ComboPooledDataSource("mysql"); } public static DataSource getDataSource() { return comboPooledDataSource ; } public static Connection getConnection() { try { return comboPooledDataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("數據庫初始化失敗了!"); } } }
很是簡單,根據實體表來設計就行了!markdown
CREATE TABLE user ( id VARCHAR(20) PRIMARY KEY, username VARCHAR(20) UNIQUE NOT NULL, password VARCHAR(20) NOT NULL, email VARCHAR(20), birthday DATE );
public class UserImplDataBase implements UserDao { @Override public User find(String username, String password) { return null; } @Override public void register(User user) { } }
下面咱們就直接使用DBUtils框架了
@Override public User find(String username, String password) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "SELECT * FROM user WHERE username=? AND password=?"; try { User user = (User) queryRunner.query(sql, new BeanHandler(User.class), new Object[]{username, password}); return user == null ? null : user; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("登錄失敗了!"); } } @Override public void register(User user) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "INSERT INTO user (id, username, password, email,birthday) VALUES (?,?,?,?,?);"; String id = user.getId(); String username = user.getUsername(); String password = user.getPassword(); String email = user.getEmail(); Date date = user.getBirthday(); try { queryRunner.update(sql, new Object[]{id, username, password, email,date}); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("註冊失敗了"); } } }
咱們的Dao實現已經有了XML版和JDBC版的,BusinessService調用Dao層方法的時候仍是要new出具體的Dao實現,也就是如下的代碼:
UserDao userDao = new UserImplXML(); //或者 UserDao userDao= new UserImplDataBase();
這樣作有點不夠靈活,也就有點不夠專業!下面咱們來講一下爲何須要DaoFactory?
參考博文:http://blog.sina.com.cn/s/blog_4ca34d0501008tpc.html
摘抄重點:
優勢:
將全部的數據訪問都單獨集中到一層中去: 由於全部的數據訪問操做如今都已經被DAO所代理,因此這個單獨的數據訪問層能夠被看做能夠是將數據訪問實現和其他應用程序相互隔離的一層. 這樣的集中,使得應用程序能夠更加容易的來維護和管理.
缺點:
首先,咱們把DaoFactory設計成單例的【工廠有一個就夠了!】
public class DaoFactory { private DaoFactory() { } private static final DaoFactory DAO_FACTORY = new DaoFactory(); //暴露公開方法獲取工廠對象 public static DaoFactory newInstance() { return DAO_FACTORY; } }
目前咱們操做的是User,因此工廠造UserDao對象,而UserDao對象是JDBC版呢,仍是XML版呢,咱們經過配置文件來定(這樣就更靈活了)!
#class須要的是完整的對象名稱(包括包) #userClass=zhongfucheng.dao.impl.UserImplDataBase userClass=zhongfucheng.dao.impl.UserImplXML
private static UserDao userDao = null; private DaoFactory() { try { //讀取配置文件的信息 InputStream inputStream = DaoFactory.class.getClassLoader().getResourceAsStream("UserDao.properties"); Properties properties = new Properties(); properties.load(inputStream); String userClass = properties.getProperty("userClass"); //利用反射機制建立相對應的對象 userDao = (UserDao) Class.forName(userClass).newInstance(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("讀取文件失敗了!"); } catch (IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException("反射失敗了!"); } catch (InstantiationException e) { e.printStackTrace(); throw new RuntimeException("反射失敗了!"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException("反射失敗了!"); } } public static UserDao createUserDao() { return userDao; }
UserDao userDao = DaoFactory.newInstance().createUserDao();
若是咱們的mysql驅動版本過低,就出現如下的異常!咱們只須要下載新的mysql的jar包,導入項目便可!
java.sql.SQLException: Feature not implemented Query:insert into guestbook (id,name,email,url,title,content,time) value(?,?,?,?,?,?,?) Parameters: [1, qwq,wqwq,qwqw,qw,qw, 2010-09-13]
JDBC版的成功的效果以下:
XML版的成功效果以下:
這裏寫圖片描述
樂於輸出乾貨的Java技術公衆號:Java3y。公衆號內有200多篇原創技術文章、海量視頻資源、精美腦圖,不妨來關注一下!
有幫助?在看!轉發!