【java学习】JDBC

1 JDBC连接 mysql

1.导入jdbc的实现类(驱动)jar包
2.注册驱动
3.获得连接
4.获得Statement
5.执行sql语句
6.如果有结果集处理结果集
7.释放资源

//获得连接
String url="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC";
Connection con = DriverManager.getConnection(url, "root", "****");
//获得Statement
Statement stmt=con.createStatement();
//执行sql语句
String sql3="select * from emp";//DQL
Statement stmt=con.createStatement();

2 sql注入

通过字符串的拼接, 改变了原来的sql语句结构:

登录密码: 123’ or ‘a’='a

发送的语句:select * from users where username=‘rose’ and password = ‘123’ or ‘a’=‘a’ ,结果一直为true

Statement: 直接通过连接获得
要执行sql语句时, 才需要将sql发送给数据库
execute(sql)
PreparedStatement: 获得对象时, 已经确定要执行的sql语句了

-> 已经将sql发送给数据库
conn.prepareStatement(sql)

String sql = "select * from users where username=? and password = ?";
// 获得预编译的 语句块 : 就将sql语句发送给数据库
PreparedStatement stmt = conn.prepareStatement(sql);
// 执行sql语句之前, 需要确定 ? 的具体内容: 设置参数
stmt.setString(1, username);
stmt.setString(2, password);
// 执行sql语句, 不需要再次发送sql语句给数据库
ResultSet rs = stmt.executeQuery();

3 连接池

c3p0连接池

工具类 C3p0Utils

public class C3p0Utils {
    private static DataSource dataSource;
    static {
        dataSource = new ComboPooledDataSource();
    }
    public static DataSource getDataSource(){
        return dataSource;
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

配置文件: 目录必须是 src/c3p0-config.xml

<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
        <property name="user">root</property>
        <property name="password">****</property>

    </default-config>
</c3p0-config>

初始化连接池

ComboPooledDataSource cpds = new ComboPooledDataSource();
Connection con=cpds.getConnection();

druid连接池

 Properties props = new Properties();
        props.load(Demo4.class.getResourceAsStream("druid.properties"));

// 初始化连接池: 使用配置文件来设置那些属性 取代了set方法
DataSource dataSource = DruidDataSourceFactory.createDataSource(props);

Connection connection = dataSource.getConnection();

配置文件 druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username=root
password=123456

DBCP连接池

工具类 DbcpUtils

public class DbcpUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static Properties props;

    static {
        props=new Properties();
        try {
            props.load(DbcpUtils.class.getResourceAsStream("dbcp.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = props.getProperty("driver");
        url = props.getProperty("url");
        username = props.getProperty("username");
        password = props.getProperty("password");
    }
    public static Connection getConnection(){
        Connection con = null;
        try {
            DataSource dataSource = BasicDataSourceFactory.createDataSource(props);
            con=dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
}

初始化

Connection con = DbcpUtils.getConnection();

4 JdbcTemplate

使用步骤:
1.导入jar包
2.创建一个JdbcTemplate对象
3.使用 JdbcTemplate对象 操作数据库

JdbcTemplate template=new JdbcTemplate(C3p0Utils.getDataSource());
String sql=null;
//增
sql="insert into account(name,balance) value (?,?)";
template.update(sql,"lucy",2500);
//删
sql="delete from account where id=6";
template.update(sql);
//改
sql="update account set balance=? where id=?";
template.update(sql,2000,4);

也可以转换成对象来存储

RowMapper: 将对象的属性和表的字段建立映射关系
完成: 一行数据自动封装成对象, 同时字段内容注入到对象的属性中
实现类 new BeanPropertyRowMapper(类.class)
原理: 根据反射 -> 能看到类中所有的信息
属性名 -> 当成表中的字段名 rs.getXXX(“属性名”)
属性对应的类型 -> get类型(“属性名”)
进行自动的封装, 和属性的注入

//查
sql="select * from emp where ename like ?";
List<Emp> l = template.query(sql, new BeanPropertyRowMapper<>(Emp.class), "%S%");
System.out.println(l);

sql="select * from emp where empno=?";
Emp emp = template.queryForObject(sql, new BeanPropertyRowMapper<>(Emp.class), 7369);
System.out.println(emp);

sql="select count(1) from emp";
String s = template.queryForObject(sql, String.class);
System.out.println(s);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值