• 查增删改MySQL数据库固定模式


    省略相关包的导入...
    
    public class Base {
    
        public static Connection connection = null;
        public static PreparedStatement preparedStatement = null;
        public static ResultSet resultSet = null;
        public static int updateRows = 0;
        
        public Connection tomcatGetConnection() {
            try {
                Context context = new InitialContext();
                DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/mysql");
                connection = dataSource.getConnection();
            } catch (NamingException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
        
        public ResultSet query(String sql, Object[] param) {
            try {
                preparedStatement = connection.prepareStatement(sql);
                for (int i = 0; i < param.length; i++) {
                    preparedStatement.setObject(i + 1, param[i]);
                }
                resultSet = preparedStatement.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return resultSet;
        }
        
        public int update(String sql, Object[] param) {
            try {
                preparedStatement = connection.prepareStatement(sql);
                for (int i = 0; i < param.length; i++) {
                    preparedStatement.setObject(i + 1, param[i]);
                }
                updateRows = preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return updateRows;
        }
        
        public void close() {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
  • 相关阅读:
    JQuery 图片轮播
    js版的虚线框
    折叠菜单,选择下拉(手风琴)
    logstash的index值可以为中文
    假如正则从来没来过,我们该如何去匹配一个字符串?
    深度解析javascript中的浅复制和深复制
    笔试题
    前端笔试题总结---持续更新
    清除浮动
    一步一步的理解闭包
  • 原文地址:https://www.cnblogs.com/52xuanxuan/p/5982434.html
Copyright © 2020-2023  润新知