亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

BaseJDBC和CRUDDAO的寫(xiě)法實(shí)例代碼

 更新時(shí)間:2017年09月21日 10:25:49   作者:周振宇  
這篇文章主要介紹了BaseJDBC和CRUDDAO的寫(xiě)法實(shí)例代碼,代碼注釋十分詳細(xì),具有一定參考價(jià)值,需要的朋友可以了解下。

我們首先看下BASEJDBC的寫(xiě)法實(shí)例:

package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class BaseJDBC {
 // 表示你要操作的是哪種類(lèi)型的數(shù)據(jù)庫(kù)
 private final String DRIVER = "com.mysql.jdbc.Driver";
 // 表示你要連接的是哪一臺(tái)電腦的服務(wù)器端口號(hào)是多少數(shù)據(jù)庫(kù)的名字是什么
 private final String URL = "jdbc:mysql://localhost:3306/zzy";//有時(shí)這里需要加上字符集
 // 登錄數(shù)據(jù)庫(kù)的用戶(hù)名
 private final String USERNMAE = "root";
 // 登錄數(shù)據(jù)庫(kù)的密碼
 private final String PASSWORD = "root";
 /**
  * 注冊(cè)驅(qū)動(dòng) 獲取連接
  * 
  * @return
  */
 public Connection getConnection() {
  try {
   //Driver d=new Driver();
   // 注冊(cè)驅(qū)動(dòng):反射(是一項(xiàng)很高深的技術(shù))
   Class.forName(DRIVER);
   // 由連接大管家創(chuàng)建連接對(duì)象
   return DriverManager.getConnection(URL, USERNMAE, PASSWORD);
  } catch (ClassNotFoundException e) {
   //e.printStackTrace("數(shù)據(jù)庫(kù)的驅(qū)動(dòng)文件沒(méi)有找到");
  } catch (SQLException e) {
   //數(shù)據(jù)庫(kù)的連接錯(cuò)誤
   e.printStackTrace();
  }
  return null;
 }
 /**
  * 關(guān)閉連接釋放資源
  * @param con
  * @param st
  * @param rt
  */
 public void closeAll(Connection con, Statement st, ResultSet rt) {
  try {
   if (rt != null) {
    rt.close();
    rt = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   if (st != null) {
    st.close();
    st = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   if (con != null) {
    con.close();
    con = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

CRUDDAO 寫(xiě)法代碼實(shí)例:

package com.dao;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;
import java.util.Map.Entry;
/**
 * 
 * @author zzy
 *
 * 2016年12月1日下午1:49:49
 */
public class CRUDDAO<T> extends BaseJDBC {
 private Connection con = null;
 private PreparedStatement pt = null;
 private Statement st = null;
 private ResultSet rt = null;
 private Class<T> c;
 public CRUDDAO() {
 }
 public CRUDDAO(Class<T> c) {
  this.c = c;
 }
 /**
  * 查詢(xún)操作要改造的地方 第一:參數(shù)必須抽象 第二:返回類(lèi)型必須抽象
  * 
  * @param <T>
  * @param <T>
  * 
  * @return Map<Integer, List<T>>
  */
 public Map<Integer, List<T>> selectAll(Map<String, Object[]> m) {
  int index = 0;
  Map<Integer, List<T>> map = new LinkedHashMap<Integer, List<T>>();
  List<T> list = null;
  try {
   con = super.getConnection();
   if (con != null) {
    Set<Entry<String, Object[]>> set = m.entrySet();
    for (Entry<String, Object[]> entry : set) {
     list = new ArrayList<T>();
     pt = con.prepareStatement(entry.getKey());
     this.bind(entry.getValue());
     rt = pt.executeQuery();
     while (rt.next()) {
      list.add(this.toBean2());
     }
     map.put(++index, list);
    }
   } else {
    System.out.println("數(shù)據(jù)庫(kù)連接失敗");
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   super.closeAll(con, pt, rt);
  }
  return map;
 }
 /**
  * 將數(shù)據(jù)庫(kù)查詢(xún)到的數(shù)據(jù)進(jìn)行封裝 封裝成實(shí)體類(lèi)之后再返回給調(diào)用者
  * 
  * @return
  */
 private T toBean() {
  T t = null;
  try {
   t = c.newInstance();
   Method[] m = c.getMethods();
   ResultSetMetaData rmt = rt.getMetaData();
   for (int i = 1, count = rmt.getColumnCount(); i <= count; i++) {
    String columName = rmt.getColumnName(i);
    columName = "set" + columName.substring(0, 1).toUpperCase()
      + columName.substring(1);
    for (int j = 0; j < m.length; j++) {
     if (columName.equals(m[j].getName())) {
      m[j].invoke(t, rt.getObject(i));
      break;
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return t;
 }
 private T toBean2() {
  T t = null;
  try {
   // 創(chuàng)建反射類(lèi)的實(shí)例
   t = c.newInstance();
   // 反射出所有字段
   Field[] field = c.getDeclaredFields();
   for (Field f : field) {
    // 根據(jù)反射的字段名得到數(shù)據(jù)庫(kù)中的字段值
    Object value = rt.getObject(f.getName());
    f.setAccessible(true);// 打開(kāi)私有字段的操作權(quán)限
    f.set(t, value);// 調(diào)用這個(gè)字段的公有的set方法封裝字段的值
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return t;
 }
 /**
  * 綁定參數(shù)
  * 
  * @param obj
  */
 private void bind(Object[] obj) {
  try {
   if (obj != null) {
    for (int i = 0, k = obj.length; i < k; i++) {
     pt.setObject(i + 1, obj[i]);
    }
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 /**
  * 修改操作 進(jìn)行的事務(wù)的控制 所有命令要么同時(shí)提交成功 要么同時(shí)回滾
  * 
  * @param name
  * @param id
  * @return
  */
 public int[] updateAll(Map<String, Object[]> map) {
  int[] row = new int[map.size()];
  int index = 0;
  int error = 0;
  try {
   con = super.getConnection();
   if (con != null) {
    Set<Entry<String, Object[]>> set = map.entrySet();
    // 關(guān)閉連接對(duì)象的自動(dòng)提交的功能
    con.setAutoCommit(false);
    for (Entry<String, Object[]> entry : set) {
     pt = con.prepareStatement(entry.getKey());
     this.bind(entry.getValue());
     row[index] = pt.executeUpdate();
     if (row[index] == 0) {
      throw new Exception("修改失敗,數(shù)據(jù)回滾!");
     }
     index++;
    }
   } else {
    System.out.println("數(shù)據(jù)庫(kù)連接失敗");
   }
  } catch (Exception e) {
   error++;
   e.printStackTrace();
  } finally {
   if (error > 0) {
    try {
     // 將前面已經(jīng)執(zhí)行的命令回滾
     con.rollback();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   } else {
    try {
     // 全部提交
     con.commit();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   super.closeAll(con, st, null);
  }
  return row;
 }
}

總結(jié)

以上就是本文關(guān)于BaseJDBC和CRUDDAO的寫(xiě)法實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • spring注入在有常量的情況下使用@AllArgsConstructor操作

    spring注入在有常量的情況下使用@AllArgsConstructor操作

    這篇文章主要介紹了spring注入在有常量的情況下使用@AllArgsConstructor操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java的位圖和布隆過(guò)濾器深入詳細(xì)講解

    Java的位圖和布隆過(guò)濾器深入詳細(xì)講解

    這篇文章主要介紹了Java的位圖和布隆過(guò)濾器,在學(xué)習(xí)之前的數(shù)據(jù)結(jié)構(gòu)的時(shí)候,我們使用的數(shù)據(jù)量都不是很大,但是在生活中,我們常常面臨著要處理大量數(shù)據(jù)結(jié)果并得出最終結(jié)果,那么有沒(méi)有什么數(shù)據(jù)結(jié)構(gòu)可以幫助我們實(shí)現(xiàn)這樣的功能呢,想要繼續(xù)了解的朋友可以參考下
    2024-10-10
  • JAVA如何定義構(gòu)造函數(shù)過(guò)程解析

    JAVA如何定義構(gòu)造函數(shù)過(guò)程解析

    這篇文章主要介紹了JAVA如何定義構(gòu)造函數(shù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解

    Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解

    這篇文章主要介紹了Java8的DateTimeFormatter與SimpleDateFormat的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java多線(xiàn)程中不同條件下編寫(xiě)生產(chǎn)消費(fèi)者模型方法介紹

    Java多線(xiàn)程中不同條件下編寫(xiě)生產(chǎn)消費(fèi)者模型方法介紹

    這篇文章主要介紹了Java多線(xiàn)程中不同條件下編寫(xiě)生產(chǎn)消費(fèi)者模型方法介紹,介紹了生產(chǎn)消費(fèi)者模型,然后分享了相關(guān)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • SpringBoot整合EasyExcel實(shí)現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出

    SpringBoot整合EasyExcel實(shí)現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合EasyExcel實(shí)現(xiàn)復(fù)雜Excel表格的導(dǎo)入導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下
    2023-11-11
  • Java字符串常量池示例詳解

    Java字符串常量池示例詳解

    作為最基礎(chǔ)的引用數(shù)據(jù)類(lèi)型,Java設(shè)計(jì)者為 String 提供了字符串常量池以提高其性能,下面這篇文章主要給大家介紹了關(guān)于Java字符串常量池的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Java編程中的檢查型異常與非檢查型異常分析

    Java編程中的檢查型異常與非檢查型異常分析

    這篇文章主要介紹了Java編程中的未檢查型異常與非檢查型異常,以及異常的處理方式,需要的朋友可以參考下
    2017-09-09
  • springBoot?@Scheduled實(shí)現(xiàn)多個(gè)任務(wù)同時(shí)開(kāi)始執(zhí)行

    springBoot?@Scheduled實(shí)現(xiàn)多個(gè)任務(wù)同時(shí)開(kāi)始執(zhí)行

    這篇文章主要介紹了springBoot?@Scheduled實(shí)現(xiàn)多個(gè)任務(wù)同時(shí)開(kāi)始執(zhí)行,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能的示例代碼

    springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10

最新評(píng)論