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支持多少并发
    Redis高可用架构—Keepalive+VIP
    MapReduce运行原理
    hihoCoder 1015 KMP算法(kmp)
    <html>
    UI--仿IOS控件之ActionSheet样式 and more..
    redis集群
    mongodb及mongoclient在win7下的编译和使用
    @property与@synthesize的差别
    传智播客c/c++公开课学习笔记--邮箱账户的破解与邮箱安全防控
  • 原文地址:https://www.cnblogs.com/yikuan-919/p/9512177.html
Copyright © 2011-2022 走看看