使用Java操作MySQL實現(xiàn)數(shù)據(jù)交互的方法
JDBC的認識:
JDBC,即Java Database Connectivity,java數(shù)據(jù)庫連接。是一種用于執(zhí)行SQL語句的Java API,它是Java中的數(shù)據(jù)庫連接規(guī)范。它為Java開發(fā)人員操作數(shù)據(jù)庫提供了一個標準的API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問;
由于每個廠商的數(shù)據(jù)庫的API都不同,為了進行統(tǒng)一管理,Java提供了一套標準的API接口標準,每個數(shù)據(jù)庫都得安裝這套標準才能在Java中使用,這套標準就是JDBC;
JDBC的作用可以在Java中對數(shù)據(jù)庫進行增刪改查操作;
安裝MySQL驅(qū)動包:
只要安裝了JDK就自帶了JDBC,JDBC是Java標準庫中提供的,但是Java要想操作數(shù)據(jù)庫,就得下載它的數(shù)據(jù)庫驅(qū)動包,得適配到Java中
中央倉庫:https://mvnrepository.com/
中央倉庫:有大佬將Java中日常開發(fā)中可能會使用到的第三方庫,安裝包統(tǒng)一收集在一個網(wǎng)站中,這個網(wǎng)站就叫做“中央倉庫”;
因為我使用的數(shù)據(jù)庫是5.7版本的,屬于舊版本,下載舊版本的驅(qū)動包即可;
隨便哪個版本都行,只需前面的大版本對應你的MySQL版本就行
下載jar文件
下載完成后將包導入IDEA
1)在項目中創(chuàng)建一個新目錄
2)將jar包復制粘貼到lib包中
3)右擊.jar文件 選擇 點擊OK
4)添加完成
實現(xiàn)代碼:
1.創(chuàng)建數(shù)據(jù)源
//1.創(chuàng)建數(shù)據(jù)源 DataSource dataSource = new MysqlDataSource(); ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf8&useSSL=false"); ((MysqlDataSource)dataSource).setUser("root"); ((MysqlDataSource)dataSource).setPassword("123456");
這里設(shè)置了賬號密碼使用的有連接方式
客戶端-服務(wù)器之間的通信有倆種:
有連接:這里JDBC使用的就是這種,需要雙方同意才能進行操作。
例如:打電話,A打B的電話,B必須接收才能相互通信
無連接:類似發(fā)短信,不需要對方同意,也可以直接發(fā)送
2.和數(shù)據(jù)庫服務(wù)器建立連接
//2.和數(shù)據(jù)庫服務(wù)器建立連接 Connection connection = dataSource.getConnection();
這塊不要導錯包,否則會出現(xiàn)錯誤;
在getConnection()這個方法的可能會連接失敗 有以下幾點可能:
1.數(shù)據(jù)庫服務(wù)器沒有正確啟動
2.url寫錯了
3.用戶名寫錯了
4.密碼填錯了
5.網(wǎng)絡(luò)斷開.....
3.構(gòu)造操作數(shù)據(jù)庫的 sql 語句.
//3.構(gòu)造操作數(shù)據(jù)庫語句 System.out.println("請輸入學號: "); int id = scanner.nextInt(); System.out.println("請輸入姓名: "); String name = scanner.next(); String sql = "insert into student values(?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, id); preparedStatement.setString(2, name);
prepareStatement(sql)
方法將 SQL 語句傳遞給數(shù)據(jù)庫,準備執(zhí)行。PreparedStatement
提供了一種預編譯的方式,確保 SQL 語句能更高效地執(zhí)行,并且可以有效防止 SQL 注入。
在sql語句中使用到?這種占位符
preparedStatement:
可以設(shè)置sql語句對應的占位符位置,并且下標是從1開始
4.執(zhí)行SQL語句
//4.執(zhí)行SQL語句 int n = preparedStatement.executeUpdate(); System.out.println("n = "+n);
有倆種方法:
- executeQuery() 方法執(zhí)行后返回單個結(jié)果集的,通常用于select語句
- executeUpdate()方法返回值是一個整數(shù),指示受影響的行數(shù),通常用于update、insert、delete語句
5.釋放資源
//5.釋放資源 preparedStatement.close(); connection.close();
對應前面創(chuàng)建的資源都得手動釋放掉,以免不必要的浪費
完整代碼:
Insert:
public class Demo { public static void main(String[] args) throws SQLException { Scanner scanner = new Scanner(System.in); //1.創(chuàng)建數(shù)據(jù)源 DataSource dataSource = new MysqlDataSource(); ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf8&useSSL=false"); ((MysqlDataSource)dataSource).setUser("root"); ((MysqlDataSource)dataSource).setPassword("123456"); //2.和數(shù)據(jù)庫服務(wù)器建立連接 Connection connection = dataSource.getConnection(); //3.構(gòu)造操作數(shù)據(jù)庫語句 System.out.println("請輸入學號: "); int id = scanner.nextInt(); System.out.println("請輸入姓名: "); String name = scanner.next(); String sql = "insert into student values(?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, id); preparedStatement.setString(2, name); //4.執(zhí)行SQL語句 int n = preparedStatement.executeUpdate(); System.out.println("n = "+n); //5.釋放資源 preparedStatement.close(); connection.close(); } }
Select:
public static void main(String[] args) throws SQLException { Scanner scanner = new Scanner(System.in); //1.創(chuàng)建數(shù)據(jù)源 DataSource dataSource = new MysqlDataSource(); ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf8&useSSL=false"); ((MysqlDataSource)dataSource).setUser("root"); ((MysqlDataSource)dataSource).setPassword("123456"); //2.和數(shù)據(jù)庫服務(wù)器建立連接 Connection connection = dataSource.getConnection(); //3.構(gòu)造操作數(shù)據(jù)庫語句 String sql = "select * from student"; PreparedStatement preparedStatement = connection.prepareStatement(sql); //4.執(zhí)行語句 ResultSet resultSet = preparedStatement.executeQuery(); //遍歷結(jié)果 while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println("id = "+id +", name = " + name); } //5.資源釋放 resultSet.close(); preparedStatement.close();; connection.close(); }
ResultSet對象它被稱為結(jié)果集,它代表符合SQL語句條件的所有行,并且它通過一套getXXX方法提供了對這些行中數(shù)據(jù)的訪問。
ResultSet里的數(shù)據(jù)一行一行排列,每行有多個字段,并且有一個記錄指針,指針所指的數(shù)據(jù)行叫做當前數(shù)據(jù)行,我們只能來操作當前的數(shù)據(jù)行。我們?nèi)绻胍〉媚骋粭l記錄,就要使用ResultSet的next()方法 ,如果我們想要得到ResultSet里的所有記錄,就應該使用while循環(huán)。
Update:
public static void main(String[] args) throws SQLException { Scanner scanner = new Scanner(System.in); //1.建立數(shù)據(jù)源 DataSource dataSource = new MysqlDataSource(); ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf8&useSSL=false"); ((MysqlDataSource)dataSource).setUser("root"); ((MysqlDataSource)dataSource).setPassword("040518"); //2.建立連接 Connection connection = dataSource.getConnection(); //3.構(gòu)建sql String sql = "update student set name = 'wangwu' where id = 2"; PreparedStatement preparedStatement = connection.prepareStatement(sql); //4.執(zhí)行sql int n = preparedStatement.executeUpdate(); System.out.println("n = " + n); //5.釋放資源 preparedStatement.close(); connection.close(); }
到此這篇關(guān)于使用Java操作MySQL實現(xiàn)數(shù)據(jù)交互的文章就介紹到這了,更多相關(guān)java mysql數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決maven父子工程install的時候排除某些子模塊,讓子模塊不install問題
在Maven父子工程中,如果希望某個子模塊不被安裝到本地倉庫,可以在該子模塊的`pom.xml`文件中添加以下配置: ```xml ... org.apache.maven.plugins maven-install-plugin 2.5.2 true2024-12-12Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(共享模式)
這篇文章主要為大家詳細介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式
這篇文章主要介紹了SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02