JDBC简单的CRUD操作

package com.jdbc.day01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class CRUD {
    @Test
    public void create(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String sql = "create table user(id int not null auto_increment,username varchar(255),password varchar(255),sex varchar(255),age INTEGER,primary key(id))";
        JDBCUtils jdbc = JDBCUtils.getInstance();
        try {
            //创建连接
            conn=jdbc.getConnection();
            //创建执行sql语句
            st=conn.createStatement();
            //执行sql语句
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //关闭资源
            jdbc.free(conn, st, rs);
        }
    }
    @Test
    public void insert(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String sql = "insert into user(username,password,sex,age)values('小f','123456','1','18')";
        JDBCUtils jdbc = JDBCUtils.getInstance();
        try {
            conn = jdbc.getConnection();
            st = conn.createStatement();
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            jdbc.free(conn, st, rs);
        }
    }
    @Test
    public void read(){
        Connection conn=null;
        Statement st = null;
        ResultSet rs=null;
        JDBCUtils jdbc = JDBCUtils.getInstance();
        String sql = "select username,password,sex,age from user";
        try {
            conn=jdbc.getConnection();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while(rs.next()){
                System.out.println(rs.getObject("username")+"\t"
                        +rs.getObject("password")+"\t"
                        +rs.getObject("sex")+"\t"
                        +rs.getObject("age")+"\t");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            jdbc.free(conn, st, rs);
        }

    }
    @Test
    public void update(){
        Connection conn=null;
        Statement st = null;
        ResultSet rs=null;
        JDBCUtils jdbc = JDBCUtils.getInstance();
        String sql = "update user set age = age+1 where id>3";
        try {
            conn=jdbc.getConnection();
            st = conn.createStatement();
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            jdbc.free(conn, st, rs);
        }
    }

    @Test
    public void delete(){
        Connection conn=null;
        Statement st = null;
        ResultSet rs=null;
        JDBCUtils jdbc = JDBCUtils.getInstance();
        String sql = "delete from user where id>5";
        try {
            conn=jdbc.getConnection();
            st = conn.createStatement();
            st.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            jdbc.free(conn, st, rs);
        }
    }

    /*-----------------------------------------------------PreparedStatement防止sql注入,预编译效率高---------------------------*/
    @Test
    public void psUpdate(){
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        JDBCUtils jdbc = JDBCUtils.getInstance();
        String sql = "update user set password=? where id=?";
        try {
            conn=jdbc.getConnection();
            ps = conn.prepareStatement(sql);
            //为每个问号赋值,1和2分别代表第一个问号和第二个问号
            ps.setString(1, "654321");
            ps.setString(2, "5");
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            jdbc.free(conn, ps, rs);
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值