1.通过jdbc方式存储clob类型数据
private void updateJdbcStudById(SqlMapClient sqlmap, Map inmap){ String infos = (String)inmap.get("infos"); //字符串类型的clob类型 Reader reader = new StringReader(infos); try { Connection conn = sqlmap.getDataSource().getConnection(); String sql = "update student set informations = ? where stuno = ?"; PreparedStatement ps = conn.prepareStatement(sql); //为clob类型赋值 ps.setCharacterStream(1, reader, infos.length()); ps.setString(2, (String)inmap.get("stuno")); ps.execute(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.通过ibatis存取clob类型数据
informations字段配置
private List<Student> queryStudentById(SqlMapClient sqlmap, String studno){ List<Student> lists = new ArrayList<Student>(); try { lists = sqlmap.queryForList("queryStdById", studno); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return lists; }
配置文件如下
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <!-- 指定映射,查询clob类型需要指定 --> <resultMap class="com.dto.Student" id="stumap"> <result property="stuno" column="STUNO"/> <result property="stuname" column="STUNAME"/> <result property="stuaddress" column="STUADDRESS"/> <result property="sex" column="SEX"/> <result property="notes" column="NOTES"/> <result property="informations" column="INFORMATIONS" javaType="String" jdbcType="CLOB"/> </resultMap> <!-- 查询学生信息 --> <select id="queryStdById" parameterClass="string" resultMap="stumap"> select stuno,stuname,stuaddress,sex,notes,informations from student where stuno = #stuno# </select> <update id="updatestdById" parameterClass="java.util.HashMap"> update student set informations = #infos,javaType=String,jdbcType=CLOB# where stuno = #stuno# </update> </sqlMap>
附相关的驱动包