【数据库-Azure SQL Database】JDBC 如何连接 SQL Azure 数据库
2024-09-08 08:06:48
使用 JAVA 代码连接 Azure SQL Database 时产生了 SSL 错误,对于此问题大多数用户都是因为不知如何编写 JDBC 连接字符串而产生的,以下为相关示例代码,供您参考:
package sqldbtest;
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class sqldbconn {
public static void main(String[] args) {
// TODO Auto-generated method stub
String connectionString =
"jdbc:sqlserver://[server name].database.chinacloudapi.cn:1433;"
+ "database=[database name];"
+ "user==[database name]@ [server name];"
+ "password=xxxxxxxxxxx;"
+ "encrypt=true;"
+ "trustServerCertificate=true;"
+ "hostNameInCertificate=*.database.chinacloudapi.cn;"
+ "loginTimeout=30;";
// Declare the JDBC objects.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
PreparedStatement prepsInsertPerson = null;
PreparedStatement prepsUpdateAge = null;
try {
// INSERT two rows into the table.
// ...
// TRANSACTION and commit for an UPDATE.
// ...
// SELECT rows from the table.
// ...
connection = DriverManager.getConnection(connectionString);
System.out.println("Successful");
System.out.println("1");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connections after the data has been handled.
if (prepsInsertPerson != null) try { prepsInsertPerson.close(); } catch(Exception e) {}
if (prepsUpdateAge != null) try { prepsUpdateAge.close(); } catch(Exception e) {}
if (resultSet != null) try { resultSet.close(); } catch(Exception e) {}
if (statement != null) try { statement.close(); } catch(Exception e) {}
if (connection != null) try { connection.close(); } catch(Exception e) {}
}
}
}
[AZURE.NOTE] 完善连接字符串 connectionString 中相关参数,根据给出的格式进行修改。
最新文章
- PD4F将HTML转换为PDF乱码问题
- svn忽略eclipse自动生成的文件
- centos6.5编译安装git
- CUDA学习笔记(一)——CUDA编程模型
- BZOJ4377 : [POI2015]Kurs szybkiego czytania
- Matlab稀疏矩阵
- GuessNumber
- Almost Prime
- Android中使用HTTP服务
- MySQL 讨厌哪种类型的查询
- jdk1.7 变更
- (转)$.extend()方法和(function($){...})(jQuery)详解
- JS中this指向问题相关知识点及解析
- java多线程系列8 高级同步工具(2)CountDownLatch
- 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列
- IE (第二部分) 浏览器 中 关于浏览器模式和文本模式
- JAVA设计模式-设计原则
- Jade模板引擎使用详解
- Solr的入门知识
- [置顶] Android异步加载数据库和多线程编程
热门文章
- windows install JDK&;&;JRE
- HDU-2553
- D - Opponents
- Python使用Timer实现验证码功能
- [Xcode 实际操作]八、网络与多线程-(25)实现ShareSdk的社会化分享功能
- vue2.0.js基础开发使用心得(结合实际项目对数据的增删改查)
- Unrecogized font family ‘Ionicons’ 在ios上报错,android正常
- web项目数据存入mysql数据库中文乱码问题
- STP-5-STP配置及分析
- 最短路之SPFA(单源)HDU 2544