public class Activityjdbc { private DataSource dataSource; protected static final Logger logger = LoggerFactory.getLogger(Activityjdbc.class); /** * 插入 */ public void insert() { String sql ="插入sql语句"; Connection conn = null; Statement state =null; try { conn = dataSource.getConnection(); state = conn.createStatement(); state.executeUpdate(sql); conn.commit();//提交(增,删,改) } catch (SQLException e) { JdbcUtil.rollbackConnection(conn); logger.info(e.getMessage(), e); }finally{ JdbcUtil.closeStatement(state); JdbcUtil.closeConnection(conn); } } /** * 查询 */ public void QueryHdIndex() { String sql = "查询语句";int a = 0; Connection conn = null; Statement state =null; ResultSet rs=null; try { conn = dataSource.getConnection(); state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { a = rs.getInt(0); } } catch (SQLException e) { JdbcUtil.rollbackConnection(conn); logger.info(e.getMessage(), e); }finally{ JdbcUtil.closeResultSet(rs); JdbcUtil.closeStatement(state); JdbcUtil.closeConnection(conn); } } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } }