The detailed process of Eclipse connecting to SQL Server Express database

Required software

1.SQL Server 2019 Express version
link: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads
Insert picture description here
2.SQL Server Management Studio
link: https://docs.microsoft.com/ zh-cn/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017
Insert picture description here

3. eclipse and related jdk, and configure

1. Download the Microsoft SQL Server JDBC driver

Download Microsoft SQL Server JDBC driver, you need to pay attention to his jdk version , it is best to use newer jdk, the official website to download the latest drivers;
such is my jdk jdk-14.0.1 , I downloaded the official website is Microsoft JDBC Driver 8.4 for SQL Server
link: https://docs.microsoft.com/zh-cn/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
Insert picture description here
I Download the zip file , and then decompress
Insert picture description here
Insert picture description here
it. Because my jdk is 14.0.1, I choose mssql-jdbc-8.4.1.jre14 , put this jar package aside, and use it later

2. Use SQL Server Management Studio to connect to the server and configure it for sa login

Insert picture description here
Insert picture description here
Just start to connect to the object resource manager, choose windows authentication as the authentication method , and set the sa login after the connection is successful

Insert picture description here
Find the security-login name, right-click on "sa" and select "Properties" to
Insert picture description here
set the password
for sa login mode and select " Enforce password enforcement policy "

Insert picture description here
The status bar
is allowed to connect to the database engine, choose to grant
login name, select Enable
and then determine
Insert picture description here
then the mouse on the server name on the map indicated by the arrow, right click and select Properties
Insert picture description here
Select Security This column
will Server Authentication Choose SQL server and windows authentication mode , and then confirm.
After the setting is complete, right-click the server name to restart.
You can log in using sa in the future.

3. Open the SQL Server configuration manager, enable the TCP/IP protocol, and set the port according to the situation

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Click SQL Server Network Configuration
enable TCP / IP protocol, and verify that the port is 1443
Insert picture description here
and then click SQL Native Client 11.0 Configuration
Click on the client protocol
Insert picture description here
allows client TCP / IP protocol enabled
by default port is 1433

4. Create a new database in SQL Server Management Studio and configure the eclipse jar package.

Insert picture description here
Insert picture description here
Create a new database named test in SQL Server,
then open eclipse, create a new Java project, create a new package, and create a new Java file in the package.
Insert picture description here
Then
the
mssql-jdbc-8.4.1.jre14 package obtained by decompressing the previously downloaded Microsoft SQL Server JDBC driver is now available.
Insert picture description here
Copy this jar package
Insert picture description here
back to eclipse, click the name of the Java project you created (mine is MyTest) with the left mouse button and paste it
directly with ctrl+v .
Insert picture description here
Get the picture above.
Right- click on mssql-jdbc-8.4.1.jre14.jar
and select Build Path→ Add to Build Path.
If you don’t see Add to Build Path, then select Configure Build Path , select the Libraries tab on the right side of the opened window, and then Click Add External JARs, find the mssql-jdbc-8.4.1.jre14 file to open, and then click OK.
Insert picture description here
To get the picture above, click Apply and Close.
The configuration is complete.

5. The last step is to write the following code in your Main.java and execute it.

package test;

import java.sql.*;
public class Main {
	  public static void main(String [] args)
	  {
	      String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	      String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test";//这里的test是你自己的数据库名称,上面我的是test
	      String userName="sa";//这里的sa要改为你的数据库用户名,默认是sa
	      String userPwd="123456";//这里123456要改为你的密码
	      try
	      {
	        Class.forName(driverName);
	        System.out.println("加载驱动成功!");
	      }catch(Exception e)
	      {
	        e.printStackTrace();
	        System.out.println("加载驱动失败!");
	      }
	      try{
	        Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
	        System.out.println("连接成功!");
	      }catch(Exception e)
	      {
	        e.printStackTrace();
	        System.out.print("连接失败!");
	      }        
	  }

}

This line of code String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test";
in 127.0.0.1:1443 is previously set in the TCP / IP protocol SQL Server network configuration.
This is a port, generally the default is 127.0.0.1:1443.
However, I found that my port is 0.0.0.0:1433, hahaha
If something goes wrong in the execution, you can manually check the port used by your SQL Server, modify the above code and it will be ok.

method:
Insert picture description here
Insert picture description here

Open the task manager , click on detailed information , find sqlservr.exe , and find that its PID is 16992.
(Or click on the service , find MSSQL$SQLEXPRESS , its PID is also 16992)
Insert picture description here
Then open cmd, enter the netstat -ano command, you can see that the port corresponding to PID 16992 is TCP 0.0.0.0:1433,
so you know the corresponding port, and substitute it The above code is executed.
Insert picture description here
success! ! !

Guess you like

Origin blog.csdn.net/Bitter_sweet_/article/details/109323269