寒假实训第八天

今天主要学习了数据库的搭建,数据库的连接

获得连接和关闭连接

package com.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String url = "";
    private static String username = "";
    private static String password = "";


    //静态代码块  只执行一次
    static{
        InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        //6、释放资源 Connection,PreparedStatement,ResultSet,释放时需从后往前
        //如果connection,preparedStatement,resultSet对象为null JVM GC
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

 

数据库的增删改查

package com.util;

import org.junit.Test;

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

import static org.junit.Assert.*;


public class DBUtilsTest {
    private static final String INSERT_SQL = "insert into emp(empno,ename,job) values(?,?,?)";
    private static final String UPDATE_SQL = "update emp set ename=?,job=? where empno=?";
    private static final String DELETE_SQL = "delete from emp where empno=?";

    @Test
    public void test01() throws SQLException {
        Connection connection = DBUtils.getConnection();
        System.out.println(connection);
    }
    @Test
    public void insertTest(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement(INSERT_SQL);
            preparedStatement.setString(1,"2024");
            preparedStatement.setString(2,"赵");
            preparedStatement.setString(3,"CEO");
            int i = preparedStatement.executeUpdate();
            if (i > 0){
                System.out.println("插入成功!");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(connection,preparedStatement,null);
        }
    }
    
    @Test
    public void updateTest(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement(UPDATE_SQL);
            preparedStatement.setString(1,"张");
            preparedStatement.setString(2,"董事长");
            preparedStatement.setString(3,"2024");
            int i = preparedStatement.executeUpdate();
            if (i > 0){
                System.out.println("修改成功!");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(connection,preparedStatement,null);
        }
    }
    
    @Test
    public void deleteTest(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement(DELETE_SQL);
            preparedStatement.setString(1,"2024");
            boolean b = preparedStatement.execute();
            if (b != false){
                System.out.println("删除成功");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(connection,preparedStatement,null);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值