BuildPath—>ConfigureBuildPath...選擇Libraries然后再選擇右邊第二個選項AddExternalJars導入mysql-.jar3.新建數據表:CREATETABLE`user`(`ID`int(11)NOTNULLAUTO_INCREMENT,`Name`varchar(32)NOTNULL," />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Rhythmk 一步一步學 JAVA(2) : 操作 MYSQL

系統 1816 0

1. ?下載 ?如:mysql-connector-java-5.1.22.zip ?解壓獲取 jar 連接包。

2. ? 引入項目:? ? ?右鍵項目名--->Build Path—>Configure Build Path... ?選擇 ?Libraries 然后再選擇 右邊

? ? ? ? ? ? ? ? ? ? ? ? 第二個選項Add External Jars 導入 mysql -.jar

3. ? 新建數據表:

? ?

      
         CREATE TABLE `user` (

  `ID` 
      
      
        int
      
      (11
      
        ) NOT NULL AUTO_INCREMENT,

  `Name` varchar(
      
      32
      
        ) NOT NULL,

  `Age` 
      
      
        int
      
      (11
      
        ) NOT NULL,

  `AddTime` datetime NOT NULL,

  PRIMARY KEY (`ID`)

) ENGINE
      
      =InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
    

?

4. 測試程序:

? ? ?

      
        package
      
      
         App1.Rhythmk.com;




      
      
        import
      
      
         java.sql.Connection;


      
      
        import
      
      
         java.sql.DriverManager;


      
      
        import
      
      
         java.sql.ResultSet;




      
      
        import
      
      
         com.mysql.jdbc.Statement;




      
      
        /*
      
      
        

 * 

 *  建表腳本

 CREATE TABLE `user` (

  `ID` int(11) NOT NULL AUTO_INCREMENT,

  `Name` varchar(32) NOT NULL,

  `Age` int(11) NOT NULL,

  `AddTime` datetime NOT NULL,

  PRIMARY KEY (`ID`)

) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

 
      
      
        */
      
      
        public
      
      
        class
      
      
         ThirdMysql {



    
      
      
        //
      
      
         final 類似 .NET 中 const
      
      
        private
      
      
        static
      
      
        final
      
       String DatabaseConURL = "jdbc:mysql://127.0.0.1:3306/rhythmkdb"
      
        ;



    
      
      
        /**
      
      
        

     *  rhythmK

     * 1.右鍵項目名--->Build Path—>Configure Build Path... 會彈出來一個框 在那四個選項選擇

     * Libraries 然后再選擇 右邊 第二個選項Add External Jars

     
      
      
        */
      
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {

        
      
      
        //
      
      
         TODO Auto-generated method stub
      
      
                Test();

        ExcuteByParam();

        Add();

        Update();

        Select();

        Del();

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Test() {

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );

            Msg(
      
      "Mysql 連接成功。"
      
        );



        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Add() {

        String sql 
      
      = "Insert  into  `user` (Name ,Age, Addtime ) "
      
        ;

        sql 
      
      += " values ('rhythmk',21,'2012-1-1')  "
      
        ;

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );



            java.sql.Statement statement 
      
      =
      
         con.createStatement();

            statement.executeUpdate(sql);

            ResultSet result 
      
      =
      
         statement

                    .executeQuery(
      
      "select  LAST_INSERT_ID() "
      
        );

            
      
      
        int
      
       resultID = 0
      
        ;

            
      
      
        if
      
      
         (result.next()) {

                
      
      
        //
      
      
         此出索引不是從0開始 ..
      
      

                resultID = result.getInt(1
      
        );

            }

            Msg(
      
      "Add 數據庫得到返回ID" +
      
         resultID);



        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Update() {

        String sql 
      
      = " Update `user` set Name= 'rhythmk.com' where ID<3 "
      
        ;

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );



            java.sql.Statement statement 
      
      =
      
         con.createStatement();

            
      
      
        int
      
       resultID =
      
         statement.executeUpdate(sql);



            Msg(
      
      "Update 數據庫得到返回ID" +
      
         resultID);



        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Del() {

        String sql 
      
      = " Delete from `user`  where ID=3 "
      
        ;

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );



            java.sql.Statement statement 
      
      =
      
         con.createStatement();

            
      
      
        int
      
       resultID =
      
         statement.executeUpdate(sql);



            Msg(
      
      "Del 數據庫得到返回ID" +
      
         resultID);



        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Select() {

        String sql 
      
      = " Select ID,Name from `user`   "
      
        ;

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );



            java.sql.Statement statement 
      
      =
      
         con.createStatement();

            ResultSet result 
      
      =
      
         statement.executeQuery(sql);



            String resultStr 
      
      = ""
      
        ;

            
      
      
        while
      
      
         (result.next()) {

                resultStr 
      
      += "\r\n ID=" + result.getString(1
      
        );

                resultStr 
      
      += ", Name=" + result.getString("Name"
      
        );

            }



            Msg(resultStr);

            
      
      
        //
      
      
         statement.executeUpdate(sql);
      
      
        

        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         ExcuteByParam() {

        String sql 
      
      = "select * from  `user` where Name =?"
      
        ;

        
      
      
        try
      
      
         {

            Connection con 
      
      = 
      
        null
      
      
        ;

            Class.forName(
      
      "com.mysql.jdbc.Driver"
      
        ).newInstance();

            con 
      
      =
      
         DriverManager

                    .getConnection(DatabaseConURL, 
      
      "root", "wangkun"
      
        );



            java.sql.PreparedStatement preparedsm 
      
      =
      
         con.prepareStatement(sql);

            preparedsm.setString(
      
      1, "rhythmk"
      
        );

            ResultSet result 
      
      =
      
         preparedsm.executeQuery();



            String resultStr 
      
      = ""
      
        ;

            
      
      
        while
      
      
         (result.next()) {

                resultStr 
      
      += "\r\n ID=" + result.getString(1
      
        );

                resultStr 
      
      += ", Name=" + result.getString("Name"
      
        );

            }



            Msg(
      
      "ExcuteByParam:\r\n" +
      
         resultStr);

            
      
      
        //
      
      
         statement.executeUpdate(sql);
      
      
        

        } 
      
      
        catch
      
      
         (Exception ex) {

            Msg(ex.toString());

        }

    }



    
      
      
        public
      
      
        static
      
      
        void
      
      
         Msg(String msg) {

        System.out.println(msg);

    }



}
      
    

?

?

?

Rhythmk 一步一步學 JAVA(2) : 操作 MYSQL 數據庫


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲女bbwxxxx另类 | 九九香蕉网 | 日韩毛片在线 | 一级毛片全部免费播放 | 国产手机精品一区二区 | 久久午夜剧场 | 亚洲夜色综合久久 | 亚洲国产成+人+综合 | 亚洲综合日本 | 好吊妞视频一区二区 | 韩日一区二区三区 | 精品久久久久久中文字幕欧美 | 2019天天干天天操 | 欧美一区二区三区免费视频 | 亚洲精品14p | 久久久久国产视频 | 国产日韩久久 | 日韩不卡在线 | 亚洲综合精品香蕉久久网 | 超清中文乱码字幕在线观看 | 久久久久久久免费 | 亚洲久久久久久久 | 亚洲第一二三四区 | 国产亚洲精品国产福利在线观看 | 国内精品综合九九久久精品 | 日韩视频不卡 | 国产精品videossex激情 | 综合亚洲精品一区二区三区 | 国产成人综合网在线观看 | 日韩欧美综合 | 九九自拍| 色噜噜狠狠色综合免费视频 | 不卡中文字幕 | 亚洲高清一区二区三区久久 | 婷婷综合社区 | 久久免费视频网站 | 国产精品嫩草研究所永久网址 | 国产成人久久精品推最新 | 久久久这里只有精品免费 | 奇米77777| 一二三四社区在线播放 |