SDK JDBC多种方式接入AZURE SQL DATABASE JAVA版

2 篇文章 0 订阅

本文阐述使用JDBC接入AZURE SQL 数据库
笔者认为AZURE云上的AZURE SQL和SQL SERVER是很相似的,在普通的账号密码情况下JDBC字符串都是一致的。
下来全部说明官方的多种连接方式,参考官方:
https://learn.microsoft.com/zh-cn/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver16#set-kerberos-ticket-on-windows-linux-and-macos
在这里插入图片描述

在这里插入图片描述

a.SqlPassword

SQL SERVER验证
使用数据库用户名+密码

String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=dbname;user=user;password=pass;authentication=SqlPassword";

b.ActiveDirectoryPassword

采用Azure AD用户名+密码验证
首先设置AD账号为数据库管理员
在这里插入图片描述
然后

String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=db;user=xx@xx.onmicrosoft.com;password=xx;authentication=ActiveDirectoryPassword";

e.ActiveDirectoryMSI
采用AZURE内部得服务器,等验证
先设置服务器为数据库的管理员
在这里插入图片描述

再进行请求,MSIClientId是服务器的id,是不必要的

String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=whalead;MSIClientId=xxx;authentication=ActiveDirectoryMSI";


f.ActiveDirectoryServicePrincipal
采用应用和密钥进行验证
先设置应用为数据库的管理员
在这里插入图片描述
给应用设置密钥
在这里插入图片描述

String sql="jdbc:sqlserver://xx.database.windows.net:1433;database=xxdb;authentication=ActiveDirectoryServicePrincipal;user=cliendid;password=secret;";

g.accessToken
采用使用和密钥去获取token进行验证,步骤和principal一致,代码不一样,不能使用jdbc连接
多了一步要获取
在这里插入图片描述

String spn = "https://database.windows.net/";
		String stsurl = "https://login.microsoftonline.com/xxxx/oauth2/v2.0/token"; // Replace with your STS URL.
		String clientId = "xxx"; // Replace with your client ID.
		String clientSecret = "xxx"; // Replace with your client secret.

		String scope = spn +  "/.default";
		Set<String> scopes = new HashSet<>();
		scopes.add(scope);

		ExecutorService executorService = Executors.newSingleThreadExecutor();
		IClientCredential credential = ClientCredentialFactory.createFromSecret(clientSecret);
		ConfidentialClientApplication clientApplication = ConfidentialClientApplication
				.builder(clientId, credential).executorService(executorService).authority(stsurl).build();
		CompletableFuture<IAuthenticationResult> future = clientApplication
				.acquireToken(ClientCredentialParameters.builder(scopes).build());
		IAuthenticationResult authenticationResult = future.get();
		String accessToken = authenticationResult.accessToken();

		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!Access Token: " + accessToken);
		String sql="jdbc:sqlserver://xxx.database.windows.net:1433;database=xxx;user=xxx;accessToken="+accessToken;
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!Access Token: " + sql);

		// Connect with the access token.
		SQLServerDataSource ds = new SQLServerDataSource();

		ds.setServerName("xxx.database.windows.net"); // Replace with your server name.
		ds.setDatabaseName("xxx"); // Replace with your database name.
		ds.setAccessToken(accessToken);

		try (Connection connection = ds.getConnection();
			 Statement stmt = connection.createStatement();
			 ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
			if (rs.next()) {
				System.out.println("You have successfully logged on as: " + rs.getString(1));
			}
		}

未实践的

c.ActiveDirectoryIntegrated
WINDOWS下是使用mssql-jdbc_auth--.dll 进行验证

Linux下采用
在 Windows、Linux 和 macOS 上设置 Kerberos 票证

d.ActiveDirectoryInteractive
使用时,弹出网页让用户自行三方验证

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你需要在Java中使用MySQL的Geometry类型字段,可以使用MySQL提供的Java SDK - MySQL Connector/J,它是Java访问MySQL数据库的官方驱动。 MySQL Connector/J提供了对Geometry类型字段的支持,可以将Geometry类型的数据转换为Java中的对象,例如Point、LineString、Polygon等等。对于Geometry类型的数据的操作,可以使用JTS Topology Suite库进行处理,也可以使用MySQL提供的Spatial Extensions中的函数来进行处理。 以下是使用MySQL Connector/J和JTS Topology Suite来读取Geometry类型字段的示例代码: ```java import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class GeometryExample { public static void main(String[] args) { try { // 加载MySQL驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 连接MySQL数据库 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb?useSSL=false", "root", "password"); // 执行查询语句,查询Geometry类型的字段 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT geom FROM mytable WHERE id=1"); // 读取查询结果 if (rs.next()) { // 将Geometry类型的数据转换为JTS Geometry对象 String wkt = rs.getString(1); Geometry geom = new WKTReader().read(wkt); // 对Geometry对象进行操作 double area = geom.getArea(); System.out.println("Geometry area: " + area); } // 关闭数据库连接 rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException | SQLException | ParseException e) { e.printStackTrace(); } } } ``` 以上示例代码演示了如何使用MySQL Connector/J和JTS Topology Suite来读取Geometry类型字段并进行操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值