当前位置:Gxlcms > 数据库问题 > Java连接Azure SQL Database

Java连接Azure SQL Database

时间:2021-07-01 10:21:17 帮助过:2人阅读

,只有你主动设置了允许的IP地址,才能连接数据库,所以你需要了解到你的哪个服务器会连接数据库,然后点击"管理允许的IP地址"将需要进行数据库连接的IP地址设置进去才可以访问:

技术分享

  • 上述配置完成后,打开Eclipse,假设你已经安装了最新的Azure plugin for Eclipse插件,(如果你不知道如何安装,请参考我的博客安装Eclipse并设置Azure插件:http://cloudapps.blog.51cto.com/3136598/1699880),新建Java项目azurelab:

    技术分享

  • 要连接到SQLServer,我们必须要添加SQL Server的JDBC driver,在Azure for Eclipse的插件中,实际已经包含了最新的驱动,在创建新项目的下一步,选择Libraries,然后在下一步中,选择Add Library:

    技术分享

    在对话框中,选择Microsoft JDBC driver 4.2 for SQL Server:

    技术分享

     

    请注意,网上看到一些报告说使用4.0,4.1驱动连接v12数据库,即使选择不适用安全连接,也会出现强制使用SSL并报不被信任错误等问题,所以请大家连接v12数据库尽量选择4.2版本的驱动。成功添加驱动库后如下如所示:

    技术分享

  • 在上述设置完成后,连接SQL Azure database主要有两种连接方式,一种是通过SSL加密的安全连接方式,一种是非加密的连接方式,那么这两种连接方式都在什么场景下使用比较合适呢?

    如果你需要连接SQL Azure数据库的虚拟机和你的数据库都在Azure上,并且在一个地区(region),那么建议你使用非加密方式具有更好的性能;如果你的应用程序要透过互联网,比如从你的数据中心连接,那么建议你使用SSL的方式连接。

     

  • 使用非加密方式访问数据库,首先需要设置白名单如#5所示,然后使用如下代码进行非安全连接:

     

  • package com.azurelabs.china.sqlserver;

     

    import java.sql.*;

    import com.microsoft.sqlserver.jdbc.*;

     

     

    public class ConnectSQL {

     

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            String connectionString =

         "jdbc:sqlserver://YOURSERVER.database.chinacloudapi.cn:1433;"

         + "database=YOURDB;"

         + "user=YOURUSER;"

         + "password=YOURPASS;"

         + "loginTimeout=30;";

            

            ……

    }

    10. 使用安全方式访问数据库,需要打开encrypt=true,设置trustServerCertificate true 等参数,如下代码所示:

    package com.azurelabs.china.sqlserver;

    import java.sql.*;

    import com.microsoft.sqlserver.jdbc.*;

    public class ConnectSQL {

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            String connectionString =

         "jdbc:sqlserver://YOURSERVER.database.chinacloudapi.cn:1433;"

         + "database=YOURDB;"

         + "user=YOURUSER;"

         + "password=YOURPASS;"

         + "encrypt=true;"

         + "trustServerCertificate=true;"

         + "hostNameInCertificate=*.database.chinacloudapi.cn;"

         + "loginTimeout=30;";

            

            ……

    }

     

    其他的程序写法没什么差别,例如查询一个数据库表:

    Connection connection = null;

    Statement statement = null;

    ResultSet resultSet = null;

    PreparedStatement prepsInsertPerson = null;

    PreparedStatement prepsUpdateAge = null;

    try {

    connection = DriverManager.getConnection(connectionString);

    // SELECT rows from the table.

    String selectSQL="select id,name,age from dbo.testcon";

    statement = connection.createStatement();

    resultSet = statement.executeQuery(selectSQL);

    // Iterate through the result set and print the attributes.

    while (resultSet.next()) {

    System.out.println(resultSet.getString(1) + " "

    + resultSet.getString(2)+ " "

    + resultSet.getString(3));

    }

    }

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

    }

        }

    Java连接Azure SQL Database

    标签:

    人气教程排行