MySQL詳解進(jìn)行JDBC編程與增刪改查方法
Java的數(shù)據(jù)庫(kù)編程JDBC
概念
- JDBC是一種用于執(zhí)行sql語(yǔ)句的Java API,他是java中的數(shù)據(jù)庫(kù)連接規(guī)范,這個(gè)API由一些接口和類(lèi)組成。它為java開(kāi)發(fā)人員操作數(shù)據(jù)庫(kù)提供了一個(gè)標(biāo)準(zhǔn)的API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問(wèn)
- 本質(zhì)是通過(guò)代碼自己實(shí)現(xiàn)一個(gè)MySQL客戶端,通過(guò)網(wǎng)絡(luò)和服務(wù)器進(jìn)行數(shù)據(jù)的交互,客戶端不能憑空出現(xiàn),所以數(shù)據(jù)庫(kù)提供了一組API方便我們實(shí)現(xiàn)
- 數(shù)據(jù)庫(kù)的種類(lèi)有很多,不同的數(shù)據(jù)庫(kù)提供的API不太一樣,所以java為了解決這一問(wèn)題提供了JDBC,java自帶的一種數(shù)據(jù)庫(kù)操作API,這種API覆蓋所有數(shù)據(jù)庫(kù)操作的操作方式
- 本質(zhì)上是java自身完成了JDBC API和數(shù)據(jù)庫(kù)API之間進(jìn)行轉(zhuǎn)換

使用步驟
創(chuàng)建DataSource對(duì)象,這個(gè)對(duì)象就描述了數(shù)據(jù)庫(kù)服務(wù)器在哪
DataSource dataSource = new MysqlDataSource();
//設(shè)置數(shù)據(jù)庫(kù)所在的地址
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/lmp?characterEncoding=utf8&useSSL=false");
//設(shè)置登錄數(shù)據(jù)庫(kù)的用戶名
((MysqlDataSource)dataSource).setUser("root");
//設(shè)置登錄數(shù)據(jù)庫(kù)的密碼
((MysqlDataSource)dataSource).setPassword("woshizhu123");
通過(guò)Connection連接數(shù)據(jù)庫(kù)(輸入密碼連接成功)
//import java.sql.Connection; Connection connection = dataSource.getConnection();
拼接sql語(yǔ)句(寫(xiě)入sql語(yǔ)句)
String sql = "insert into student values(1,'張三')";
將sql語(yǔ)句包裝成對(duì)象
PreparedStatement statement = connection.prepareStatement(sql);
執(zhí)行sql語(yǔ)句(按下回車(chē)執(zhí)行sql語(yǔ)句)
int ret = statement.executeUpdate();
- 執(zhí)行 update delete insert 使用 executeUpdate() 方法
- 執(zhí)行 select 使用 executeQuery() 方法
- 使用 executeQuery() 方法 會(huì)返回一個(gè)resultSet集合, 包含查找到的數(shù)據(jù), 初始情況下resultSet不指向任一行記錄, 使用next,讓他指向第一條記錄, 再使用next指向下一條記錄
釋放資源
statement.close(); connection.close();
利用JDBC實(shí)現(xiàn)增加(insert)
public class TestJDBC {
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf-8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("woshizhu123");
Connection connection = dataSource.getConnection();
System.out.println("輸入id");
int id = scanner.nextInt();
System.out.println("輸入名字");
String name = scanner.next();
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.setString(2,name);
int ret = statement.executeUpdate();
if(ret == 1){
System.out.println("插入成功");
}else {
System.out.println("插入失敗");
}
statement.close();
connection.close();
}
}
利用JDBC實(shí)現(xiàn)刪除(delete)
public class TestJDBCDelete
{
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("woshizhu123");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入要?jiǎng)h除的id");
int id = scanner.nextInt();
String sql = "delete from student where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
int ret = preparedStatement.executeUpdate();
System.out.println(ret);
preparedStatement.close();
connection.close();
}利用JDBC實(shí)現(xiàn)修改(update)
public class TestJDBCUpdate {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("woshizhu123");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入要修改的學(xué)生id");
int id = scanner.nextInt();
System.out.println("請(qǐng)輸入要修改的學(xué)生姓名");
String name = scanner.next();
String sql = "update student set name = ? where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
int ret = statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
利用JDBC實(shí)現(xiàn)查找(select)
public static void testJDBCSelect() throws SQLException {
//1創(chuàng)建DataSource對(duì)象
DataSource dataSource = new MysqlDataSource();
//2連接數(shù)據(jù)庫(kù)
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("listen");
Connection connection = dataSource.getConnection();
//3拼接sql
String sql = "select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
//4執(zhí)行sql
ResultSet resultSet = statement.executeQuery();
//5遍歷得到的集合
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int classId = resultSet.getInt("classId");
System.out.println("id " + id + " name " + name + " classId " + classId);
}
//6關(guān)閉資源
resultSet.close();
statement.close();
connection.close();
}到此這篇關(guān)于MySQL詳解進(jìn)行JDBC編程與增刪改查方法的文章就介紹到這了,更多相關(guān)MySQL JDBC編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WIN10下cmd如何查看編碼方式,命令行窗口修改UTF-8編碼
這篇文章主要介紹了WIN10下cmd如何查看編碼方式,命令行窗口修改UTF-8編碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
mysql regexp匹配多個(gè)字符串實(shí)現(xiàn)
本文主要介紹了mysql regexp匹配多個(gè)字符串實(shí)現(xiàn),可以利用REGEXP正則表達(dá)式匹配多個(gè)字符串,從而實(shí)現(xiàn)高效查詢,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符處理詳解
本文是對(duì)MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
MySQL事務(wù)的ACID特性以及并發(fā)問(wèn)題方案
這篇文章主要介紹了MySQL事務(wù)的ACID特性以及并發(fā)問(wèn)題方案,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07

