1. 程式人生 > >使用Eclipse的JDBC連線Mysql資料庫

使用Eclipse的JDBC連線Mysql資料庫

因為好久沒有弄資料庫和程式碼,今天心血來潮,弄個簡單的連線恢復一下記憶。

環境:MySQL任意版本;eclipse任意版本;JDBC驅動jar包;

1.MySQL安裝完畢後,建立資料庫,建立表;

Enter password:*******  //出現welcome to the mysql monitor;

mysql>create database testDB; //建立資料庫

mysql>ues testDB;//指定要操作的資料庫

mysql>create table student(sutId int ,name varchar(20));//建立一張表

mysql>insert into student values (1,'zhangsan');//插入一條資料


2.使用Eclipse匯入jar包並建立連線;

建立一個java專案,然後右鍵-Build Path--Add External Archiver--選擇JDBC驅動jar的存放位置--點選確定;


來吧,上程式碼驗證一下


如果執行結果出現‘zhangsan’說明你成功啦;

下面再試一下插入資料100條;


執行成功後,開啟資料庫驗證一下;

mysql>show databases; // 檢視所有資料庫

mysql> use testDB; //使用我們建立的資料庫

mysql> show tables;//檢視資料庫中所有的表

mysql> select * from student;//檢視當前插入資料的表

看到記錄就證明你成功啦!