JAVA高級(jí)編程系列之JDBC連接數(shù)據(jù)庫(kù)(持續(xù)更新)

JDBC(推薦學(xué)習(xí)網(wǎng)站:stackoverflow)

在這里先給出可能會(huì)用到的資源包括以下:

  • MySql驅(qū)動(dòng)jar包
  • Oracle驅(qū)動(dòng)jar包
  • navicatMySql數(shù)據(jù)庫(kù)的可視化工具(navicat110_mysql_cs_x86為中文,另一個(gè)英文,英文沒(méi)有破解說(shuō)明,不會(huì)破解的可以在下方評(píng)論)
  • Oracle數(shù)據(jù)庫(kù)的可視化工具
  • mySql安裝包和安裝說(shuō)明(mySql資料有安裝詳細(xì)說(shuō)明)
  • oracle安裝包和安裝說(shuō)明,這個(gè)Oracle比較?。▽?duì)于學(xué)習(xí)來(lái)說(shuō)足夠了),而且卸載方便不必像其它安裝包的oracle,安裝之后難以卸載干凈。
    需要的點(diǎn)擊以下鏈接獲?。ㄊЯ嗽谙路皆u(píng)論):JDBC編程工具
sql 標(biāo)準(zhǔn)化查詢語(yǔ)言
sun          【驅(qū)動(dòng)】    數(shù)據(jù)庫(kù)廠商
Java語(yǔ)言            mysql
                    oracle

標(biāo)準(zhǔn)                實(shí)現(xiàn)

數(shù)據(jù)庫(kù)連接步驟:

  1. 注冊(cè)驅(qū)動(dòng)

    • mysql驅(qū)動(dòng)

    • oracle驅(qū)動(dòng)

    • ...

        Class.forName(driver);
      
  2. 獲取連接
    連接 url
    url
    jdbc:oracle:thin:@localhost:1521:XE
    jdbc:mysql://127.0.0.1:3306/zmysql
    user root
    password root

     Connection conn = ConnectionFactory.getConnection();
    
  3. 創(chuàng)建pstmt/stmt對(duì)象
    如果有占位符替換占位符

         String sql = "insert into tbl_student values(null,'"+stu.getName()+"',"+stu.getAge()+")";
         pstmt = conn.prepareStatement(sql);
    
  4. 執(zhí)行sql

     int executeUpdate();
         增 刪  該
     Result executeQuery();
         查
     
     execute();
    
  5. 如果有結(jié)果集,處理結(jié)果集

     while(rs.next()){
                 long id = rs.getLong("id");
                 String name = rs.getString("name");
                 int age = rs.getInt("age");
                 System.out.println(id+"=="+name+"=="+age);
             }
    
  6. 釋放資源
    Connection
    PreparedStatement
    ResultSet
    后創(chuàng)建的先釋放 (一般放在try{}catch{}的finally{})

     if(rs!=null){
         rs.close();
     }
     if(pstmt!=null){
         pstmt.close();
     }
     if(conn!=null){
         conn.close();
     }
    
額外知識(shí)點(diǎn)
             三層架構(gòu)  
    1. 數(shù)據(jù)訪問(wèn)層          (jdbc)    bean  
                                dao
    2. 業(yè)務(wù)邏輯處理層                service(面向接口編程)
    3. 視圖層                        web
      |
      M 數(shù)據(jù)
      
    V    C
    視圖    控制器

封裝的源碼

public class ConnectionFactory {
private static String driver;
private static String url;
private static String user;
private static String password;

static {
    driver = "com.mysql.jdbc.Driver";
    url = "jdbc:mysql://127.0.0.1:3306/tbl_student";
    user = "root";
    password = "13870775439z";
    // 從文件系統(tǒng)中獲取參數(shù)
}

/**
 * 獲取連接
 * */
public static Connection getConnection() throws Exception {
    Class.forName(driver);
    return DriverManager.getConnection(url, user, password);
}

/**
 * 釋放資源
 * */
public static void close(ResultSet rs, PreparedStatement pstmt,
        Connection conn) throws SQLException {
    if (rs != null) {
        rs.close();
    }
    if (pstmt != null) {
        pstmt.close();
    }
    if (conn != null) {
        conn.close();
    }
}

/*
 * 插入和更新
 */
public static int Operator(String sql) {
    int num = 0;
    try {
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = ConnectionFactory.getConnection();
            pstmt = connection.prepareStatement(sql);
            num = pstmt.executeUpdate();
        } finally {
            ConnectionFactory.close(null, pstmt, connection);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return num;
}

/*
 * 查詢
 */
public static String queryOperator(String sql,String username) {
    
    String result="查詢失敗";
    try {
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;
        try {
            conn = ConnectionFactory.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()) {
                String name = rs.getString("name");
                if (usernamae.equals(name)) {
                    result="查詢成功";
                    break;
                }
            }
        } finally {

            ConnectionFactory.close(rs, pstmt, conn);
        }

    } catch (Exception e) {
        // TODO: handle exception
    }
    return result;
}
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,734評(píng)論 18 399
  • 本人的環(huán)境為Myeclipse10、MySQL5.7.15 本文包括:簡(jiǎn)介JDBC編程步驟打通數(shù)據(jù)庫(kù)程序詳解—Dr...
    廖少少閱讀 4,357評(píng)論 7 39
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,017評(píng)論 0 11
  • 致歉是一門(mén)藝術(shù)。 然而,大多數(shù)人將它當(dāng)做了一種表演。 于是,生活中我們遇到的“對(duì)不起”,不是套路的敷衍,就是無(wú)力的...
    sanko三口閱讀 623評(píng)論 0 1
  • 我家小狗真可愛(ài)! 一個(gè)叫豆豆, 惹人喜愛(ài) 一個(gè)叫糖寶,花見(jiàn)花開(kāi) 一個(gè)叫帥帥,貪吃耍賴 這些小東西讓人愛(ài)不釋手, 個(gè)...
    牛淼銳閱讀 240評(píng)論 0 2

友情鏈接更多精彩內(nèi)容