1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        JAVA操作MySQL數(shù)據(jù)庫

        共 2034字,需瀏覽 5分鐘

         ·

        2020-12-15 03:56

        JAVA操作MySQL數(shù)據(jù)庫,涉及創(chuàng)建連接,創(chuàng)建表,插入數(shù)據(jù),更新數(shù)據(jù),查詢數(shù)據(jù)

        總體步驟:

        1) 獲取驅(qū)動(可以省略)

        2) 獲取連接

          Connection 接口,代表一個連接對象 ,具體的實現(xiàn)類由數(shù)據(jù)庫的廠商實現(xiàn)

          使用 DriverManager類的靜態(tài)方法,getConnection可以獲取數(shù)據(jù)庫的連接

        3) 獲取Statement對象

          通過Connection 的 createStatement方法 獲取sql語句執(zhí)行對象

        4) 處理結(jié)果集(只在查詢時處理)

        5) 釋放資源


        代碼如下:

        package jdbc;import java.sql.*;
        public class Test01 { public static void main(String[] args) throws ClassNotFoundException, SQLException {     //使用驅(qū)動; Class.forName("com.mysql.jdbc.Driver");     // 創(chuàng)建連接,涉及數(shù)據(jù)庫IP,端口,數(shù)據(jù)庫名,字符集,賬號及密碼 String url = "jdbc:mysql://127.0.0.1:3307/testdb?characterEncoding=UTF-8"; Connection conn = DriverManager.getConnection(url,"root","Root#123456"); //System.out.println(conn); // 獲取語句執(zhí)行平臺對象 Statement Statement smt = conn.createStatement(); // 創(chuàng)建表 executeUpdate方法 String sql1 = "create table if not exists test14(id int primary key auto_increment ,name varchar(20),age int);"; smt.executeUpdate(sql1); // 插入數(shù)據(jù) String sql_i = "insert into test14 values(1,'劉備',45),(2,'關(guān)羽',40),(3,'張飛',37),(4,'趙云',30),(5,'諸葛亮',27);"; smt.executeUpdate(sql_i);
        // 更新數(shù)據(jù) String sql_u= "update test14 set age = 36 where name='張飛';"; smt.executeUpdate(sql_u);
        // 查詢結(jié)果 String sql_q = "select * from test14;"; ResultSet res = smt.executeQuery(sql_q); while(res.next()){ int id = res.getInt(1); String name= res.getString("name"); int age = res.getInt("age"); System.out.println("id:"+ id + " name:" + name +" age:"+age); }

        // 關(guān)閉流 (先開后關(guān)) res.close(); smt.close(); conn.close();
        }
        }

        ?結(jié)果如下:

        ?

        往期精彩回顧

        1.??MySQL高可用之MHA集群部署

        2.??mysql8.0新增用戶及加密規(guī)則修改的那些事

        3.? 比hive快10倍的大數(shù)據(jù)查詢利器-- presto

        4. ?監(jiān)控利器出鞘:Prometheus+Grafana監(jiān)控MySQL、Redis數(shù)據(jù)庫

        5.? PostgreSQL主從復(fù)制--物理復(fù)制

        6.??MySQL傳統(tǒng)點位復(fù)制在線轉(zhuǎn)為GTID模式復(fù)制

        7.??MySQL敏感數(shù)據(jù)加密及解密

        8.??MySQL數(shù)據(jù)備份及還原(一)

        9.??MySQL數(shù)據(jù)備份及還原(二)












        關(guān)





        瀏覽 47
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产午夜成人视频 | 欧美日韩一区二区三区不卡视频 | 欧美a在线视频 | 操我网| 国产精品成人一区二区三区电影毛片 | 69av视频在线观看 | 做爱免费网址 | 一二三区免费视频 | 我要看操比 | 亚洲欧美爱爱 |