關於JDBC、MYSQL的連接網上有了很多教程,但是感覺都不夠詳細。這次作業剛好要用到JDBC,所以參考網上的方法順手寫了下步驟,以供以後使用。
首先下載一個WAMP,附上網盤下載鏈接:
http://pan.baidu.com/s/1qW2Uzc4
WAMP的介紹這裡就不說了吧,集成了Mysql數據庫並且提供phpMyadmin,方便使用。
其次下載JDBC的驅動鏈接:
http://pan.baidu.com/s/1eQDylfO
最後的工具就是eclipse或者myeclipse這個網上搜下就可以,任意版本。Eclipse是免費的。
附:集成android開發環境ADT(ECLIPSE)
http://pan.baidu.com/s/1bn7yBNL
連接步驟如下:
1.首先打開phpMyadmin,創建數據庫(可以手動也可以用SQL)
CREATE DATABASE test; //創建一個數據庫
use test; //指定test為當前要操作的數據庫
之後創建表:
CREATE TABLE user (nameVARCHAR(20),password VARCHAR(20));
//創建一個表user,設置兩個字段。
插入數據:
INSERT INTO userVALUES('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",""); //連接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
即連接成功。
至於JDBC的插入數據、查詢數據、刪除數據詳細見如下代碼:
Add: JDBC_Add(String name,String character,String introduce) throws SQLException{ Connection conn = null; Statement st = null; String sql ="insert into game(gamename,gamecharacter,gameintroduce)" + "VALUES('" + name + "','" + character + "','" + introduce +"')"; conn = DriverManager.getConnection(DBURL,DBUSER,""); st = conn.createStatement(); st.executeUpdate(sql); st.close(); conn.close(); }
DEL: JDBC_DEL(String name) throws SQLException{ Connection conn = null; Statement st = null; String sql ="DELETE FROM game WHERE gamename ='" +name+"'"; //NAME是String類型所以應該加引號 conn = DriverManager.getConnection(DBURL,DBUSER,""); st = conn.createStatement(); st.executeUpdate(sql); st.close(); conn.close(); }
SEL: JDBC_SEL(String name,JLabel showname,JLabel character,JLabel introduce) throws SQLException { Connection conn = null; Statement st = null; ResultSet rs = null; String sql ="SELECT gamename,gamecharacter,gameintroduce from game WHERE gamename ='" + name + "'"; conn = DriverManager.getConnection(DBURL,DBUSER,""); st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ showname.setText(rs.getString("gamename")); character.setText(rs.getString("gamecharacter")); introduce.setText(rs.getString("gameintroduce")); } rs.close(); st.close(); conn.close(); }
UPD: public class JDBC_UPD { private static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; private static final String DBURL = "jdbc:mysql://localhost:3306/mysql"; private static final String DBUSER = "root"; JDBC_UPD(String name,String character,String introduce) throws SQLException{ Connection conn = null; Statement st = null; String sql ="UPDATE game SET gamename = '" + name + "',gamecharacter='" + character +"',gameintroduce='"+ introduce + "' WHERE gamename ='" + name + "'"; //重寫N次 conn = DriverManager.getConnection(DBURL,DBUSER,""); st = conn.createStatement(); st.executeUpdate(sql); st.close(); conn.close(); }
----------------------------------------------END-----------------------------------------------------