最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL詳解進(jìn)行JDBC編程與增刪改查方法

 更新時(shí)間:2022年06月15日 11:46:16   作者:亞太地區(qū)百大最帥面孔第101名  
JDBC是指Java數(shù)據(jù)庫(kù)連接,是一種標(biāo)準(zhǔn)Java應(yīng)用編程接口( JAVA API),用來(lái)連接 Java 編程語(yǔ)言和廣泛的數(shù)據(jù)庫(kù)。從根本上來(lái)說(shuō),JDBC 是一種規(guī)范,它提供了一套完整的接口,允許便攜式訪(fǎng)問(wèn)到底層數(shù)據(jù)庫(kù)

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)一訪(fǎng)問(wèn)
  • 本質(zhì)是通過(guò)代碼自己實(shí)現(xiàn)一個(gè)MySQL客戶(hù)端,通過(guò)網(wǎng)絡(luò)和服務(wù)器進(jìn)行數(shù)據(jù)的交互,客戶(hù)端不能憑空出現(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ù)的用戶(hù)名
        ((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)文章

  • MySQL外鍵創(chuàng)建失敗1005原因匯總

    MySQL外鍵創(chuàng)建失敗1005原因匯總

    MySQL外鍵創(chuàng)建失敗1005原因有很多,本文整理了一些,希望對(duì)大家有所幫助
    2014-01-01
  • MySQL主從延遲現(xiàn)象及原理分析詳解

    MySQL主從延遲現(xiàn)象及原理分析詳解

    今天小編就為大家分享一篇關(guān)于MySQL主從延遲現(xiàn)象及原理分析詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • WIN10下cmd如何查看編碼方式,命令行窗口修改UTF-8編碼

    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)

    本文主要介紹了mysql regexp匹配多個(gè)字符串實(shí)現(xiàn),可以利用REGEXP正則表達(dá)式匹配多個(gè)字符串,從而實(shí)現(xiàn)高效查詢(xún),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • 如何快速修改MySQL用戶(hù)的host屬性

    如何快速修改MySQL用戶(hù)的host屬性

    這篇文章主要介紹了修改MySQL用戶(hù)的host屬性操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • mysql創(chuàng)建刪除表的實(shí)例詳解

    mysql創(chuàng)建刪除表的實(shí)例詳解

    這篇文章主要介紹了mysql創(chuàng)建刪除表的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-10-10
  • MySQL雙主(主主)架構(gòu)配置方案

    MySQL雙主(主主)架構(gòu)配置方案

    這篇文章主要介紹了MySQL雙主(主主)架構(gòu)配置方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符處理詳解

    MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符處理詳解

    本文是對(duì)MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-11-11
  • MySQL阻塞與死鎖的解決

    MySQL阻塞與死鎖的解決

    本文主要介紹了MySQL阻塞與死鎖的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • MySQL事務(wù)的ACID特性以及并發(fā)問(wèn)題方案

    MySQL事務(wù)的ACID特性以及并發(fā)問(wèn)題方案

    這篇文章主要介紹了MySQL事務(wù)的ACID特性以及并發(fā)問(wèn)題方案,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07

最新評(píng)論

屏南县| 福海县| 获嘉县| 津市市| 柞水县| 陆川县| 盈江县| 长兴县| 新蔡县| 朝阳县| 通渭县| 临高县| 扬州市| 拜城县| 大丰市| 合作市| 四会市| 麻江县| 池州市| 平舆县| 资中县| 香格里拉县| 宁陕县| 格尔木市| 大冶市| 望城县| 惠水县| 大城县| 剑川县| 平定县| 鄂托克前旗| 临邑县| 喀喇| 玉门市| 东乌珠穆沁旗| 武宣县| 武山县| 兴业县| 安乡县| 乌拉特中旗| 平武县|