zoukankan      html  css  js  c++  java
  • 毕业设计-1.09

    情况概述:

      连接数据库,并完成了通用方法的编写。

    代码如下:

    DBUtil.java

    package com.zlc.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    //数据库连接 SqlServer
    public class DBUtil {
        // 驱动类名
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3307/weather?useUnicode=true&characterEncoding=UTF8";// JavaTest为你的数据库名称
        String user = "root";
        String password = "364624";
        Connection coon = null;
    
        public Connection DBConnection() {
            Connection coon = null;
            try {
                // 加载驱动程序
                Class.forName(driver);
                coon = DriverManager.getConnection(url,user,password);
                if (!coon.isClosed()) {
                    System.out.println("成功连接数据库!");
                } else {
                    System.out.println("失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return coon;
        }
    
        public void close() {
            try {
                this.coon.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            DBUtil db = new DBUtil();
            db.DBConnection();
        }
    }

    CommonMethod.Java

    package com.zlc.util;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Statement;
    
    public class CommonMethod {
        public static Connection conn = null;
        public static PreparedStatement ps = null;
        public static ResultSet rs = null;
    
        /**
         * 
         * @Title: createPreParedStatement
         * @Description: PreParedStatement的创建方法
         * @param: @param sql
         * @param: @param params
         * @param: @return
         * @param: @throws SQLException   
         * @return: PreparedStatement   
         * @throws
         */
        public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws SQLException {
            DBUtil db = new DBUtil();
            conn = db.DBConnection();
            ps = (PreparedStatement) conn.prepareStatement(sql);
            // 查询
            // set方法的个数依赖于?的个数,而?的个数与数组的个数一致
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            return ps;
        }
        
        /**
         * 
         * @Title: executeUpdate
         * @Description: 通用的增刪改查
         * @param: @param sql 语句
         * @param: @param params 参数
         * @param: @return   
         * @return: boolean   是否完成操作
         * @throws
         */
        public static boolean executeUpdate(String sql, Object[] params) {
            try {
                ps = createPreParedStatement(sql, params);
                // 执行查询-返回值
                int count = ps.executeUpdate();
                if (count > 0) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    closeAll(rs, (Statement) ps, conn);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 
         * @Title: executeQuery
         * @Description: 通用的查 返回值是一个集合
         * @param: @param sql 语句
         * @param: @param params 参数
         * @param: @return   
         * @return: ResultSet   返回集
         * @throws
         */
        public static ResultSet executeQuery(String sql, Object[] params) {
            try {
                ps = createPreParedStatement(sql, params);
                rs = ps.executeQuery();
                return rs;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        
        /**
         * 
         * @Title: closeAll
         * @Description: 关闭数据库连接
         * @param: @param rs
         * @param: @param stmt
         * @param: @param conn
         * @param: @throws SQLException   
         * @return: void   
         * @throws
         */
        public static void closeAll(ResultSet rs,Statement stmt,Connection conn) throws SQLException {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        }
    
        /**
         * 
         * @Title: getTotalCount
         * @Description: 查询数据总数
         * @param: @param sql
         * @param: @return   
         * @return: int   
         * @throws
         */
        public static int getTotalCount(String sql) {
            int count = -1;
            try {
                ps = createPreParedStatement(sql, null);
                rs = ps.executeQuery();
                if (rs.next()) {
                    count = rs.getInt(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    closeAll(rs, (Statement) ps, conn);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return count;
        }
        
        
        
    }
  • 相关阅读:
    在Centos 7下编译openwrt+njit-client
    开博随笔
    Chapter 6. Statements
    Chapter 4. Arrays and Pointers
    Chapter 3. Library Types
    Chapter 2.  Variables and Basic Types
    关于stm32不常用的中断,如何添加, 比如timer10 timer11等
    keil 报错 expected an identifier
    案例分析 串口的地不要接到电源上 会烧掉
    案例分析 CAN OPEN 调试记录 进度
  • 原文地址:https://www.cnblogs.com/zlc364624/p/14453649.html
Copyright © 2011-2022 走看看