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();
                }
            }
        }
        
    }
  • 相关阅读:
    pm2 配置
    添加项目到远程服务器(git)
    psql 命令行使用
    SQL
    iOS AFNetworking 打印从服务器返回的错误提示信息
    iOS 获取网络图片的大小
    iOS 10 常见配置的问题
    LGLTagsView
    xcode8 关闭控制台打印不用信息
    LGLProgressHUD
  • 原文地址:https://www.cnblogs.com/52xuanxuan/p/5982434.html
Copyright © 2011-2022 走看看