Java connection to Azure SQL Database

Source: Internet
Author: User
Tags stub

Azure SQL database PAAs service on Azure allows users to quickly create and use SQL databases without worrying about the underlying backup, security, operations, and recovery tasks, this article provides a brief introduction to using Java programs to connect to SQL Azure databases.

  1. First sign in to your Azure management interface, choose Create a new SQL database, select Custom Create:

  2. Enter the name of the database, operations is created for the first time, so choose New SQL database server, for different service levels, please refer to the relevant documentation:

  3. Enter your database login name, password, select Database area, and of course your database should be in one with your application in order to get better performance, lower latency; For the version, we choose the V12 version, the new version has many features, choose OK, the database starts to create:

  4. When the database is created, click on the database name "Myjavatestdb" to enter the dashboard and the "quick glance" displays the options you currently have, including the string that shows the connection, the IP address you are allowed to access, and information about the database management tools:

    The connection string is displayed, the connection information is displayed in the main language, and you will see the following link:

  5. Based on the default database security design, in the initial case, the SQL Azure database does not allow any IP address to connect and access the database , only if you actively set the allowed IP address to connect to the database, so you need to know which server you connect to the database, and then click " Manage allowed IP addresses "The IP address that will require a database connection is set in order to be accessible:

  6. Once the above configuration is complete, open eclipse, assuming you have installed the latest Azure plugin for Eclipse plugin, (if you don't know how to install it, please refer to my blog to install Eclipse and set up Azure plug-in:/HTTP) cloudapps.blog.51cto.com/3136598/1699880), New Java project Azurelab:

  7. To connect to SQL Server, we have to add the JDBC driver to the azure for Eclipse, which actually contains the latest driver in the next step of creating a new project, select Libraries, and then in the next step, Select the Add Library:

    In the dialog box, select Microsoft JDBC driver 4.2 for SQL Server:

    Please note that there are some reports on the Internet that use the 4.0,4.1 driver to connect the V12 database, even if you choose not to apply a secure connection, there will be a forced use of SSL and reported not trusted error, so please connect to the V12 database as far as possible to select the 4.2 version of the driver. After the driver library is successfully added as follows:

  8. After the above setup is complete, there are two main ways to connect to SQL Azure database, one is a secure connection via SSL encryption, and a non-encrypted connection, so what is the best way to use these two connections?

    If you need to connect a SQL Azure database to a virtual machine and your database is on azure, and in a region, then it is recommended that you use non-encryption for better performance, if your application is to be connected through the Internet, such as from your data center, It is recommended that you connect using SSL.

  9. Using non-encrypted access to the database, you first need to set the whitelist as shown in # #, and then use the following code for unsecured connections:

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. Access the database using secure mode, you need to open encrypt=true, set trustservercertificate to True parameters, as shown in the following code:

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;" ;

        

...

}

There are no differences in how other programs are written, such as querying a database table:

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 connection to Azure SQL Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.