zoukankan      html  css  js  c++  java
  • 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;
                    }
                }
            }    
        }
    }
  • 相关阅读:
    tomcat使用不同的jdk版本 liunx 装两个jdk
    接下来自己的研究对象
    钉钉小程序开发的所有坑
    java 在web应用中获取本地目录和服务器上的目录不一致的问题
    Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
    vue项目中禁止移动端双击放大,双手拉大放大的方法
    JZ56 删除链表中重复的结点
    JZ55 链表中环的入口结点
    JZ54 字符流中第一个不重复的字符
    JZ53 表示数值的字符串
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9512177.html
Copyright © 2011-2022 走看看