Eclipse连接MySQL数据库(傻瓜篇)
本來不想寫這么簡單人文章,在百度上搜索我這個標題,完全符合標題的一大堆。但我按照那些文章?lián)v鼓了很久,就是不行。
?
我的環(huán)境:MySQL:mysql-essential-5.1.51-win32
jdbc驅(qū)動:我已經(jīng)上傳到csdn上一個:http://download.csdn.net/source/3451945
Eclipse:任意版本,免費的,可以百度的到。
1。MySQL安裝,不會的朋友可以看連接:http://www.duote.com/tech/1/2430_1.html
?? ?下面來創(chuàng)建一個數(shù)據(jù):
mysql>CREATE DATABASE test; //創(chuàng)建一個數(shù)據(jù)庫mysql>use test; //指定test為當前要操作的數(shù)據(jù)庫mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //創(chuàng)建一個表user,設置兩個字段。mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一條數(shù)據(jù)到表中
2。打開Eclipse,創(chuàng)建一個項目(my),
操作:右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅(qū)動,點擊確定。
我的項目列表:
?
?
3。驅(qū)動已經(jīng)導入,下面我們來寫一個程序驗證一下
?
import java.sql.*; publicclass MysqlJdbc {publicstaticvoid main(String args[]) {try {Class.forName("com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅(qū)動程序 //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//服務器地址/數(shù)據(jù)庫名 ,后面的2個參數(shù)分別是登陸用戶名和密碼 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
出現(xiàn)上面結(jié)果,說明你連接數(shù)據(jù)庫成功。
4。可以查看到MySQL里面的內(nèi)容,那我們是不是想往MySQL中插入數(shù)據(jù)呢。 下面的例子,往MySQL的user表中插入100條數(shù)據(jù) import java.sql.*;publicclass Myjproject {publicstaticvoid main(String args[]){try {Class.forName("com.mysql.jdbc.Driver"); //加載MYSQL JDBC驅(qū)動程序 //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次的循環(huán),往表里插入一百條信息。 {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數(shù)據(jù)庫進行查看
| mysql> show databases;? //查看所數(shù)據(jù)庫 |
| mysql> use? test;??? //使test為當前要操作的數(shù)據(jù)庫 |
| mysql> show tables; //查看當前數(shù)據(jù)庫的所有表 |
| mysql> select *from user;? //查看當前表(user)的所有信息 |
?
注意:如果不能正常連接你的數(shù)據(jù)庫,請檢查你代碼中,驅(qū)動、用戶名、密碼、表等信息是否對應無誤,不要把別人的代碼直接復制過來,看也不看就用。
from: http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html
總結(jié)
以上是生活随笔為你收集整理的Eclipse连接MySQL数据库(傻瓜篇)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中通过JDBC操作MySQL数据
- 下一篇: JAVA数据库编程(JDBC技术)-入门