查增删改MySQL数据库固定模式
Posted 人唯虚、始能知人!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查增删改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数据库固定模式的主要内容,如果未能解决你的问题,请参考以下文章