jdbc……………

package com.dashabi;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class DBUtils {
    private static DruidDataSource dataSource = new DruidDataSource();

    static {
        dataSource.setUrl("jdbc:mysql://localhost:3306/task?serverTimezone=UTC&useSSL=true");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); //这个可以缺省的,会根据url自动识别
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //下面都是可选的配置
        dataSource.setInitialSize(10);  //初始连接数,默认0
        dataSource.setMaxActive(30);  //最大连接数,默认8
        dataSource.setMinIdle(10);  //最小闲置数
        dataSource.setMaxWait(2000);  //获取连接的最大等待时间,单位毫秒
        dataSource.setPoolPreparedStatements(true); //缓存PreparedStatement,默认false
        dataSource.setMaxOpenPreparedStatements(20); //缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句代码
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;//出现错误 return null
    }
}

package com.dashabi;

public class Student {
    private String name;
    private Integer id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}

/

package com.dashabi;

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

/**
 *
 * @author 黄大仙
 * @date 2020-06-13 18:30
 */
public class aa {
    public static void main(String[] args) throws SQLException {
        //chaxun(7);

        //chuxunsuoyou();

        Student student = new Student();
        student.setId(8);
        student.setName("张天师吃粑粑");
        xiugai(student);


        //shanchu(11);
    }

    /**
     * 按id查询
     *
     * @param id
     * @throws SQLException
     */
    public static void chaxun(int id) throws SQLException {
        Connection connection = DBUtils.getConnection();
        PreparedStatement stm= connection.prepareStatement("select * from student where id=?");
        stm.setInt(1,id);
        ResultSet rs = stm.executeQuery();
        Student stu = new Student();
        if (rs.next()) {
            stu.setId(rs.getInt("id"));
            stu.setName(rs.getString("name"));
        }
        System.out.println(stu.toString());
        connection.close();

    }

    /**
     * 查询所有数据
     *
     * @return
     * @throws SQLException
     */
    public static List<Student> chuxunsuoyou() throws SQLException {
        List<Student> stuList=new ArrayList<Student>();//集合
        Connection connection = DBUtils.getConnection();
        PreparedStatement stm= connection.prepareStatement("select * from student");
        ResultSet rs = stm.executeQuery();
        Student stu = new Student();
        while (rs.next()) {
            stu.setId(rs.getInt("id"));
            stu.setName(rs.getString("name"));
            stuList.add(stu);
            System.out.println(stu.toString());
        }
        connection.close();
        return stuList;
    }

    /**
     * 按id修改数据
     *
     * @param student
     * @throws SQLException
     */
    public static void xiugai(Student student) throws SQLException {
        Connection connection = DBUtils.getConnection();
        PreparedStatement stm= connection.prepareStatement("update student set name=? where id=?");
        stm.setString(1,student.getName());
        stm.setInt(2,student.getId());
        System.out.println("修改成功行数: "+stm.executeUpdate());
        connection.close();
    }

    /**
     * 按id删除数据
     *
     * @param id
     * @throws SQLException
     */
    public static void shanchu(int id) throws SQLException {
        Connection connection = DBUtils.getConnection();
        PreparedStatement stm= connection.prepareStatement("delete  from student where id=?");
        stm.setInt(1,id);
        System.out.println("删除成功行数: "+stm.executeUpdate());
        connection.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值