Apache DS管理的JAVA实现

<pre name="code" class="java">//设置连接
	LdapConnection connection = new LdapNetworkConnection("localhost", 10389);
//检测连接状态
	@Test
	public void testSimpleBindRequest() throws LdapException {
		connection.bind("uid=admin,ou=system", "secret");
		connection.unBind();
		Assert.assertFalse(connection.isConnected());
		Assert.assertFalse(connection.isAuthenticated());
	}
	//简单的查询
	@Test
	public void testSimplesearch() throws LdapException, CursorException,
			IOException {
		connection.bind("uid=admin,ou=system", "secret");
		EntryCursor cursor = connection.search("ou=system", "(objectclass=*)",
				SearchScope.ONELEVEL);
		while (cursor.next()) {
			Entry entry = cursor.get();
			Assert.assertNotNull(entry);
			System.out.println(entry);
		}
		cursor.close();
		connection.close();
	}
	//带有约束条件的查询
	@Test
	public void testComplexSearches() throws LdapException, CursorException,
			IOException {
		connection.bind("uid=admin,ou=system", "secret");
		SearchRequest req = new SearchRequestImpl();
		req.setScope(SearchScope.SUBTREE);
		req.addAttributes("*");
		req.setTimeLimit(0);
		req.setBase(new Dn("ou=system"));
		req.setFilter("(ou=consumers)");

		SearchCursor searchCursor = connection.search(req);

		while (searchCursor.next()) {
			Response response = searchCursor.get();

			if (response instanceof SearchResultEntry) {
				Entry resultEntry = ((SearchResultEntry) response).getEntry();
				Assert.assertNotNull(resultEntry);
				System.out.println(resultEntry);
			}
		}
		searchCursor.close();
		connection.close();
	}
	//添加条目
	@Test
	public void testAddLdif() throws LdapException, IOException {
		connection.bind("uid=admin,ou=system", "secret");
		connection.add(new DefaultEntry("cn=testadd,ou=system",
				"ObjectClass:top", "ObjectClass:person", "cn:testadd_cn",
				"sn:testadd_sn"));
		connection.close();

	}
	//以请求的方式添加条目
	@Test
	public void testAddWithControl() throws LdapException, IOException {
		connection.bind("uid=admin,ou=system", "secret");
		Entry entry = new DefaultEntry("cn=testadd2,ou=system",
				"ObjectClass:top", "ObjectClass:person", "sn:testadd_sn");

		AddRequest addRequest = new AddRequestImpl();
		addRequest.setEntry(entry);
		addRequest.addControl(new ManageDsaITImpl());
		AddResponse response = connection.add(addRequest);

		Assert.assertNotNull(response);
		Assert.assertEquals(ResultCodeEnum.SUCCESS, response.getLdapResult()
				.getResultCode());

		connection.close();
	}
	//删除条目
	@Test
	public void testDeleteLeafNode() throws LdapException, IOException {
		connection.bind("uid=admin,ou=system", "secret");
		connection.delete("cn=testadd,ou=system");
		connection.close();
	}
	//删除条目
	@Test
	public void testModify() throws LdapException, IOException {
		connection.bind("uid=admin,ou=system", "secret");
		Modification addedGivenName = new DefaultModification(
				ModificationOperation.ADD_ATTRIBUTE, "givenName");
		connection.modify("uid=Doe,dc=acme,dc=com",addedGivenName);
		connection.close();
	}


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我给你提供一个简单的org.apache.shardingsphere实现Oracle分库的示例。 1. 配置数据源 在org.apache.shardingsphere的配置文件中,配置Oracle数据库的数据源信息,例如: ```yaml # 数据源列表 dataSources: # 主库 master_ds: url: jdbc:oracle:thin:@//localhost:1521/ORCLCDB username: root password: root # 从库1 slave_ds1: url: jdbc:oracle:thin:@//localhost:1521/ORCLPDB1 username: root password: root # 从库2 slave_ds2: url: jdbc:oracle:thin:@//localhost:1521/ORCLPDB2 username: root password: root ``` 2. 配置分库策略 在配置文件中,设置分库规则,例如按照订单号的后两位进行分库,例如: ```yaml # 分片规则列表 shardingRule: # 分库规则 defaultDatabaseStrategy: inline: shardingColumn: order_id algorithmExpression: ds${order_id % 2 + 1} ``` 其中,`order_id`是订单号字段,这里的分库规则是将订单号的后两位进行取模运算,然后根据结果选择对应的数据源,这里有两个数据源,所以结果是`ds1`或`ds2`。 3. 配置表规则 在配置文件中,设置表规则,例如按照订单号的后两位进行分表,例如: ```yaml # 分片规则列表 shardingRule: # 分表规则 tables: order: actualDataNodes: ds${1..2}.order_${1..2} tableStrategy: inline: shardingColumn: order_id algorithmExpression: order_${order_id % 2 + 1} ``` 其中,`order`是表名,`ds${1..2}.order_${1..2}`是数据表的实际节点,这里会根据分库规则选择对应的数据源,然后根据分表规则选择对应的表,例如订单号为`1234`,则会选择`ds1.order_1`表。 4. 进行代码改造 在应用程序中,使用org.apache.shardingsphere提供的数据访问接口来访问数据库,例如: ```java DataSource dataSource = ShardingDataSourceFactory.createDataSource(yamlFile); try (Connection conn = dataSource.getConnection()) { String sql = "SELECT * FROM order WHERE order_id = ?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, orderId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { // 处理查询结果 } } } } ``` 这里使用了ShardingDataSourceFactory来创建数据源,然后使用标准的JDBC接口来进行数据访问,而不需要关心具体的分库分表规则。 以上就是一个简单的org.apache.shardingsphere实现Oracle分库的示例,具体实现方式可能因为实际场景和需求的不同而有所差异,需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值