达梦数据库-CLOB类型JAVA案例

最近遇见很多使用java项目从oracle 迁移到达梦后,读取大字段存在各种各种的问题,下面我整理一下CLOB类型java案例,防遗忘

普通代码中读取大字段

	// TODO Auto-generated method stub
		String driver = "dm.jdbc.driver.DmDriver";
		String url = "jdbc:dm://localhost:5236";
		String username = "TEST";
		String password = "123456789";
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection conn;

		conn = DriverManager.getConnection(url, username, password);
		//CREATE TABLE MENU(MENUID VARCHAR2(36),DOMOBJ CLOB)
	    String sql2 = "select * from MENU;";
		conn.setAutoCommit(false);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql2);
		//达梦默认是将clob以String类型读取出来
		while (rs.next()) {			 
			System.out.println(rs.getString(1)+"\t"+rs.getString(2));
		}
		conn.commit();

达梦数据库-CLOB类型JAVA案例_第1张图片

mybatis中对象类中返回大字段类型


对象类

public class MenuDto {
	String menuid;
	String domobj;
//	Map changedValues;
	public MenuDto() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public MenuDto(String menuid, String domobj) {
	super();
	this.menuid = menuid;
	this.domobj = domobj;
}

	public String getMenuid() {
		return menuid;
	}

	
	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}
	public String getDomebj() {
		return domobj;
	}
	public void setDomebj(String domobj) {
		this.domobj = domobj;
	}
	@Override
	public String toString() {
		return "MenuDto [menuid=" + menuid + ", domobj=" + domobj + "]";
	}
}
	<select id="selectMenuDto" resultType="MenuDto">
		      select * from MENU;
	</select>

mapper

	List<MenuDto> selectMenuDto();

Test

	@Test //查询所有
	public void selectMenuDto() {
		SqlSession session =sf.openSession(true);
		MenuDtoMapper mapper = session.getMapper(MenuDtoMapper.class);
		List<MenuDto> billTables=mapper.selectMenuDto();
		for (MenuDto billTable:billTables) {
			System.out.println(billTable.toString());		
		}
	}

连接信息,同普通代码中的一样按照string 读取

<dataSource type="com.dm.pojo.DruidDataSourceFactory">
				<property name="driverClass" value="dm.jdbc.driver.DmDriver"/>
				<property name="url" value="jdbc:dm://127.0.0.1:5236"/>
				<property name="username" value="TEST"/>
				<property name="password" value="123456789"/>
			dataSource>

截图为证
达梦数据库-CLOB类型JAVA案例_第2张图片

mybatis中Map中返回大字段类型

	List<HashMap<String,Object>> selectMenuMapDto();

test 方法

	public void selectMenuMapDto() {
		SqlSession session =sf.openSession(true);
		MenuDtoMapper mapper = session.getMapper(MenuDtoMapper.class);
		List<HashMap<String,Object>> billTables=mapper.selectMenuMapDto();
		for (HashMap<String,Object> billTable:billTables) {

			for (Map.Entry<String,Object>  bill:billTable.entrySet()   //遍历map的key集合 获取对应key的value
		             ) {
		            if ("MENUID".equals(bill.getKey())) {
		              System.out.println(bill.getValue().toString());
		            }else if ("DOMOBJ".equals(bill.getKey())) {
			              System.out.println((String)bill.getValue());
		            }
		        }		
		}
	}

	<select id="selectMenuMapDto" resultType="java.util.HashMap">
		      select MENUID,DOMOBJ from MENU;
	</select>

<dataSource type="com.dm.pojo.DruidDataSourceFactory">
				<property name="driverClass" value="dm.jdbc.driver.DmDriver"/>
				<property name="url" value="jdbc:dm://127.0.0.1:5236?clobAsString=1"/><!-- ?clobAsString=1 -->
				<property name="username" value="TEST"/>
				<property name="password" value="123456789"/>
			</dataSource>

截图为证
达梦数据库-CLOB类型JAVA案例_第3张图片
更多资讯请上达梦技术社区了解: https://eco.dameng.com

你可能感兴趣的:(达梦数据库,SQL,java,数据库)