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

JDBC實(shí)現(xiàn)數(shù)據(jù)庫增刪改查功能

 更新時(shí)間:2021年07月04日 14:23:40   作者:跑起來要帶風(fēng)!  
這篇文章主要為大家詳細(xì)介紹了JDBC實(shí)現(xiàn)數(shù)據(jù)庫增刪改查功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

JDBC,簡(jiǎn)單點(diǎn)來說,就是用Java操作數(shù)據(jù)庫,下面簡(jiǎn)單介紹怎么實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查功能。

1、添加數(shù)據(jù)

package cn.itcast.jdbc;

import java.sql.*;

public class JdbcDemo2 {

    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            //1、注冊(cè)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            //2、定義sql
            String sql = "insert into course values(?,?,?)";
            //3、獲取Connection對(duì)象
            //student表示你要操作的數(shù)據(jù)庫
            //如果是locakhost:3306,也可以簡(jiǎn)寫為"jdbc:mysql:///student"
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
            //4、獲取執(zhí)行sql的對(duì)象
            preparedStatement = connection.prepareStatement(sql);
            //傳入?yún)?shù)
            preparedStatement.setInt(1,5);
            preparedStatement.setString(2,"JavaWeb");
            preparedStatement.setInt(3,88);
            //5、執(zhí)行sql
            int count = preparedStatement.executeUpdate();
            //6、處理結(jié)果
            System.out.println(count);
            if (count > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失敗");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7、釋放資源
            //避免空指針異常
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2、刪除數(shù)據(jù)

package cn.itcast.jdbc;

import java.sql.*;

public class JdbcDemo4 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1、注冊(cè)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");

            //2、獲取連接對(duì)象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");

            //3、定義sql
            String sql = "delete from course where cno = ?";

            //4、獲取執(zhí)行sql對(duì)象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,5);

            //5、執(zhí)行sql
            int count = preparedStatement.executeUpdate();

            //6、處理結(jié)果
            System.out.println(count);
            if (count > 0) {
                System.out.println("刪除成功");
            } else {
                System.out.println("刪除失敗");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7、釋放資源
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

3、修改數(shù)據(jù)

package cn.itcast.jdbc;

import java.sql.*;

public class JdbcDemo3 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1、注冊(cè)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");

            //2、獲取連接對(duì)象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");

            //3、定義sql
            String sql = "update course set period = ? where cno = ?";

            //4、獲取執(zhí)行sql對(duì)象
            preparedStatement = connection.prepareStatement(sql);
            //設(shè)置參數(shù)
            preparedStatement.setInt(1,90);
            preparedStatement.setInt(2,1);


            //5、執(zhí)行sql
            int count = preparedStatement.executeUpdate();

            //6、處理結(jié)果
            System.out.println(count);
            if (count > 0) {
                System.out.println("修改成功!");
            } else {
                System.out.println("修改失敗!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7、釋放資源
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

4、查詢數(shù)據(jù)

package cn.itcast.jdbc;

import cn.itcast.domain.Course;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo5 {

    /**
     * 查詢所有Course對(duì)象
     * @return
     */
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Course> list = null;
        try {
            //1、注冊(cè)驅(qū)動(dòng)
            Class.forName("com.mysql.jdbc.Driver");
            //2、獲取連接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");

            //3、定義sql
            String sql = "select * from course";
            //4、獲取執(zhí)行sql的對(duì)象
            preparedStatement = connection.prepareStatement(sql);
            //5、執(zhí)行sql
            resultSet = preparedStatement.executeQuery();
            //6、遍歷結(jié)果集,封裝對(duì)象,裝載集合
            Course course = null;
            list = new ArrayList<Course>();
            while (resultSet.next()) {
                //獲取數(shù)據(jù)
                int cno = resultSet.getInt("cno");
                String cname = resultSet.getString("cname");
                int period = resultSet.getInt("period");
                //創(chuàng)建Course對(duì)象并賦值
                course = new Course();
                course.setCno(cno);
                course.setCname(cname);
                course.setPeriod(period);
                //裝載集合
                list.add(course);

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(list);
    }

}

我們可以發(fā)現(xiàn),增刪改的操作基本都是差不多的語句,且執(zhí)行sql的語句都是一樣的,都是preparedStatement.executeUpdate()。但查詢操作就有所不同了,返回的是一個(gè)結(jié)果集,且執(zhí)行sql的語句就是preparedStatement.executeQuery()。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot自定義注解如何解決公共字段填充問題

    SpringBoot自定義注解如何解決公共字段填充問題

    本文介紹了在系統(tǒng)開發(fā)中,如何使用AOP切面編程實(shí)現(xiàn)公共字段自動(dòng)填充的功能,從而簡(jiǎn)化代碼,通過自定義注解和切面類,可以統(tǒng)一處理創(chuàng)建時(shí)間和修改時(shí)間,以及創(chuàng)建人和修改人的賦值操作
    2025-03-03
  • 詳解SpringBoot中時(shí)間類型的序列化與反序列化

    詳解SpringBoot中時(shí)間類型的序列化與反序列化

    這篇文章主要為大家詳細(xì)介紹了SpringBoot中時(shí)間類型的序列化與反序列化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-02-02
  • SPFA 算法實(shí)例講解

    SPFA 算法實(shí)例講解

    下面小編就為大家?guī)硪黄猄PFA 算法實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • Springboot?jpa使用sum()函數(shù)返回結(jié)果如何被接收

    Springboot?jpa使用sum()函數(shù)返回結(jié)果如何被接收

    這篇文章主要介紹了Springboot?jpa使用sum()函數(shù)返回結(jié)果如何接收,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • MyBatis批量插入/修改/刪除MySql數(shù)據(jù)

    MyBatis批量插入/修改/刪除MySql數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于MyBatis批量插入/修改/刪除MySql數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • springboot3.x版本集成log4j遇到Logging?system?failed?to?initialize?using?configuration?from‘classpath:log4問題

    springboot3.x版本集成log4j遇到Logging?system?failed?to?initial

    使用Springboot?3.x集成Log4j時(shí)可能會(huì)遇到版本沖突的問題,這通常可以通過檢查Maven依賴樹來識(shí)別,一旦發(fā)現(xiàn)沖突,將Log4j的版本統(tǒng)一更新到最新的兼容版本,例如2.21.1,即可解決問題,此方法有效解決了日志打印錯(cuò)誤,是處理類似問題的一個(gè)實(shí)用參考
    2024-09-09
  • Java 正則表達(dá)式功能及應(yīng)用

    Java 正則表達(dá)式功能及應(yīng)用

    自從jdk1.4推出java.util.regex包,就為我們提供了很好的Java正則表達(dá)式應(yīng)用平臺(tái),因?yàn)镴ava正則表達(dá)式是一個(gè)很龐雜的體系。
    2010-03-03
  • Java實(shí)現(xiàn)紅黑樹(平衡二叉樹)的詳細(xì)過程

    Java實(shí)現(xiàn)紅黑樹(平衡二叉樹)的詳細(xì)過程

    紅黑樹接近平衡的二叉樹,插入,刪除函數(shù)跟平衡二叉樹一樣,只是平衡函數(shù)不同,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)紅黑樹(平衡二叉樹)的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • Hadoop組件簡(jiǎn)介

    Hadoop組件簡(jiǎn)介

    Hadoop作為一種分布式基礎(chǔ)架構(gòu),可以使用戶在不了解分布式底層細(xì)節(jié)的情況下,開發(fā)分布式程序。接下來通過本文給大家分享Hadoop組件簡(jiǎn)介,感興趣的朋友一起看看吧
    2017-09-09
  • java 利用java反射機(jī)制動(dòng)態(tài)加載類的簡(jiǎn)單實(shí)現(xiàn)

    java 利用java反射機(jī)制動(dòng)態(tài)加載類的簡(jiǎn)單實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄猨ava 利用java反射機(jī)制動(dòng)態(tài)加載類的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09

最新評(píng)論

青龙| 汝南县| 新龙县| 锡林郭勒盟| 阿合奇县| 吴桥县| 错那县| 淮滨县| 台湾省| 旬邑县| 金寨县| 江陵县| 那曲县| 茌平县| 苍梧县| 南充市| 雷山县| 原平市| 新巴尔虎左旗| 车险| 蓝山县| 措美县| 特克斯县| 斗六市| 巍山| 昌都县| 凌云县| 怀宁县| 通河县| 大洼县| 阳泉市| 台江县| 托里县| 黄石市| 调兵山市| 凤山市| 栾川县| 赣榆县| 崇义县| 昌都县| 秀山|