First, before connecting to the database, you must ensure that SQL Server 2012 uses SQL Server authentication instead of Windows authentication. If the latter is selected during installation, reset as follows:

https://www.51it.wang/ll/13-1
After you ensure that SQL Server 2012 uses SQL Server authentication, start the following configuration:

1、 Since the default protocol is not enabled after SQL Server 2012 is installed, you should open the SQL Server Configuration Manager to enable:

1. After installing SQL Server 2012, run Start → All Programs → Microsoft SQL Server 2012 → Configuration Tools → SQL Server Configuration Manager, as shown in Figure 1 below:
 Java connects to SQL database through jdbc (SQL2012 example)

(Figure 1)

2. The open window is shown in the following figure. Find the SQL Server network configuration option in the left column, click its small arrow, and you will see the "Protocol of [Your Database Name]" (MSSQL protocol in the figure). Select it and see the right column. As shown in Figure 2 below:
 Java connects to SQL database through jdbc (SQL2012 example)

(Figure 2)

(1) If Named Pipes is not enabled, right-click → Enable

(2) Right click TCP/IP and select Enable

(3) Double click TCP/IP (right click Properties), select the "IP Address" tab in the pop-up window, set the [IP Address] of IP1 and IP10 to 127.0.0.1, and set the [Enabled] of all [IPx] to Yes. Then, drag the drop-down bar to the bottom, and set the [TCP Port] in IPAll to [1433]. The rest remains unchanged.

3. Restart the computer.

4. Next, use the telnet command to test whether port 1433 is open. First, ensure that the telnet service is enabled. (The specific method to start the telnet command can be described in Baidu: here is a brief description. Control panel - uninstall program - add and close windows functions, and check the telnet service and client protocols.)

5. After completing the previous step. Start menu → Run cmd → Input: telnet 127.0.0.1 1433, (note that there is a space between telnet and 127, and between 1 and 1433). As shown below
 Java connects to SQL database through jdbc (SQL2012 example)

6. If the message "Unable to open the connection to the host, connection failed on port 1433:" is prompted, it means that port 1433 is not open, and the above configuration needs to be repeated. If the connection is successful, the display is as shown in Figure 6:
 Java connects to SQL database through jdbc (SQL2012 example)

(Figure 6)


2、 Environment variable CLASSPATH configuration:

1. Download Microsoft JDBC Driver 4.0 for SQL Server

Download here: http://www.microsoft.com/zh-cn/download/details.aspx?id=11774

The SQL Servers supported by version 4.0 are:

Microsoft ® SQL Server ® two thousand and twelve

Microsoft ® SQL Server ® 2008 R2

Microsoft ® SQL Server ® two thousand and eight

Microsoft ® SQL Server ® two thousand and five

Microsoft ® SQL Azure

Download sqljdbc_4.0.2206.100_chs. tar. gz (2.2M) and unzip the file to get sqljdbc.jar and sqljdbc4.jar. If you use the jre1.7 version, ignore sqljdbc.jar (because it can't be used, and errors will occur if it is used with sqljdbc4.jar), leaving only sqljdbc4.jar.

The following settings are specific to jre1.7 (the settings below 1.7 should also be applicable. Self testing is not allowed for the next version):

Create a new folder on disk D and name it sqljdbc4. Copy sqljdbc4. jar into it.

(You can change your hobbies)

2. Right click My Computer → Properties → Advanced System Settings (Advanced) → Environment Variables, double-click the CLASSPATH variable in the system variable (or select CLASSPATH → Edit), and add "; D: sqljdbc4 sqljdbc4. jar" at the end (note that there is a; at the front). If there is no CLASSPATH, create a new CLASSPATH variable, And set its value to "D: sqljdbc4 sqljdbc4. jar". As shown in Figure 8:
 Java connects to SQL database through jdbc (SQL2012 example)

(Figure 8)

3. Continue to click OK to exit the environment variable configuration.

4. The next work is very important (because I have been struggling with this problem for a long time)!!

There are several points to note:

(1) We need to copy the sqljdbc4.jar class library file to the D: Program Files java jdk1.7.0 jre lib ext directory. (It depends on which disk you install on. If it is disk C, change the front D to C, the same below)

(2) We need to copy the sqljdbc4.jar class library file to the D: Program Files Java jre7 lib ext directory

The best thing is to copy a sqljdbc4.jar to jre7 lib ext as long as it is a jre folder!!

(3) If we use Tomcat as the server (I use Tomcat 8.0), we need to copy the sqljdbc4.jar class library file to the C: apache-tomcat-8.0 lib directory.

(4) If Tomcat is used as the server, we need to copy the sqljdbc4.jar class library file to the D: apache-tomcat-8.0 webapps XXX WEB-INF lib directory (XXX directory is my application)

Note that only sqljdbc4. jar!! If you copy sqljdbc.jar and sqljdbc4.jar together, even if you are all right, the problem of "this driver does not support JRE1.7, please use the class library of sqljdbc4.jar that supports JDBC 4.0" will continue to occur. Because the jdk selects sqljdbc. jar by default (as I mentioned earlier, only sqljdbc4. jar is left).


3、 Use Eclipse test to connect SQL Server 2012 database:

1. Open SQL Server 2012, create a new database Test in it, and then exit SQL Server 2012.

2. Run Eclipse and create a new Java Project named Test.

3. Right click src, select Build Path → Configure Build Path, select the Libraries tab on the right side of the open window, click Add External JARs, find the sqljdbc4.jar file and open it, and then click OK to complete the configuration of the build path. As shown in Figure 9:
 Java connects to SQL database through jdbc (SQL2012 example)

(Figure 9)
4. Create a new package pkg in Test and a new class Main in pkg. Enter the code as follows:

 package pkg; import java.sql.*; public class Main { public static void main(String [] args) { String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc: sqlserver://localhost:1433 ; DatabaseName=Test"; String userName="leizi"; String userPwd="liulei"; try { Class.forName(driverName); Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd); System. out. println ("Successfully connected to the database"); } catch(Exception e) { e.printStackTrace(); System. out. print ("Connection failed"); }     } }

Tips: If you want to operate on a table in the database, you need to do something like this: String sql="SELECT FROM [database name] [dbo]. [Table name] where xxx "; for example, String sql=" SELECT FROM [metro]. [dbo]. [4] Where xxx ". Note that the brackets are necessary and cannot be removed.

5. Right click and select run as -->Java Application. The following figure appears on the console and the connection is successful!
 Java connects to SQL database through jdbc (SQL2012 example)