qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Eclipse連接MySQL數據庫(傻瓜篇)

            本來不想寫這么簡單人文章,在百度上搜索我這個標題,完全符合標題的一大堆。但我按照那些文章搗鼓了很久,就是不行。

          我的環境:MySQL:mysql-essential-5.1.51-win32

               jdbc驅動:我已經上傳到csdn上一個:http://download.csdn.net/source/3451945

               Eclipse:任意版本,免費的,可以百度的到。

          1。MySQL安裝,不會的朋友可以看連接:http://www.duote.com/tech/1/2430_1.html

              下面來創建一個數據: 

          復制代碼
          mysql>CREATE DATABASE test;   //創建一個數據庫

          mysql
          >use test; //指定test為當前要操作的數據庫

          mysql
          >CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //創建一個表user,設置兩個字段。

          mysql
          >INSERT INTO user VALUES('huzhiheng','123456'); //插入一條數據到表中
          復制代碼

            

          2。打開Eclipse,創建一個項目(my),

          操作:右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅動,點擊確定。

          我的項目列表:



          3。驅動已經導入,下面我們來寫一個程序驗證一下

          復制代碼
          import java.sql.*;
          public class MysqlJdbc {
          public static void main(String args[]) {
          try {
          Class.forName(
          "com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅動程序
          //Class.forName("org.gjt.mm.mysql.Driver");
          System.out.println("Success loading Mysql Driver!");
          }
          catch (Exception e) {
          System.out.print(
          "Error loading Mysql Driver!");
          e.printStackTrace();
          }
          try {
          Connection connect
          = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test","root","198876");
          //連接URL為 jdbc:mysql//服務器地址/數據庫名 ,后面的2個參數分別是登陸用戶名和密碼

          System.out.println(
          "Success connect Mysql server!");
          Statement stmt
          = connect.createStatement();
          ResultSet rs
          = stmt.executeQuery("select * from user");
          //user 為你表的名稱
          while (rs.next()) {
          System.out.println(rs.getString(
          "name"));
          }
          }
          catch (Exception e) {
          System.out.print(
          "get data error!");
          e.printStackTrace();
          }
          }
          }
          復制代碼

          點擊運行程序:  

          Success loading Mysql Driver!

          Success connect Mysql server!

          huzhiheng  

          出現上面結果,說明你連接數據庫成功。

          4。可以查看到MySQL里面的內容,那我們是不是想往MySQL中插入數據呢。
          下面的例子,往MySQL的user表中插入100條數據
          復制代碼
          import java.sql.*;

          public class Myjproject {
          public static void main(String args[])
          {
          try {
          Class.forName(
          "com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅動程序
          //Class.forName("org.gjt.mm.mysql.Driver");
          System.out.println("Success loading Mysql Driver!");
          }
          catch (Exception e) {
          System.out.print(
          "Error loading Mysql Driver!");
          e.printStackTrace();
          }
          try {
          Connection connect
          = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876");

          int num=100;
          PreparedStatement Statement
          =connect.prepareStatement("INSERT INTO user VALUES(?,?)");
          for(int i=0;i<num;i++) //定義個100次的循環,往表里插入一百條信息。
          {
          Statement.setString(
          1,"chongshi"+i);
          Statement.setString(
          2,"bo"+i);
          Statement.executeUpdate();
          }

          // } catch (ClassNotFoundException e) {
          // TODO Auto-generated catch block
          // System.out.println("An error has occurred:"+e.toString());
          // e.printStackTrace();
          }catch(SQLException e)
          {
          }
          }
          }
          復制代碼

           

          5.下面我們打開MySQL數據庫進行查看 

          mysql> show tatabases;  //查看所數據庫
          mysql> use  test;    //使test為當前要操作的數據庫
          mysql> show tables; //查看當前數據庫的所有表
          mysql> select *from user;  //查看當前表(user)的所有信息


          注意:如果不能正常連接你的數據庫,請檢查你代碼中,驅動、用戶名、密碼、表等信息是否對應無誤,不要把別人的代碼直接復制過來,看也不看就用。

          分類: Java

          posted on 2012-05-02 14:48 順其自然EVO 閱讀(534) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 岗巴县| 阿合奇县| 师宗县| 礼泉县| 临洮县| 汉中市| 双江| 新建县| 汉寿县| 镇巴县| 锦屏县| 苗栗市| 汝阳县| 丰都县| 松阳县| 永顺县| 新营市| 神池县| 来安县| 漯河市| 新邵县| 晋城| 岳普湖县| 吉隆县| 临泽县| 莎车县| 遂宁市| 万宁市| 荣成市| 保靖县| 舒城县| 天津市| 阿拉善右旗| 涞源县| 万山特区| 城口县| 横峰县| 岗巴县| 巢湖市| 全椒县| 邹平县|