Statement


```java
package com.atguigu.test;

import com.atguigu.utils.JDBCUtils;
import org.junit.Test;

import java.sql.*;

/**
 * Date:2021/1/6
 * Author:ybc
 * Description:通过JDBC测试增删改查
 */
public class CRUDTest {

    /**
     * JDBC的步骤:
     * 1、注册驱动(加载驱动)
     * 2、创建连接
     * url:使用统一资源定位符连接mysql
     * 统一资源定位符:协议://ip:port/资源
     * 连接mysql的统一资源定位符-->jdbc:mysql://localhost:3306/数据库名
     * 访问服务器中资源的统一资源定位符-->http://localhost:8080/资源
     * username:mysql的登录名
     * password:mysql的登录密码
     * 3、创建预编译对象Statement
     * 4、执行sql
     * 实现增删改-->executeUpdate()
     * 实现查询-->executeQuery()
     * 查询功能的返回值为ResultSet,可以通过rs.next()判断是否由下一个数据,并将指针指向下一个数据
     * rs.getXxx(int columnCount):通过列数获取数据
     * rs.getXxx(String columnName):通过列名(字段名)获取数据
     */

    
    //添加测试
    @Test
    public void testInsert(){
        Connection connection = null;
        Statement statement = null;
        try {
            //1、注册驱动(加载驱动)
            Class.forName("com.mysql.jdbc.Driver");
            //2、创建连接
            String url = "jdbc:mysql://localhost:3306/test";
            String username = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, username, password);
            //3、创建预编译对象
            statement = connection.createStatement();
            //执行sql
            int i = statement.executeUpdate("insert into t_user values(null,'admin1','123456',23,'男'),(null,'admin2','123456',23,'男')");
            System.out.println("结果-->"+i);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    
    //删除测试
    @Test
    public void testDelete(){
        Connection connection = null;
        Statement statement = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            //创建预编译对象
            statement = connection.createStatement();
            //执行sql
            int i = statement.executeUpdate("delete from t_user where id = 4");
            System.out.println("结果-->"+i);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, statement, connection);
        }
    }

    
    //更新测试
    @Test
    public void testUpdate(){
        Connection connection = null;
        Statement statement = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            //创建预编译对象
            statement = connection.createStatement();
            //执行sql
            int i = statement.executeUpdate("update t_user set username = 'ybc', password = '123' where id = 5");
            System.out.println("结果-->"+i);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, statement, connection);
        }
    }

    
    //查询测试
    @Test
    public void testSelect(){
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            statement = connection.createStatement();
            rs = statement.executeQuery("select * from t_user");
            while (rs.next()){
                System.out.println("id:"+rs.getInt(1)+",username:"+rs.getString(2)+",password:"+rs.getString(3)+",age:"+rs.getInt(4)+",sex:"+rs.getString(5));
                //System.out.println("id:"+rs.getInt("id")+",username:"+rs.getString("username")+",password:"+rs.getString("password")+",age:"+rs.getInt("age")+",sex:"+rs.getString("sex"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, statement, connection);
        }
    }

    
    //查询测试
    @Test
    public void testSelectOne(){
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            statement = connection.createStatement();
            rs = statement.executeQuery("select * from t_user where id = 1");
            while (rs.next()){
                System.out.println("id:"+rs.getInt(1)+",username:"+rs.getString(2)+",password:"+rs.getString(3)+",age:"+rs.getInt(4)+",sex:"+rs.getString(5));
                //System.out.println("id:"+rs.getInt("id")+",username:"+rs.getString("username")+",password:"+rs.getString("password")+",age:"+rs.getInt("age")+",sex:"+rs.getString("sex"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, statement, connection);
        }
    }

    
    //登录测试
    @Test
    public void testLogin(){
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            statement = connection.createStatement();
            rs = statement.executeQuery("select * from t_user where username = 'admin123' and password = '123456'");
            System.out.println(rs.next()?"登录成功":"登录失败");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs, statement, connection);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值