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條評論
主站蜘蛛池模板: 黄色片视频在线观看 | 亚洲a网 | 国产日韩精品一区二区 | 亚洲综合在线播放 | 午夜影剧 | 激情奇米| 大开眼界 无删减 | 丁香六月婷婷在线 | 高清一区二区亚洲欧美日韩 | bt日韩| 成人精品鲁一区一区二区 | 精品国产一区二区三区久久 | 久久久无码精品亚洲日韩按摩 | 色多多成视频人在线观看 | 一级黄色大毛片 | 五月婷婷开心综合 | 深夜福利软件 | 亚洲奇米| 男女性爽大片在线观看 | 国产亚洲精品久久精品录音 | 日本高清天码一区在线播放 | 国产精品无码2021在线观看 | 日韩精品 电影一区 亚洲 | 婷婷久久综合 | 欧美日韩亚洲国产 | 九九精品免费 | 在线播放一区二区三区 | 欧洲成人午夜免费大片 | 久草视频精品 | 精品一区二区国语对白 | 好吊妞gao988在线播放 | 国产精选91热在线观看 | 国产精品啪一品二区三区粉嫩 | 日韩一级在线 | 亚欧精品一区二区三区四区 | 久久av电影院 | 亚洲依依成人综合网站 | 99久久99 | snh48欧洲大片在线观看 | 亚洲欧美在线观看 | 亚洲成a人在线观看 |