必須的準備工作
一、MySQL的安裝。可以參考博文:http://blog.csdn.net/jueblog/article/details/9499245
二、下載 jdbc 驅動。可以從在官網上下載,或者點擊 http://download.csdn.net/detail/oyuntaolianwu/5822697 下載
三、在 Eclipse 的 Java 工程中新建一個 lib 文件夾,將所準備的 JAR 包複製進去。
四、右鍵該 JAR 包:Build Path --> Add to Build Path
連接的理論知識
需要用到 Java.sql.*; 中的幾個相關類:
Connection類
負責建立與指定URL(包含數據庫IP地址、庫名、用戶名和密碼的信息)的連接;
Connection conn = DriverManager.getConnection(url,user,password);
DriverManager.getConnection(url);
利用驅動管理器類獲取指定URL連接
[java] view plaincopy
String url = "jdbc:mysql://localhost:3306/test"; //連接URL爲 jdbc:mysql//服務器地址/數據庫名
Statement類
stmt = conn.createStatment();
語句對象,用來向數據庫發送一條SQL語句
rs = stmt.executeQuery(sql) //返回記錄集對象,用於查詢
int count = stmt.executeUpdate(sql) //執行一條增刪改語句,返回int
stmt.execute(sql) //增刪改都可以,返回布爾值(執行成功or失敗)
ResultSet類
記錄集對象,存儲executeQuery()方法返回的記錄集合。用相關的rs.getString("列名") rs.getInt("列名")等方法獲取指定列的值。
連接測試
Java代碼如下:
[java] view plaincopy
package com.sql;
import java.sql.*;
public class JDBC0726_Base {
Connection connection;
Statement statement;
ResultSet rSet;
//返回一個與特定數據庫的連接
public Connection getConnection() {
try {
//連接URL爲 jdbc:mysql//服務器地址/數據庫名 ,後面的2個參數分別是登陸用戶名和密碼
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "yongqiang");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) {
JDBC0726_Base jDao = new JDBC0726_Base();
System.out.println(jDao.getConnection());
}
}
如果輸出相應的對象地址,而不是異常,則證明連接成功。
如輸出:com.mysql.jdbc.JDBC4Connection@200bde
用 Java 對SQL進行相關操作
【注】以下操作需要依託於上面的 getConnection() 方法
非查詢類SQL語句
[java] view plaincopy
//非查詢類
public int Update(String sql) {
getConnection();
int count =0;
try {
statement = connection.createStatement();
count = statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
return count;
}
應用:
[java] view plaincopy
System.out.println(jDao.Update("INSERT INTO t_user(username,password,sex) values('hehe','131','n');"));
輸出值爲 1 則證明添加成功。
查詢類 SQL 並返回多條記錄
[java] view plaincopy
//執行一條查詢類SQL,返回多條記錄集
public void Qurty(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
while (rSet.next()) {
System.out.println(rSet.getRow()+ "----" + rSet.getString("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
應用舉例:
[java] view plaincopy
jDao.Qurty("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.Qurty("SELECT * FROM t_user;");
System.out.println("-----------------------------");
查詢類 SQL 並返回一條記錄
[java] view plaincopy
//執行一條查詢類SQL,返回單條記錄集
public void QurtyByUnique(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
System.out.println("id\t" + "realName\t"+"school\t");
if (rSet.next()) {
System.out.println(rSet.getInt("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
應用舉例:
[java] view plaincopy
jDao.QurtyByUnique("SELECT * FROM t_user WHERE sex='女';");
System.out.println("-----------------------------");
jDao.QurtyByUnique("SELECT * FROM t_user;");
System.out.println("-----------------------------");
輸出表單所有數據
[java] view plaincopy
public void QurtyTest(String sql) {
getConnection();
try {
statement = connection.createStatement();
rSet = statement.executeQuery(sql);
while (rSet.next()) {
System.out.print(rSet.getRow()+ "----" );
for (int i = 1; i < 14; i++) {
System.out.print(rSet.getString(i) +"\t");
}
System.out.println();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (statement != null) {
statement.close();
statement = null;
}
if (rSet != null) {
rSet.close();
rSet = null;
}
} catch (SQLException e2) {
// TODO: handle exception
}
}
}<span style="font-family:Arial,Helvetica,sans-serif"> </span>
MySQL與JDBC連接
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
PHP開發環境搭建與部署(wampserver+MySQL) PHP開發環境搭建與部署(wampserver+MySQL)
FremontBoy
2018-08-27 20:02:39
Mysql 5.6.30 insert null (ERROR 1048 (23000): Column 'ctime' cannot be null)
saife
2018-08-27 20:00:14
Can't read dir of '.' (errno: 13)
saife
2018-08-27 20:00:13
mysqldump: Got error: 1: Can't create/write to file '/data/export/test.txt' (Errcode: 13)
saife
2018-08-27 20:00:11
Ubuntu安裝inception錯誤處理make[2]: *** [sql/CMakeFiles/sql.dir/sql_yacc.cc.o] Error 1
saife
2018-08-27 20:00:11
MySQL鎖;Lock wait timeout exceeded; try restarting transaction
saife
2018-08-27 20:00:11
關於MySQL的order by與group by的組合使用
saife
2018-08-27 20:00:11
MySQL的基本操作
qq_875174369
2018-08-27 19:38:47
Ubuntu下使用PRM包安裝MySQL5.1.72
jimmylincole
2018-08-27 19:19:45
SQL GROUP BY
summerdream_
2018-08-27 19:19:08
MySQL 去除重複 Error Code:1093
summerdream_
2018-08-27 19:19:08
磨刀不誤砍柴工-Windows下MySQL日誌查看
auleit
2018-08-27 19:16:44