• 2020.11.8收获


    DaoTest.java

    package 人口普查系统;
    
    import static org.junit.Assert.*;
    
    import java.sql.SQLException;
    
    import org.junit.Test;
    
    public class DaoTest {
    
        @Test
        public void test() throws SQLException {
            Dao dao=new Dao();
            System.out.print(dao.searchByName("2").size());
        }
    }

    DBUtil.java

    package 人口普查系统;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class DBUtil {
        private static String url = "jdbc:mysql://localhost:3306/course?useUnicode=true&characterEncoding=utf8";
        private static String user = "root";
        private static String password = "123";
        private static String jdbcName="com.mysql.jdbc.Driver";
        private Connection con=null;
        public static  Connection getConnection() {
            Connection con=null;
            try {
                Class.forName(jdbcName);
                con=DriverManager.getConnection(url, user, password);
                //System.out.println("数据库连接成功");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //System.out.println("数据库连接失败");
                e.printStackTrace();
            }
            try {
                con = DriverManager.getConnection(url,user,password);
                System.out.println("连接成功");
    
    
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return con;
        }
        public static void main(String[] args)throws SQLException { 
            Connection conn = getConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from person";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            System.out.println(getConnection());
            while(rs.next()){
                System.out.println("成功");
            }
    
            }
    
           // return con;
            
        
        public static void close(Connection con) {
            if(con!=null)
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            
        }
        public static void close(Statement state, Connection conn) {
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close(ResultSet rs, Statement state, Connection conn) {
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    CF 538 D. Flood Fill 递归 区间DP
    P3355 骑士共存问题 二分建图 + 当前弧优化dinic
    P1726 上白泽慧音 tarjan 模板
    P1073 最优贸易 建立分层图 + spfa
    P2774 方格取数问题 网络最大流 割
    P2157 [SDOI2009]学校食堂 状压DP
    P1357 花园 状压 矩阵快速幂
    P3084 [USACO13OPEN]照片Photo dp
    CF_EDU51 E. Vasya and Big Integers
    搜索:DLX算法
  • 原文地址:https://www.cnblogs.com/ltw222/p/13965653.html
Copyright © 2020-2023  润新知