Java的数据库编程:JDBC

1.数据库编程

MySQL是一个CS结构的系统。
在这里插入图片描述
我们这个数据库编程呢,就是通过自己写代码实现一个MySQL客户端,同样是通过网络和服务器进行交互。

客户端不是很容易实现的。数据库会给我们提供一组API(application programming interface),提供一组函数/类/方法,让用户去直接使用,来方便我们实现。

不同的数据库提供的API都是差距比较大的,所以为了解决这个问题,Java中引入JDBC,事Java自带的一组数据库操作的API,相当于涵盖了各种数据操作的操作方式,把不同的数据库的API统一到了一起。

不同类型的API可以通过适配器转换为JDBC统一的API,不用的数据库需要提供不同的驱动程序去搭配到JDBC中。

再Java中驱动程序是一个独立的jar包(很多.class文件的压缩)

数据库连接Connection

编程实现客户端过程:
1.创建DataSource对象(准备工作)
2.基于DataSource对象,创建Connection对象,和数据库建立连接(打开了客户端,输入了密码,连接成功)
3.PrepareStatement对象拼装具体的SQL语句(客户端输入SQL的过程)
4.拼装好SQL后,需要执行SQL(客户端按下回车,SQL就被发到服务器了)
5.查看服务器返回结果(客户端显示结果)
6.关闭连接,释放资源(退出客户端)

JDBC提供了两套API:
一种是通过DriverManager(驱动管理类)的静态方法获取

一种是通过DataSource(数据源)对象获取。实际应用中会使用DataSource对象。

// 加载JDBC驱动程序 Class.forName(“com.mysql.jdbc.Driver”);
// 创建数据库连接 Connection connection = DriverManager.getConnection(url);
DataSource ds = new MysqlDataSource();
((MysqlDataSource) ds).setUrl(“jdbc:mysql://localhost:3306/test”);
((MysqlDataSource) ds).setUser(“root”);
((MysqlDataSource) ds).setPassword(“root”);
Connection connection = ds.getConnection();

JDBC实现增删改查

1》插入

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJDBC {
    public static void main(String[] args) throws SQLException {
        //创建dataSource对象
        DataSource dataSource = new MysqlDataSource();
       //对dataSource进行一些配置:URL,User,Password需要向下转型
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");

        //建立连接
        //connection对象生命周期较短,每个请求都需创建一个新的connection
        Connection connection = dataSource.getConnection();
        //拼装sql语句PrepareStatement
        //以插入为例
        int id = 1;
        String name = "曹操";
        int classId = 10;
        String sql = "insert into student values(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        //1,2,3相当于刚才问号的下标,从1开始
        statement.setInt(1,id);
        statement.setString(2,name);
        statement.setInt(3,classId);
        //执行sql
        //insert用executeUpdate
        //select 使用executeQuery来执行
        //返回值为操作修改了多少行
        int ret = statement.executeUpdate();
        System.out.println("ret: "+ ret);
        //关闭释放相关资源  后创建的被先释放
        statement.close();
        connection.close();

    }
}

在这里插入图片描述
2》查询

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class javaJDBCSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //ResultSet新的类,表示查找结果集
        ResultSet resultSet =  statement.executeQuery();
        //遍历结果集(相当于一张表,里面有很多行,每行又是一条记录)
        //next()一方面判断是否存在下一行,如果存在就获取这一行
        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);

        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}

在这里插入图片描述
在这里插入图片描述
3》删除

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCDelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除学生的姓名: ");
        String name = scanner.next();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        String sql = "delete from student where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        statement.close();
        connection.close();
    }
}

在这里插入图片描述
在这里插入图片描述
4》更新

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改学生姓名id:");
        int id = scanner.nextInt();
        System.out.println("请输入要修改学生的姓名: ");
        String name = scanner.next();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_0531?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("xxxxxx");
        Connection connection = dataSource.getConnection();
        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();
        if (ret == 1) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        statement.close();
        connection.close();

    }
}

在这里插入图片描述
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章