MySQL数据库-JDBC基础

一.写操作(insert update delete)

import com.mysql.cj.jdbc.MysqlDataSource;

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

public class Demo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号:");
        int id = scanner.nextInt();
        System.out.println("请输入姓名:");
        String name = scanner.next();

        // 1.先创建DataSource
        //(这种写法后续如果修改为其他数据库,代码改动较小,可以降低耦合)
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        //2.建立数据库与服务器之间的连接,连接完成后,才能进行后续的请求相应交互
        Connection connection = dataSource.getConnection();

        //3.构造sql
//        String sql = "insert into student value(" + id + ", '" + name + "')"; //有sql注入的风险 如果输入的不是字符串而是sql语句,会对数据库造成影响
//        String sql = "delete from student where id = 1";
//        String sql = "update student set name = 'zhangsan' where id = 100";
        String sql = "insert into student value(?, ?)"; //使用问号作为占位符
        PreparedStatement statement = connection.prepareStatement(sql); //检查sql,省下服务器解析的工作
        statement.setInt(1, id); //从1开始,表示第几个占位符
        statement.setString(2, name);

        //4.把sql发送给服务器,执行sql
        //statement.executeQuery(); //select 读操作
        //返回的整数表示影响到的行数
        int n = statement.executeUpdate(); //insert update delete 写操作
//        System.out.println(n);

        //5.关闭连接,释放资源。后获取到的资源,先释放。
        statement.close();
        connection.close();
    }
}

二.读操作(select)

import com.mysql.cj.jdbc.MysqlDataSource;

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

public class Demo2 {
    public static void main(String[] args) throws SQLException {
        // 1.创建datasource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        // 2.建立连接
        Connection connection = dataSource.getConnection();

        // 3.构造sql
        String sql = "select * from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, 2);

        // 4.执行sql
        //resultset储存结果(临时表),此处要针对表进行遍历
        ResultSet resultSet = statement.executeQuery();

        // 5.遍历结果集合
        // next相当于一个光标,指向哪一行之前,取出哪一行
        // 如果取到最后一行后,再执行next返回false,循环结束
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + " " + "name = " + name);
        }

        // 6.释放资源,后获取到的资源,先释放。
        resultSet.close();
        statement.close();
        connection.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值