溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用MySQL進(jìn)行JDBC編程與增刪改查

發(fā)布時(shí)間:2022-06-15 13:48:57 來(lái)源:億速云 閱讀:118 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“怎么使用MySQL進(jìn)行JDBC編程與增刪改查”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“怎么使用MySQL進(jìn)行JDBC編程與增刪改查”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

Java的數(shù)據(jù)庫(kù)編程JDBC

概念

  • JDBC是一種用于執(zhí)行sql語(yǔ)句的Java API,他是java中的數(shù)據(jù)庫(kù)連接規(guī)范,這個(gè)API由一些接口和類組成。它為java開發(fā)人員操作數(shù)據(jù)庫(kù)提供了一個(gè)標(biāo)準(zhǔn)的API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問

  • 本質(zhì)是通過代碼自己實(shí)現(xiàn)一個(gè)MySQL客戶端,通過網(wǎng)絡(luò)和服務(wù)器進(jìn)行數(shù)據(jù)的交互,客戶端不能憑空出現(xiàn),所以數(shù)據(jù)庫(kù)提供了一組API方便我們實(shí)現(xiàn)

  • 數(shù)據(jù)庫(kù)的種類有很多,不同的數(shù)據(jù)庫(kù)提供的API不太一樣,所以java為了解決這一問題提供了JDBC,java自帶的一種數(shù)據(jù)庫(kù)操作API,這種API覆蓋所有數(shù)據(jù)庫(kù)操作的操作方式

  • 本質(zhì)上是java自身完成了JDBC API和數(shù)據(jù)庫(kù)API之間進(jìn)行轉(zhuǎn)換

怎么使用MySQL進(jìn)行JDBC編程與增刪改查

使用步驟

創(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ù)的用戶名
        ((MysqlDataSource)dataSource).setUser("root");
        //設(shè)置登錄數(shù)據(jù)庫(kù)的密碼
        ((MysqlDataSource)dataSource).setPassword("woshizhu123");

通過Connection連接數(shù)據(jù)庫(kù)(輸入密碼連接成功)

//import java.sql.Connection;
 Connection connection  = dataSource.getConnection();

拼接sql語(yǔ)句(寫入sql語(yǔ)句)

String sql = "insert into student values(1,'張三')";

將sql語(yǔ)句包裝成對(duì)象

PreparedStatement statement = connection.prepareStatement(sql);

執(zhí)行sql語(yǔ)句(按下回車執(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();
    }

讀到這里,這篇“怎么使用MySQL進(jìn)行JDBC編程與增刪改查”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI