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

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統 1896 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條評論
主站蜘蛛池模板: 大香久久| 天堂中文网 | 精品欧美一区二区三区 | 全日本爽视频在线 | 激情色播 | 人人香蕉 | 色综合亚洲精品激情狠狠 | 欧美性黑人极品 hd 无码一区二区三区曰本A片 | 久久国产亚洲欧美日韩精品 | 免费a视频在线观看 | 大伊香蕉在线精品不卡视频 | 国产精品久久久久久免费 | 免费观看影院 | 色婷婷综合久久久中字幕精品久久 | 日韩一区中文字幕 | 男女午夜性爽快免费视频不卡 | 九九导航 | 国产综合精品久久亚洲 | 在线精品国内外视频 | 亚洲AV久久无码精品九九九小说 | 免费久久 | 五月天欧美激情午夜情 | 精品国产免费久久久久久 | 在线看亚洲 | 国产97色在线 | 日韩 | 日本aaa级片 | 亚洲综合激情另类小说区 | 亚洲欧美视频 | 国产成人精品免费视频大全可播放的 | 亚洲图片欧洲电影 | 富二代视频污 | 免费网站看v片在线a | 91精品啪国产在线观看免费牛牛 | 久久一er精这里有精品 | 国产精品久久久天天影视香蕉 | 精品久久中文字幕 | 国产精品久久久久无码av | 五月网站| 色福利网 | 亚洲精品一区在线 | 午夜影视免费片在线观看 |