spring jdbc JdbcTemplate入门
程序员文章站
2022-04-25 10:01:25
...
填
jdbcTemplate.execute("CREATE TABLE USER (user_id integer, name varchar(100))");
增
@Override public int save(ArchiveBlob ab) { String sql = "insert into trace_archive_blob_table(id_archive,archive_name,application_id,date_string,date_string_2,archive_type,time_string,archive_value) values (?,?,?,?,?,?,?,?)"; return this.getJdbcTemplate().update( sql, new Object[] {ab.getIdArchive(),ab.getArchiveName(),ab.getApplicationId(),ab.getDateString(),ab.getDateString2(),ab.getArchiveType(),ab.getTimeString(),ab.getArchiveValue()}); }
删
@Override public int deleteArchiveBlob(String idAchrive){ String sql = "delete from trace_archive_blob_table where id_archive='" + idAchrive+"'"; return this.getJdbcTemplate().update(sql); }
查
@SuppressWarnings({ "rawtypes" }) private class ArchiveBlobRowMapper implements RowMapper { @Override public ArchiveBlob mapRow(ResultSet rs, int i) throws SQLException { ArchiveBlob ab = new ArchiveBlob(); ab.setIdArchive(rs.getString("id_archive")); ab.setArchiveName(rs.getString("archive_name")); ab.setApplicationId(rs.getLong("application_id")); ab.setDateString(rs.getLong("date_string")); ab.setDateString2(rs.getLong("date_string_2")); ab.setArchiveType(rs.getInt("archive_type")); ab.setTimeString(rs.getLong("time_string")); ab.setArchiveValue(rs.getString("archive_value")); return ab; } }
查Object
@SuppressWarnings("unchecked") @Override public ArchiveBlob getArchiveBlob(String idAchrive) { List <ArchiveBlob> list = new ArrayList<ArchiveBlob>(); try{ String sql = "select id_archive,archive_name,application_id,date_string,date_string_2,archive_type,time_string,archive_value " + "from trace_archive_blob_table " + "where id_archive = '" + idAchrive+"'"; list = this.getJdbcTemplate().query(sql, new ArchiveBlobRowMapper()); if (list !=null && list.size() >0) { return list.get(0); } }catch(Exception e){ logger.error("Exception,", e); } return null; }
查 string
@Override public String getArchiveBlobArchiverValue(String idAchrive) { String sql = "select archive_value from trace_archive_blob_table where id_archive = '" + idAchrive+"'"; return this.getJdbcTemplate().queryForObject(sql, String.class); }
查list
@SuppressWarnings("unchecked") public List<UserAreaRelation> getUserAreaRelationList(int start, int end){ String sql =" select telephone,province, city, supplier from application_user_id_area_relation_table limit "+start+","+end+" "; return this.getJdbcTemplate().query(sql, new UserAreaRelationRowMapper()); }
改
@Override public int update(ArchiveBlob ab) { try { String sql = "update trace_archive_blob_table set archive_name = '"+ab.getArchiveName()+"' ,application_id = "+ab.getApplicationId()+" ,date_string= "+ab.getDateString()+" ,date_string_2= "+ab.getDateString2()+" ,archive_type= "+ab.getArchiveType()+" ,time_string= "+ab.getTimeString()+" ,archive_value = '"+ab.getArchiveValue()+"' where id_archive='"+ab.getIdArchive()+"'"; return this.getJdbcTemplate().update(sql); } catch(Exception e) { e.printStackTrace(); } return -1; }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
上一篇: 商城项目-已登录购物车