Java對MySQL數(shù)據(jù)庫進行連接、查詢和修改操作方法
0. 一般過程:
(1) 調用Class.forName()方法加載驅動程序。
(2) 調用DriverManager對象的getConnection()方法,獲得一個Connection對象。
(3) 創(chuàng)建一個Statement對象,準備一個SQL語句,這個SQL語句可以是Statement對象(立即執(zhí)行的的語句)、PreparedStatement語句(預編譯的語句)或CallableStatement對象(存儲過程調用的語句)。
(4) 調用excuteQuery()等方法執(zhí)行SQL語句,并將結果保存在ResultSet對象;或者調用executeUpdate()等方法執(zhí)行SQL語句,不返回ResultSet對象的結果。
(5)對返回的ResultSet對象進行顯示等相當?shù)奶幚怼?/p>
(6)釋放資源。
1. 連接數(shù)據(jù)庫
(1) 下載Mysql連接驅動
網(wǎng)址: http://dev.mysql.com/downloads/connector/j/ ,下載后放在F:\博士科研資料\數(shù)據(jù)庫學習\mysql相關程序文件中,解壓。
(2) 加載JDBC驅動
操作方法:在Eclipse中,選中相應的工程,點擊Project-Properties中的Java Build Path,在Libraries中增加mysql-connector-java-5.1.21-bin.jar,點OK。
(3) 建一個簡單的數(shù)據(jù)庫如下:
import java.sql.*; public class GetConnection { public static void main(String[] args){ try{ //調用Class.forName()方法加載驅動程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加載MySQL驅動!"); }catch(ClassNotFoundException e1){ System.out.println("找不到MySQL驅動!"); e1.printStackTrace(); } String url="jdbc:mysql://localhost:3306/mysql"; //JDBC的URL //調用DriverManager對象的getConnection()方法,獲得一個Connection對象 Connection conn; try { conn = DriverManager.getConnection(url, "root",""); //創(chuàng)建一個Statement對象 Statement stmt = conn.createStatement(); //創(chuàng)建Statement對象 System.out.print("成功連接到數(shù)據(jù)庫!"); stmt.close(); conn.close(); } catch (SQLException e){ e.printStackTrace(); } } }
2. 查詢數(shù)據(jù)表
在詢數(shù)據(jù)表時,需要用到ResultSet接口,它類似于一個數(shù)據(jù)表,通過該接口的實例可以獲得檢索結果集,以及對應數(shù)據(jù)表的接口信息。
import java.sql.*; public class SelectTable { public static void main(String[] args){ try{ //調用Class.forName()方法加載驅動程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加載MySQL驅動!"); String url="jdbc:mysql://localhost:3306/aniu"; //JDBC的URL Connection conn; conn = DriverManager.getConnection(url, "root",""); Statement stmt = conn.createStatement(); //創(chuàng)建Statement對象 System.out.println("成功連接到數(shù)據(jù)庫!"); String sql = "select * from stu"; //要執(zhí)行的SQL ResultSet rs = stmt.executeQuery(sql);//創(chuàng)建數(shù)據(jù)對象 System.out.println("編號"+"\t"+"姓名"+"\t"+"年齡"); while (rs.next()){ System.out.print(rs.getInt(1) + "\t"); System.out.print(rs.getString(2) + "\t"); System.out.print(rs.getInt(3) + "\t"); System.out.println(); } rs.close(); stmt.close(); conn.close(); }catch(Exception e) { e.printStackTrace(); } } }
3. 修改和刪除數(shù)據(jù)庫
//修改刪除數(shù)據(jù) import java.sql.*; public class UpdateDeleteDemo { public static void main(String[] args)throws Exception{ try{ //調用Class.forName()方法加載驅動程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加載MySQL驅動!"); String url="jdbc:mysql://localhost:3306/aniu"; //JDBC的URL Connection conn; conn = DriverManager.getConnection(url, "root",""); Statement stmt = conn.createStatement(); //創(chuàng)建Statement對象 System.out.println("成功連接到數(shù)據(jù)庫!"); //查詢數(shù)據(jù)的代碼 String sql = "select * from stu"; //要執(zhí)行的SQL ResultSet rs = stmt.executeQuery(sql);//創(chuàng)建數(shù)據(jù)對象 System.out.println("編號"+"\t"+"姓名"+"\t"+"年齡"); while (rs.next()){ System.out.print(rs.getInt(1) + "\t"); System.out.print(rs.getString(2) + "\t"); System.out.print(rs.getInt(3) + "\t"); System.out.println(); } //修改數(shù)據(jù)的代碼 String sql2 = "update stu set name=? where number=?"; PreparedStatement pst = conn.prepareStatement(sql2); pst.setString(1,"8888"); pst.setInt(2,198); pst.executeUpdate(); //刪除數(shù)據(jù)的代碼 String sql3 = "delete from stu where number=?"; pst = conn.prepareStatement(sql3); pst.setInt(1,701); pst.executeUpdate(); ResultSet rs2 = stmt.executeQuery(sql);//創(chuàng)建數(shù)據(jù)對象 System.out.println("編號"+"\t"+"姓名"+"\t"+"年齡"); while (rs.next()){ System.out.print(rs2.getInt(1) + "\t"); System.out.print(rs2.getString(2) + "\t"); System.out.print(rs2.getInt(3) + "\t"); System.out.println(); } rs.close(); stmt.close(); conn.close(); }catch(Exception e) { e.printStackTrace(); } } }
以上所述是小編給大家介紹的Java對MySQL數(shù)據(jù)庫進行連接、查詢和修改操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 使用Java實現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫
- Java mysql數(shù)據(jù)庫并進行內(nèi)容查詢實例代碼
- 在Java的Hibernate框架中對數(shù)據(jù)庫數(shù)據(jù)進行查詢操作
- 使用Java對數(shù)據(jù)庫進行基本的查詢和更新操作
- java 使用ElasticSearch完成百萬級數(shù)據(jù)查詢附近的人功能
- java實現(xiàn)的連接數(shù)據(jù)庫及模糊查詢功能示例
- Java連接mysql數(shù)據(jù)庫并進行內(nèi)容查詢的方法
- java查詢近七日數(shù)據(jù)功能的實現(xiàn)
相關文章
spring中websocket定時任務實現(xiàn)實時推送
本文主要介紹了spring中websocket定時任務實現(xiàn)實時推送,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01Spring @Primary作用和實現(xiàn)原理詳解
今天分享一下Spring中的@Primary注解,Primary的意思是主要的,我們在使用spring的時候,難免會定義多個類型相同的bean,這時候如果不采取一些方法,那么是無法正常使用bean的,所以本就給大家介紹Spring @Primary的作用和實現(xiàn)原理2023-07-07Java中l(wèi)ist根據(jù)id獲取對象的幾種方式
這篇文章主要給大家介紹了關于Java中l(wèi)ist根據(jù)id獲取對象的幾種方式,文中通過實例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07SpringBoot自定義注解實現(xiàn)Token校驗的方法
這篇文章主要介紹了SpringBoot自定義注解實現(xiàn)Token校驗的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03