当前位置: 技术问答>java相关
java访问MS SQL Server的jdbc url怎么写?
来源: 互联网 发布时间:2017-03-10
本文导语: 想访问本地的ms sql server 数据库,该怎么写JDBC的url呀 最好有个例子,谢谢高手赏脸。。。。 | 依驱动程序的不同而不同的,如果你有SQL SERVER2000驱动的话, 照如下格式: Connecting to a Databa...
想访问本地的ms sql server 数据库,该怎么写JDBC的url呀
最好有个例子,谢谢高手赏脸。。。。
最好有个例子,谢谢高手赏脸。。。。
|
依驱动程序的不同而不同的,如果你有SQL SERVER2000驱动的话,
照如下格式:
Connecting to a Database
--------------------------------------------------------------------------------
Once the driver is installed, you can connect from your application to your database in two ways: with a connection URL through the JDBC driver manager, or with a JNDI data source. This quick start explains how to establish your database connection using a connection URL. See "Using the SQL Server 2000 Driver for JDBC" for details on using data sources.
You can connect through the JDBC driver manager with the method DriverManager.getConnection. This method uses a string containing a URL. Use the following steps to load the driver from your JDBC application.
1. Setting the Classpath
The SQL Server 2000 Driver for JDBC needs to be defined in your CLASSPATH variable. The CLASSPATH is the search string that your Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not on your CLASSPATH, you receive the error "class not found" when trying to load the driver. Set your system CLASSPATH to include the following entries, where install_dir is the path to your SQL Server 2000 Driver for JDBC installation directory:
install_dir/lib/msbase.jar
install_dir/lib/msutil.jar
install_dir/lib/mssqlserver.jar
Windows Example
CLASSPATH=.;c:Microsoft SQL Server 2000 Driver for JDBClibmsbase.jar;c:Microsoft SQL Server 2000 Driver for JDBClibmsutil.jar;c:Microsoft SQL Server 2000 Driver for JDBC
libmssqlserver.jar
UNIX Example
CLASSPATH=.;/home/user1/mssqlserver2000jdbc/lib/msbase.jar;/home/user1/mssqlserver2000jdbc/lib/msutil.jar;/home/user1/mssqlserver2000jdbc/lib/mssqlserver.jar
2. Registering the Driver
Registering the driver tells the JDBC driver manager which driver to load. When loading a driver using class.forName(), you must specify the name of the driver:
com.microsoft.jdbc.sqlserver.SQLServerDriver
For example:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
3. Passing the Connection URL
After registering the driver, you must pass your database connection information in the form of a connection URL. The following is a template URL for the SQL Server 2000 Driver for JDBC. Substitute the values specific to your database.
jdbc:microsoft:sqlserver://server_name:1433
For example, to specify a connection URL that includes the user ID "username" and the password "secret":
Connection conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://server1:1433","username","secret");
NOTE:
The server_name is an IP address or a host name, assuming that your network resolves host names to IP addresses. You can test this by using the ping command to access the host name and verifying that you receive a reply with the correct IP address.
The numeric value after the server name is the port number on which the database is listening. The values listed here are sample defaults. You should determine the port number that your database is using and substitute that value.
You can find the complete list of Connection URL parameters in "Connection String Properties" of this book.
照如下格式:
Connecting to a Database
--------------------------------------------------------------------------------
Once the driver is installed, you can connect from your application to your database in two ways: with a connection URL through the JDBC driver manager, or with a JNDI data source. This quick start explains how to establish your database connection using a connection URL. See "Using the SQL Server 2000 Driver for JDBC" for details on using data sources.
You can connect through the JDBC driver manager with the method DriverManager.getConnection. This method uses a string containing a URL. Use the following steps to load the driver from your JDBC application.
1. Setting the Classpath
The SQL Server 2000 Driver for JDBC needs to be defined in your CLASSPATH variable. The CLASSPATH is the search string that your Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not on your CLASSPATH, you receive the error "class not found" when trying to load the driver. Set your system CLASSPATH to include the following entries, where install_dir is the path to your SQL Server 2000 Driver for JDBC installation directory:
install_dir/lib/msbase.jar
install_dir/lib/msutil.jar
install_dir/lib/mssqlserver.jar
Windows Example
CLASSPATH=.;c:Microsoft SQL Server 2000 Driver for JDBClibmsbase.jar;c:Microsoft SQL Server 2000 Driver for JDBClibmsutil.jar;c:Microsoft SQL Server 2000 Driver for JDBC
libmssqlserver.jar
UNIX Example
CLASSPATH=.;/home/user1/mssqlserver2000jdbc/lib/msbase.jar;/home/user1/mssqlserver2000jdbc/lib/msutil.jar;/home/user1/mssqlserver2000jdbc/lib/mssqlserver.jar
2. Registering the Driver
Registering the driver tells the JDBC driver manager which driver to load. When loading a driver using class.forName(), you must specify the name of the driver:
com.microsoft.jdbc.sqlserver.SQLServerDriver
For example:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
3. Passing the Connection URL
After registering the driver, you must pass your database connection information in the form of a connection URL. The following is a template URL for the SQL Server 2000 Driver for JDBC. Substitute the values specific to your database.
jdbc:microsoft:sqlserver://server_name:1433
For example, to specify a connection URL that includes the user ID "username" and the password "secret":
Connection conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://server1:1433","username","secret");
NOTE:
The server_name is an IP address or a host name, assuming that your network resolves host names to IP addresses. You can test this by using the ping command to access the host name and verifying that you receive a reply with the correct IP address.
The numeric value after the server name is the port number on which the database is listening. The values listed here are sample defaults. You should determine the port number that your database is using and substitute that value.
You can find the complete list of Connection URL parameters in "Connection String Properties" of this book.
|
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sourceURL=
"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=NorthWind";
~~~~~~~~~~(或者换为ip)
Connection conn=DriverManager.getConnection(sourceURL,"test","test");
String sourceURL=
"jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=NorthWind";
~~~~~~~~~~(或者换为ip)
Connection conn=DriverManager.getConnection(sourceURL,"test","test");