欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ī)范,它提供了一套完整的接口,允許便攜式訪問(wèn)到底層數(shù)據(jù)庫(kù)

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)一訪問(wèn)
  • 本質(zhì)是通過(guò)代碼自己實(shí)現(xiàn)一個(gè)MySQL客戶端,通過(guò)網(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為了解決這一問(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ù)的用戶名
        ((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ǔ)句(寫入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();
    }

到此這篇關(guān)于MySQL詳解進(jìn)行JDBC編程與增刪改查方法的文章就介紹到這了,更多相關(guān)MySQL JDBC編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論