首页 > 代码库 > 查增删改MySQL数据库固定模式
查增删改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(); } } } }
查增删改MySQL数据库固定模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。