[英]Error occurred while connecting with azure database using JDBC driver
與Azure數據庫連接時發生錯誤
2016年7月7日8:42:32 PM 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.MD5和SHA1withRSA-> 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 $ TLSCo ntext別名:[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) {}
}
}
}
根據您的代碼,此問題是由於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舊門戶上的選項卡DASHBOARD
的右欄中Show connection strings
的鏈接中找到它,也可以在Azure新門戶上的顯示數據庫連接字符串的鏈接中找到它。
您可以參考我對SO線程的答案如何使用JDBC連接到Azure SQL以查看圖和代碼,或參閱官方文章https://azure.microsoft.com/zh-cn/documentation/articles/sql- database-develop-java-simple / 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.