來(lái)源:Tanyboye 發(fā)布時(shí)間:2018-11-21 14:07:42 閱讀量:1038
JDBC全稱為:Java Data Base Connectivity (java數(shù)據(jù)庫(kù)連接),主要用于java與數(shù)據(jù)庫(kù)的鏈接。
整個(gè)鏈接過(guò)程如下圖:
1.數(shù)據(jù)庫(kù)驅(qū)動(dòng):Driver
加載mysql驅(qū)動(dòng):Class.forName("com.mysql.jdbc.Driver");
加載oracle驅(qū)動(dòng):Class.forName("oracle.jdbc.driver.OracleDriver");
加載相應(yīng)的驅(qū)動(dòng)需要導(dǎo)入相應(yīng)的包,如MySQL則需要導(dǎo)入:mysql-connector-java-5.1.13-bin.jar
否則無(wú)法正常執(zhí)行。
2.獲取數(shù)據(jù)庫(kù)鏈接:Connection
Connetion類主要用來(lái)鏈接數(shù)據(jù)庫(kù),常通過(guò)DriverManager.getConnection()來(lái)獲取一個(gè)連接對(duì)象。
這里有3個(gè)參數(shù),分別是url,user,passwrod。對(duì)應(yīng)的是要鏈接的數(shù)據(jù)庫(kù),用戶名,密碼等。如:
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
user=root
password=root
3.執(zhí)行sql語(yǔ)句:Statement
Statement對(duì)象用于執(zhí)行sql語(yǔ)句,有以下3種:
(1)Statement對(duì)象用于執(zhí)行不帶參數(shù)的簡(jiǎn)單的SQL語(yǔ)句;
(2)PerparedStatement對(duì)象用于執(zhí)行帶或不帶參數(shù)的預(yù)編譯SQL語(yǔ)句;
(3)CallableStatement對(duì)象用于執(zhí)行對(duì)數(shù)據(jù)庫(kù)已存儲(chǔ)過(guò)程的調(diào)用;
Statement的常用方法:
(1)executeQuery()方法:運(yùn)行查詢語(yǔ)句,返回ReaultSet對(duì)象。
(2)executeUpdata()方法:運(yùn)行增,刪,改操作,返回更新的行數(shù)。
(3)addBatch(String sql) :把多條sql語(yǔ)句放到一個(gè)批處理中。
(4)executeBatch():向數(shù)據(jù)庫(kù)發(fā)送一批sql語(yǔ)句執(zhí)行。
4.結(jié)果集:ResultSet
執(zhí)行executeQuery()方法后返回的結(jié)果集
常用方法:
(1)getString(String columnName):獲得當(dāng)前行的某一string類型的字段
(2)getFloat(String columnName):獲得當(dāng)前行的某一string類型的字段
(3)getDate(String columnName):獲得當(dāng)前行的某一date類型的字段
(4)getBoolean(String columnName):獲得在當(dāng)前行的某一Boolean類型的字段
(5)getObject(String columnName):獲取當(dāng)前行的某一任意類型的字段
(6)next():移動(dòng)到下一行
實(shí)際代碼
首先建立一個(gè)配置文件,內(nèi)容如下:
#數(shù)據(jù)庫(kù)驅(qū)動(dòng)
driver=com.mysql.jdbc.Driver
#連接數(shù)據(jù)庫(kù)的URL
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
#用戶名
user=root
#密碼
password=root
接著寫(xiě)一個(gè)連接數(shù)據(jù)庫(kù)的通用工具類:
public class DBUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
static{
try {
//讀取配置文件
InputStream in = DBUtil.class.getResourceAsStream("db.properties");
Properties properties = new Properties();
//加載配置文件
properties.load(in);
//獲取配置文件中的數(shù)據(jù)
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
//加載數(shù)據(jù)庫(kù)鏈接驅(qū)動(dòng)
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取一個(gè)數(shù)據(jù)庫(kù)鏈接
*/
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
}
實(shí)現(xiàn)簡(jiǎn)單的增刪改查:
public class jdbcTest {
/**
* 創(chuàng)建表
*/
public static void createTable() throws SQLException{
String sql = "CREATE TABLE IF NOT EXISTS `user`("
+"`id` INT UNSIGNED AUTO_INCREMENT,"
+" `user_name` VARCHAR(100),"
+" `user_password` VARCHAR(100),"
+" `user_age` INT(11),"
+"PRIMARY KEY ( `id` )"
+")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
Connection conn = DBUtil.getConnection();
//開(kāi)啟事務(wù)
conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
//提交事務(wù)
conn.commit();
//要注意關(guān)閉連接(在實(shí)際中最好不要像這樣關(guān)閉,最好標(biāo)準(zhǔn)一點(diǎn))
conn.close();
}
/**
* 增加數(shù)據(jù)
*/
public static void add() throws SQLException{
String sql = "INSERT INTO USER (user_name,user_password,user_age) VALUES('老王','123456',18)";
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
conn.commit();
conn.close();
}
/**
* 刪除數(shù)據(jù)
*/
public static void delete() throws SQLException{
String sql = "DELETE FROM USER WHERE USER.user_name = '老王'";
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
conn.commit();
conn.close();
}
/**
* 修改數(shù)據(jù)
*/
public static void updata() throws SQLException{
String sql = "UPDATE USER SET USER.user_name = '老李'";
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
conn.commit();
conn.close();
}
/**
* 查找數(shù)據(jù)
*/
public static void query() throws SQLException{
String sql = "SELECT * FROM USER";
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
//執(zhí)行查詢語(yǔ)句并返回結(jié)果集
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
//注意:這里要與數(shù)據(jù)庫(kù)里的字段對(duì)應(yīng)
String username = resultSet.getString("user_name");
String password = resultSet.getString("user_password");
String age = resultSet.getString("user_age");
System.out.println(username + " " + password + " " + age);
}
conn.commit();
conn.close();
}
在線
客服
服務(wù)時(shí)間:周一至周日 08:30-18:00
選擇下列產(chǎn)品馬上在線溝通:
客服
熱線
7*24小時(shí)客服服務(wù)熱線
關(guān)注
微信
關(guān)注官方微信