最近可能需要用到用java調(diào)用MySQL數(shù)據(jù)庫(kù),java提供了接口,打算趕緊學(xué)來(lái)用用碎节!
軟件使用:Eclipse。
下載
java連接MySQL需要驅(qū)動(dòng)包抵卫,所以需要下載jar包狮荔。網(wǎng)上各處都有提供.
一般長(zhǎng)這樣
mysql-connector-java-5.1.38.jar
配置
1.新建一個(gè)項(xiàng)目胎撇,在自己的項(xiàng)目新建一個(gè)文件夾(用來(lái)存儲(chǔ)jar包)
將自己下載的jar包直接拖入這個(gè)文件夾就行了。
2.接下來(lái)在項(xiàng)目下右鍵選擇Configure Build Path
3.選擇Libraries
下的Add JARS...
找到下載的jar包殖氏,完成晚树!
使用
首先import sql包
import java.sql.*;
在自己本身MySQL有一個(gè)數(shù)據(jù)庫(kù)后
我們創(chuàng)建一些初始數(shù)據(jù)
/*
MySQL的JDBC URL編寫方式:
jdbc:mysql://主機(jī)名稱:連接端口/數(shù)據(jù)庫(kù)名稱?參數(shù)=值
為了避免中文亂碼要設(shè)置"characterEncoding=utf8"
*/
//創(chuàng)建數(shù)據(jù)庫(kù)URL
static final String DB_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true";
// 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置
static final String USER = "root";
static final String PASS = "root";
創(chuàng)建完雅采,在主函數(shù)加載驅(qū)動(dòng)程序及使用
public static void main(String[] args) {
Connection con = null;
//1. 注冊(cè) JDBC 驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加載MySQL驅(qū)動(dòng)");
//2. 獲得數(shù)據(jù)庫(kù)連接
System.out.println("連接數(shù)據(jù)庫(kù)...");
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("成功連接數(shù)據(jù)庫(kù)");
//3. 成功即可操作數(shù)據(jù)庫(kù)爵憎,創(chuàng)建Statenment實(shí)例
Statement stmt = conn.createStatement();
// Statement里面帶有很多方法,比如executeUpdate可以實(shí)現(xiàn)插入婚瓜,更新和刪除等
ResultSet rs = stmt.executeQuery("SELECT * FROM websites");
//4. 如果有數(shù)據(jù)宝鼓,rs.next()返回true
while(rs.next()){
System.out.println(" 名稱:"+rs.getString("name")+" 地址:"+rs.getString("url"));
}
}
最簡(jiǎn)單就是上面四個(gè)步驟,我分別解釋了上面意思巴刻。
其實(shí)還需要有處理各種錯(cuò)誤的情況愚铡,還有斷開(kāi)連接的函數(shù)。
讓我們看看結(jié)果呈現(xiàn)上面吧
可見(jiàn)已經(jīng)成功連接數(shù)據(jù)庫(kù)胡陪,并且用sql語(yǔ)句調(diào)用數(shù)據(jù)庫(kù)沥寥。
創(chuàng)建數(shù)據(jù)表并插入數(shù)據(jù)
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
try {
//1.加載驅(qū)動(dòng)程序
Class.forName("com.mysql.jdbc.Driver");
//2. 獲得數(shù)據(jù)庫(kù)連接
System.out.println("連接數(shù)據(jù)庫(kù)...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//3.操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)增刪改查
stmt = conn.createStatement();
String mysql;
mysql="create table student(NO char(20),name varchar(20),primary key(NO))";
//創(chuàng)建表格
int result=stmt.executeUpdate(mysql);
//在執(zhí)行之后柠座,查看受影響行數(shù)邑雅,返回-1代表不成功
if(result!=-1) {
System.out.println("創(chuàng)建students表格成功");
mysql = "insert into student(NO,name) values('2016001','haige')";
result= stmt.executeUpdate(mysql);
mysql = "insert into student(NO,name) values('2016002','Mr.chen')";
result= stmt.executeUpdate(mysql);
System.out.println("插入完成");
mysql = "select * from student";
ResultSet rs = stmt.executeQuery(mysql);
//執(zhí)行查詢語(yǔ)句看結(jié)果
System.out.println("學(xué)號(hào)\t姓名");
while(rs.next()){
String student_id=rs.getString(1);
String student_name =rs.getString(2);
System.out.println(student_id+"\t"+student_name);
}
rs.close();
stmt.close();
conn.close();
}
}catch (SQLException e) {
System.out.println("MySQL操作錯(cuò)誤");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("程序結(jié)束!");
}
以上我加入了try-catch是為了在錯(cuò)誤時(shí)可以檢測(cè)出錯(cuò)誤的地方妈经,可以不加淮野。
還加入了close關(guān)閉數(shù)據(jù)庫(kù)的連接的操作。
更新刪除操作
//SQL語(yǔ)句
update student set name=? where NO='2016002'
現(xiàn)在對(duì)數(shù)據(jù)庫(kù)進(jìn)行更新刪除操作
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
try {
//1.加載驅(qū)動(dòng)程序
Class.forName("com.mysql.jdbc.Driver");
//2. 獲得數(shù)據(jù)庫(kù)連接
System.out.println("連接數(shù)據(jù)庫(kù)...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//3.操作數(shù)據(jù)庫(kù)狂塘,實(shí)現(xiàn)增刪改查
stmt = conn.createStatement();
String mysql,mysql2,mysql3;
//更新操作
mysql="update student set name=? where NO='2016002'";
PreparedStatement pst=conn.prepareStatement(mysql);
pst.setString(1, "chenxx");
pst.executeUpdate();
System.out.println("更新為chenxx完成");
//刪除操作
mysql2="delete from student where name=?";
pst = conn.prepareStatement(mysql2);
pst.setString(1, "haige");
pst.executeUpdate();
System.out.println("刪除haige完成");
//查詢操作
mysql3="select * from student";
ResultSet rs = stmt.executeQuery(mysql3);
System.out.println("查詢完成");
//執(zhí)行查詢語(yǔ)句看結(jié)果
System.out.println("學(xué)號(hào)\t姓名");
while(rs.next()){
String student_id=rs.getString(1);
String student_name =rs.getString(2);
System.out.println(student_id+"\t"+student_name);
}
rs.close();
stmt.close();
conn.close();
}catch (SQLException e) {
System.out.println("MySQL操作錯(cuò)誤");
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("程序結(jié)束录煤!");
}
很好,結(jié)果如我們所料荞胡!
現(xiàn)在暫時(shí)沒(méi)啥問(wèn)題妈踊,目前還只是基礎(chǔ),等到真正使用起來(lái)可能會(huì)有一些問(wèn)題泪漂,到時(shí)候再記錄下來(lái)吧廊营!
-to be continue-
學(xué)習(xí)筆記