简体   繁体   English

使用JDBC驱动程序与Azure数据库连接时发生错误

[英]Error occurred while connecting with azure database using JDBC driver

Error occurred while connecting with azure database 与Azure数据库连接时发生错误
Jul 07, 2016 8:42:32 PM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL INFO: java.security path: C:\\Program Files\\Java\\jdk1.8.0_60\\jre\\lib\\security Security providers: [SUN version 1.8, SunRsaSign version 1.8, SunEC version 1.8, SunJSSE version 1.8, SunJCE version 1.8, SunJGSS version 1.8, SunSASL version 1.8, XMLDSig version 1.8, SunPCSC version 1.8, SunMSCAPI version 1.8] SSLContext provider info: Sun JSSE provider(PKCS12, SunX509/PKIX key/trust factories, SSLv3/TLSv1/TLSv1.1/TLSv1.2) SSLContext provider services: [SunJSSE: KeyFactory.RSA -> sun.security.rsa.RSAKeyFactory aliases: [1.2.840.113549.1.1, OID.1.2.840.113549.1.1] , SunJSSE: KeyPairGenerator.RSA -> sun.security.rsa.RSAKeyPairGenerator aliases: [1.2.840.113549.1.1, OID.1.2.840.113549.1.1] , SunJSSE: Signature.MD2withRSA -> sun.security.rsa.RSASignature$MD2withRSA aliases: [1.2.840.113549.1.1.2, OID.1.2.840.113549.1.1.2] , SunJSSE: Signature.MD5withRSA -> sun.security.rsa.RSASignature$MD5withRSA aliases: [1.2.840.113549.1 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 aliases: [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 aliases: [PKIX] , SunJSSE: TrustManagerFactory.SunX509 -> sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory , SunJSSE: TrustManagerFactory.PKIX -> sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory aliases: [SunPKIX, X509, X.509] , SunJSSE: SSLContext.TLSv1 -> sun.security.ssl.SSLContextImpl$TLS10Context aliases: [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 .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 aliases: [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 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

Sample Code used 使用的示例代码

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) {}  
        }  
        }

}  

according to your code, the issue was caused by the incorrect connection string for Azure SQL Database. 根据您的代码,此问题是由于Azure SQL数据库的连接字符串不正确引起的。

The correct format of connection string for SQL Azure is like below. 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;

That you can find it at the link Show connection strings of the right bar of the tab DASHBOARD on Azure old portal, or see it at the link Show database connection strings on Azure new portal. 您可以在Azure旧门户上的选项卡DASHBOARD的右栏中Show connection strings的链接中找到它,也可以在Azure新门户上的显示数据库连接字符串的链接中找到它。

You can refer to my answer for the SO thread How to connect to Azure SQL with JDBC to see the figures and codes, or see the offical article https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple/ . 您可以参考我对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.

相关问题 使用 jdbc 驱动程序连接到 Cloud SQL 时出错 - Error while connecting to Cloud SQL using jdbc driver 将jdbc连接到Oracle数据库时出错 - Error while connecting jdbc to the oracle database 使用ojdbc6驱动程序通过jdbc连接到Oracle 8i数据库时出错 - Error when connecting via jdbc to Oracle 8i database using ojdbc6 driver 使用 JDBC 连接到 Athena 时找不到合适的驱动程序 - No suitable driver found for while connecting to Athena using JDBC 使用 jdbc 驱动程序连接到 MongoDB - Connecting to MongoDB using jdbc driver 使用JDBC连接到Azure SQL数据仓库时出现SQLException - SQLException while connecting to Azure SQL Data Warehouse using JDBC 将JDBC驱动程序连接到MSSQL数据库时出现问题 - Problems connecting JDBC driver to MSSQL database 连接相机时发生错误:0 - An error occurred while connecting to camera: 0 连接到数据库时,Class.forName(“ oracle.jdbc.driver.OracleDriver”)的实际用途是什么? - What is the actual use of Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database? 为什么我们在连接数据库时使用 Class.forName(“oracle.jdbc.driver.OracleDriver”)? - Why we use Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM