JDBC进行数据库的--增--删--改--案例----纯代码

package cn.yikuan.crud;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Logger;

import org.junit.Test;

/**
 * 这个类用来完成JDBC的增删改业务
 */
public class JdbcCrud {
    //单元测试方法:@Test + void + public
    @Test
    public void add(){
        Connection conn = null;
        Statement st = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            String url = "jdbc:mysql:///jtdb";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url, user, password);
            //3.获取传输器
            st = conn.createStatement();
            //4.执行sql
            String sql = "insert into account values(null,'WangHT',1000)";
            int rows = st.executeUpdate(sql);
            //5.遍历结果集
            System.out.println(rows);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    st = null;
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    conn = null;
                }
            }
            
        }    
    }
    
    @Test
    public void update(){
        Connection conn = null;
        Statement st = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            String url = "jdbc:mysql:///jtdb";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url, user, password);
            //3.获取传输器
            st = conn.createStatement();
            //4.执行sql
            String sql = "update account set money=1001 where name='WangHT' ";
            int row = st.executeUpdate(sql);
            //5.遍历结果集
            System.out.println(row);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    st = null;
                }            
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    conn = null;
                }            
            }
        }
    }
    
    @Test
    public void del(){
        /*
         * 1.注册驱动
         * 2.获取数据库连接
         * 3.获取传输器
         * 4.执行sql
         * 5.遍历结果集
         * 6.释放资源
         */
        Connection conn = null;
        Statement st = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            String url = "jdbc:mysql:///jtdb";
            String user = "root";
            String password = "123456";
            conn = DriverManager.getConnection(url, user, password);
            //3.获取传输器
            st = conn.createStatement();
            // 4.执行sql
            String sql = "delete from account where id=3";
            int row = st.executeUpdate(sql);
            //5.遍历结果集
            System.out.println(row);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    st = null;
                }
            }
            
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    conn = null;
                }
            }
        }    
    }
}
原文地址:https://www.cnblogs.com/yikuan-919/p/9512177.html