原生jdbc

原生jdbc


package com.mm.test;


import org.junit.Test;


import java.sql.*;
import java.util.Objects;


/**
* @类名: Demo1
* @说明: dbc入门小demo
* @作者: 小马
* @时间:2019/10/11 14:36
*/


public class Demo1 {


    private static String url="jdbc:mysql://localhost:3306/mk";
    private static String username="root";
    private static String password="root";
    public static void main(String[] args) throws Exception {


        //1.驱动
        Class.forName("com.mysql.jdbc.Driver");


        //2.获得连接
        Connection connection = DriverManager.getConnection(url, username, password);


        //3.获得执行sql语句的对象
        PreparedStatement preparedStatement = connection.prepareStatement("select * from t_cuser");


        //4.执行sql语句,并返回结果
        ResultSet rs = preparedStatement.executeQuery();


        //5.处理结果
        //遍历
        while (rs.next()){
            int anInt = rs.getInt(1);
            System.out.println(anInt);
        }


        //6.释放资源
        if (!Objects.isNull(rs)){
            rs.close();
        }
        if (!Objects.isNull(preparedStatement)){
            preparedStatement.close();
        }
        if (!Objects.isNull(connection)){
            connection.close();
        }


    }


    //添加
    @Test
    public void testInsert(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            preparedStatement = connection.prepareStatement("insert into t_cuser(cname) values('奢侈品')");
            int i = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (!Objects.isNull(preparedStatement)){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(connection)){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //删除
    @Test
    public void testDelect(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            preparedStatement = connection.prepareStatement("delete from t_cuser where cid=6");
            int i = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (!Objects.isNull(preparedStatement)){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(connection)){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //修改
    @Test
    public void testUpdate(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            preparedStatement = connection.prepareStatement("update t_cuser set cname='奢侈品' where cid=5");
            int i = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (!Objects.isNull(preparedStatement)){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(connection)){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }




    //查询
    @Test
    public void testSelect(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection(url,username,password);
            preparedStatement=connection.prepareStatement("select * from t_cuser");
            rs = preparedStatement.executeQuery();
            while (rs.next()){
                int anInt = rs.getInt(1);
                String an = rs.getString(2);
                System.out.println(anInt+"-"+an);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (!Objects.isNull(rs)){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(preparedStatement)){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(connection)){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值