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