Establish a connection with SQL Server through JDBC

Source: Internet
Author: User

Establish a connection with SQL Server through JDBC

Keywords

Sub-protocol, server address, port number, instance, port number and instance, connection Property

Prone to Problems

    • The port number is used together with the instance. Here, the port number is used first, and the instance configuration is ignored.
    • An uncertain port number is specified.

Summary

    • When using JDBC to connect to SQL Server, the port number is not particularly important.
    • Note that you cannot specify the port number for a specific instance.
    • The http://msdn.microsoft.com/zh-cn/library/ms378428 (En-US, SQL .90). aspx

ExampleCode

/*** Get MSSQL database connection * @ Param connurl connection string * @ return rtnconn rtnconnection */public static connection createconn4mssql (string connurl) {connection conn = NULL; try {class. forname (ms_driver); Conn = drivermanager. getconnection (connurl);} catch (classnotfoundexception cnfe) {Throw new runtimeexception (" database driver loading failed", cnfe. fillinstacktrace ();} catch (sqlexception sqle) {Throw new runtimeexception ("sqlserver database connection failed", sqle. fillinstacktrace ();} return conn;} public static void main (string [] ARGs) {string [] mssqlurls = {"JDBC: sqlserver: // 192.168.3.228: 1433; databasename = nsdev151; user = nsdev151; Password = 800224 "," JDBC: sqlserver: // 192.168.0.16; databasename = nename; user = nsag; Password = 11111111; InstanceName = sql2005 ", "JDBC: sqlserver: // 192.168.3.228: 1433; databasename = nsdev169; user = nsdev169; Password = 186958"}; connection conn = NULL; For (string TMP: mssqlurls) {try {system. out. println (TMP); Conn = dbutils. createconn4mssql (TMP);} catch (runtimeexception e) {e. printstacktrace ();} finally {dbutils. close (conn );}}}

Related Article

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.