zoukankan      html  css  js  c++  java
  • android 学习

    DBUtil层

    package DBUtil;
    
    
    
    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 {
        
        public static String db_url = "jdbc:mysql://localhost:3306/cov2021?serverTimezone=GMT%2B8&useSSL=false";
        public static String db_user = "root";
        public static String db_pass = "20194015";
        
        public static Connection getConn () {
            Connection conn = null;
            
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return conn;
        }//end getConn
        
        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();
                }
            }
        }
    
        public static void main(String[] args) throws SQLException {
            Connection conn = getConn();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from china";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("成功");
            }else{
                System.out.println("失败");
            }
        }
    }
  • 相关阅读:
    node设置res.cookie跨域问题解决
    Rails常用插件
    什么是编程基础
    【数学之美】抽屉原理
    时分秒针重合问题
    如何理解【业务逻辑】
    【转】有关环境变量的若干问题?
    关于指针类型转化后在printf输出的问题
    rand * () 之间相互生成总结
    4.14做一个新浪微博的小东东
  • 原文地址:https://www.cnblogs.com/Nojava/p/14908269.html
Copyright © 2011-2022 走看看