zoukankan      html  css  js  c++  java
  • 查增删改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();
                }
            }
        }
        
    }
  • 相关阅读:
    软件测试进程&测试类型
    课堂笔记:软件测试知识点汇总小结
    闰年测试程序
    关于 int.parse("abcd") 出错的问题分析及解决方案
    软件测试——字符串检测2.0
    边界值分析法实例分析
    测试管理
    软件评审
    单元测试与集成测试
    白盒测试
  • 原文地址:https://www.cnblogs.com/52xuanxuan/p/5982434.html
Copyright © 2011-2022 走看看