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.