Java connection SQL Server database considerations

Source: Internet
Author: User
Tags mssqlserver port number

In a Java file, when you connect to a SQL Server database, you need a driver file to connect to the database.

Copy a file Jtds-0.6.jar in the Lib folder under the path where you install the JDK, and set the classpath in the environment variable, for example: Your JDK is installed in C:\JDK, then the path of the environment variable classpath should be set to C:\jdk\lib\ Jtds-0.6.jar. (Environment variables know how to set this up?) In the properties of my computer, advanced. Multiple paths are separated by semicolons. )

The SQL Server connection driver file Jtds-0.6.jar is provided here, and the download is unzipped and copied to the Lib folder under the JDK path.

Specific application in the file:

import java.sql.*;
String url="jdbc:jtds:sqlserver://localhost:1433/‘数据库名´";
String driver="net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver);
Connection conn=DriverManager.getConnection(url,"用户名","密码");

You can connect to your database.

1433 is the default port number for SQL Server, and if you want to connect to another machine, change the localhost to another machine's IP address, such as 192.168.0.101.

Where the database name is the name of the database you created, the username and password is the username and password that you logged into SQL Server.

When someone installs SQL Server with Windows Authentication, both the username and password are empty. If you cannot log in, or you want to use SQL authentication, you need to modify the registry. Open the Regedit in the run, and then look for LoginMode (specific location: Hkey_local_machine\software\microsoft\mssqlserver\

Mssqlserver\), the value of Windows authentication is only 1, and the mixed authentication value is 2. As long as the 1 is changed to 2, you can use SQL authentication, the user name is ' sa ', and the password is blank.

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.