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條評論
主站蜘蛛池模板: 久久国产成人福利播放 | 国产精品激情福利视频 | 国内精品伊人久久久久7777人 | 精品性久久 | 国产高清一区二区 | 高清性| 色偷偷免费 | 亚洲伊人成综合网 | 欧美日韩免费在线观看 | 国产一区二区丁香婷婷 | 日韩欧美在线观看视频 | 不卡一区二区三区四区 | 欧美一区二区三区爽大粗免费 | 在线视频综合视频免费观看 | 夜夜夜爽| 久久99欧美| 添人人躁日日躁夜夜躁夜夜揉 | 欧美18 19sex性处video | 欧美日韩亚洲精品国产色 | 久久久久国产精品人 | 爱爱视频网站 | 国产免费观看一区二区三区 | 四季久久免费一区二区三区四区 | 精品久久久久久久久久 | 波多野结衣mxgs1124在线 | 午夜免费小视频 | 极品白嫩无套视频在线播放张悠雨 | 色鬼久久 | 亚洲一区二区三区免费视频 | 国产精品无码永久免费888 | 国产成人精品免费视频大全可播放的 | 精品三级视频 | 深夜电影网 | 日韩在线观看精品 | 欧美日韩一区视频 | 九九黄色| 99久久综合给久久精品 | 久草香蕉| 午夜成人在线视频 | 欧美a级成人淫片免费看 | 国产日韩精品入口 |