连接azure数据库时发生错误
2016年7月7日下午8:42:32 com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
信息:java.security路径:C:\ Program Files \ Java \ jdk1.8.0_60 \ jre \ lib \ security
安全提供商:[SUN版本1.8,SunRsaSign版本1.8,SunEC版本1.8,SunJSSE版本1.8,SunJCE版本1.8,SunJGSS版本1.8,SunSASL版本1.8,XMLDSig版本1.8,SunPCSC版本1.8,SunMSCAPI版本1.8]
SSLContext提供程序信息:Sun JSSE提供程序(PKCS12,SunX509 / PKIX密钥/信任工厂,SSLv3 / TLSv1 / TLSv1.1 / TLSv1.2)
SSLContext提供商服务:
[SunJSSE:KeyFactory.RSA - > sun.security.rsa.RSAKeyFactory
别名:[1.2.840.113549.1.1,OID.1.2.840.113549.1.1]
,SunJSSE:KeyPairGenerator.RSA - > sun.security.rsa.RSAKeyPairGenerator
别名:[1.2.840.113549.1.1,OID.1.2.840.113549.1.1]
,SunJSSE:Signature.MD2withRSA - > sun.security.rsa.RSASignature $ MD2withRSA
别名:[1.2.840.113549.1.1.2,OID.1.2.840.113549.1.1.2]
,SunJSSE:Signature.MD5withRSA - > sun.security.rsa.RSASignature $ MD5withRSA
别名:[1.2.840.113549.1.1.4,OID.1.2.840.113549.1.1.4]
,SunJSSE:Signature.SHA1withRSA - > sun.security.rsa.RSASignature $ SHA1withRSA
别名:[1.2.840.113549.1.1.5,OID.1.2.840.113549.1.1.5,1.3.14.3.2.29,OID.1.3.14.3.2.29]
,SunJSSE:Signature.MD5andSHA1withRSA - > sun.security.ssl.RSASignature
,SunJSSE:KeyManagerFactory.SunX509 - > sun.security.ssl.KeyManagerFactoryImpl $ SunX509
,SunJSSE:KeyManagerFactory.NewSunX509 - > sun.security.ssl.KeyManagerFactoryImpl $ X509
别名:[PKIX]
,SunJSSE:TrustManagerFactory.SunX509 - > sun.security.ssl.TrustManagerFactoryImpl $ SimpleFactory
,SunJSSE:TrustManagerFactory.PKIX - > sun.security.ssl.TrustManagerFactoryImpl $ PKIXFactory
别名:[SunPKIX,X509,X.509]
,SunJSSE:SSLContext.TLSv1 - > sun.security.ssl.SSLContextImpl $ TLS10Context
别名:[SSLv3]
,SunJSSE:SSLContext.TLSv1.1 - > sun.security.ssl.SSLContextImpl $ TLS11Context
,SunJSSE:SSLContext.TLSv1.2 - > sun.security.ssl.SSLContextImpl $ TLS12Context
,SunJSSE:SSLContext.TLS - > sun.security.ssl.SSLContextImpl $ TLSContext
别名:[SSL]
,SunJSSE:SSLContext.Default - > sun.security.ssl.SSLContextImpl $ DefaultSSLContext
,SunJSSE:KeyStore.PKCS12 - > sun.security.pkcs12.PKCS12KeyStore
]
java.ext.dirs:C:\ Program Files \ Java \ jdk1.8.0_60 \ jre \ lib \ ext; C:\ WINDOWS \ Sun \ Java \ lib \ ext
使用的示例代码
package UtilityCreateSensorData;
// Use the JDBC driver
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class SQLDatabaseConnection {
// Connect to your database.
// Replace server name, username, and password with your credentials
public void InsertData(String Value1,String Value2,String Value3,int Value4) {
String connectionString ="jdbc:sqlserver://test.database.windows.net:1433;database=test;user=TestUsername;password=TestPassword";
// Declare the JDBC objects.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
PreparedStatement prepsInsertProduct = null;
try {
connection = DriverManager.getConnection(connectionString);
// Create and execute an INSERT SQL prepared statement.
String insertSql = "INSERT INTO [dbo].[tblAutomationTest] (DESC,MessageStart,MessageEnd,SenseCount) VALUES " + "(Value1, Value2, Value3, Value4);";
prepsInsertProduct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);
prepsInsertProduct.execute();
// Retrieve the generated key from the insert.
resultSet = prepsInsertProduct.getGeneratedKeys();
// Print the ID of the inserted row.
while (resultSet.next()) { System.out.println("Generated: " + resultSet.getString(1));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) try { connection.close(); } catch(Exception e) {}
// Close the connections after the data has been handled.
if (prepsInsertProduct != null) try { prepsInsertProduct.close(); } catch(Exception e) {}
if (resultSet != null) try { resultSet.close(); } catch(Exception e) {}
if (statement != null) try { statement.close(); } catch(Exception e) {}
}
}
}
答案 0 :(得分:0)
根据您的代码,问题是由Azure SQL数据库的错误连接字符串引起的。
SQL Azure的正确连接字符串格式如下所示。
jdbc:sqlserver://<hostname>.database.windows.net:1433;database=<database-name>;user=<username>@<hostname>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
您可以在Azure旧门户网站上Show connection strings
选项卡右侧栏的链接DASHBOARD
找到它,或者在Azure新门户网站上显示数据库连接字符串链接中查看。
您可以参考我对SO帖子How to connect to Azure SQL with JDBC的回答来查看数字和代码,或查看官方文章https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple/。