• 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;
                    }
                }
            }    
        }
    }
  • 相关阅读:
    如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)
    jQuery插件之----缓冲运动
    jQuery插件学习基础
    javascript随机一个1-9的数字
    jQuery事件之鼠标事件
    枚举类的应用
    ConvertUtils.register(new DateConverter(null), java.util.Date.class)使用
    逆向工程不使用驼峰命名而保持字段中下划线
    mongoDB 的介绍
    eclipse 的版本及下载地址
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9512177.html
Copyright © 2020-2023  润新知