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

使用步驟
創(chuàng)建DataSource對象,這個對象就描述了數(shù)據(jù)庫服務(wù)器在哪
DataSource dataSource = new MysqlDataSource();
//設(shè)置數(shù)據(jù)庫所在的地址
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/lmp?characterEncoding=utf8&useSSL=false");
//設(shè)置登錄數(shù)據(jù)庫的用戶名
((MysqlDataSource)dataSource).setUser("root");
//設(shè)置登錄數(shù)據(jù)庫的密碼
((MysqlDataSource)dataSource).setPassword("woshizhu123");
通過Connection連接數(shù)據(jù)庫(輸入密碼連接成功)
//import java.sql.Connection; Connection connection = dataSource.getConnection();
拼接sql語句(寫入sql語句)
String sql = "insert into student values(1,'張三')";
將sql語句包裝成對象
PreparedStatement statement = connection.prepareStatement(sql);
執(zhí)行sql語句(按下回車執(zhí)行sql語句)
int ret = statement.executeUpdate();
- 執(zhí)行 update delete insert 使用 executeUpdate() 方法
- 執(zhí)行 select 使用 executeQuery() 方法
- 使用 executeQuery() 方法 會返回一個resultSet集合, 包含查找到的數(shù)據(jù), 初始情況下resultSet不指向任一行記錄, 使用next,讓他指向第一條記錄, 再使用next指向下一條記錄
釋放資源
statement.close(); connection.close();
利用JDBC實現(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實現(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("請輸入要刪除的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實現(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("請輸入要修改的學生id");
int id = scanner.nextInt();
System.out.println("請輸入要修改的學生姓名");
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實現(xiàn)查找(select)
public static void testJDBCSelect() throws SQLException {
//1創(chuàng)建DataSource對象
DataSource dataSource = new MysqlDataSource();
//2連接數(shù)據(jù)庫
((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詳解進行JDBC編程與增刪改查方法的文章就介紹到這了,更多相關(guān)MySQL JDBC編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL8.0.28安裝教程詳細圖解(windows?64位)
如果電腦上已經(jīng)有MySQL數(shù)據(jù)庫再進行重做往往會遇到問題,下面這篇文章主要給大家介紹了關(guān)于windows?64位系統(tǒng)下MySQL8.0.28安裝教程的詳細教程,文章通過圖文介紹的非常詳細,需要的朋友可以參考下2023-04-04
MYSQL METADATA LOCK(MDL LOCK) 理論及加鎖類型測試
這篇文章主要介紹了MYSQL METADATA LOCK(MDL LOCK)的內(nèi)容,有理論知識和加鎖類型測試的以下代碼,感興趣的朋友請參考下午文2021-09-09
MySQL數(shù)據(jù)庫中數(shù)值字段類型長度int(11)和Decimal(M,D)詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫中數(shù)值字段類型長度int(11)和Decimal(M,D)字段詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
安裝使用Percona XtraBackup來備份恢復MySQL的教程
這篇文章主要介紹了安裝使用Percona XtraBackup來備份恢復MySQL的教程,文中的示例環(huán)境基于CentOS系統(tǒng),需要的朋友可以參考下2015-12-12

