Java through JDBC connection SQL SERVER 2017

1. First, we download the Microsoft JDBC driver package

https://www.microsoft.com/en-us/download/details.aspx?id=11774

2. Download good we found it to be a self-extracting program opens, click Unzip unzip it, and find sqljdbc42.jar this file, use this file to configure the environment variables, configuration as shown below:

Configuration information (environment variable name: CLASSPATH, environment variables Address: is the location of the file, arbitrary)

3. After configuration is complete, we open our sqlserver configuration manager to configure our connection port

Find Sql Server Network Configuration -> MSSQLSERVER agreement, if TCP / IP is closed, please open it, double-click the TCP / IP protocol, select the tab IP address and the IP address IP1 IP10 changed to 127.0.0.1, port reform into 1433, what activities are changed, the other default, then IPAII change the TCP port 1433, modify the look with TelnetOR netstat -an command after completion of the port is not open, then the connection is successful, the next user configuration sqlserver landing inside, you can also add your own one, I here use the default sa

Note: Before installing the database, to select a custom installation, use Windows and SQL login mixed mode, manual configuration database account name and password

4. landing database, as follows: Security -> login -> right-click and select Properties sa, set their own password, select Properties from the right-click the server circle marked with the following

After having all the above configuration Next we write the code, open the eclipse, the new java project, right-click on src, select the build path, and then point to the last option, the sqljdbc42.jar import into it.

There will be the absence of jaxb-api-2.2.7.jar in some cases, you can download to import into.

Finally, paste the following code can be run successfully connect to the database:

package test;

import java.sql.*;

public class Demo {
    public static void main(String[] args) {
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
        String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=";//数据源
        String Name="";
        String Pwd="";
        try {
            Class.forName(driverName);
            Connection conn=DriverManager.getConnection(dbURL,Name,Pwd);
            System.out.println("连接数据库成功");
            } catch(Exception e) {
                e.printStackTrace();
                System.out.println("连接失败");
        }
    }
}

Guess you like

Origin www.cnblogs.com/whatsabc/p/11483231.html