Java via JDBC Link SQLServer2012

Source: Internet
Author: User

    Original

Below please word to see, once again set success, than you set dozens of times failed, time will be much less.

First, you must ensure that SQL Server 2012 uses SQL Server Authentication instead of Windows authentication before you connect to the database. If the latter is selected at the time of installation, reset the following:

http://blog.163.com/jackie_howe/blog/static/19949134720122261121214/

When you are sure that SQL Server 2012 is in SQL Server authentication mode, start with the following configuration:

One, because SQL Server 2012 is installed, the default protocol is not turned on, so to open the SQL Server Configuration Manager opens:

1. After installing SQL Server 2012, run start → All Programs → Microsoft SQL server → configuration Tool →sql Server Configuration Manager, such as 1 Example:


Figure 1

2. Open the window as shown in. Find the SQL Server network configuration option in the left column, click on its small arrow and you will see the "Your database name" protocol (the ERIC2012 protocol in the diagram), select it, and look at the right column. As shown in 2:

Figure 2

(1) If named Pipes is not enabled, right click → enable

(2) Right-click TCP/IP, 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. Next, drag the drop-down bar to the bottom to set the TCP port in IPAll to "1433" and the rest unchanged. As shown in Fig. 3 and Figure 4:

Figure 3

Figure 4

3. Restart the computer.

4. Next, use the Telnet command to test whether port 1433 is open. The first step is to ensure that the Telnet service is turned on. Here's how to turn on win7 telnet:

http://blog.163.com/jackie_howe/blog/static/199491347201251723939691/

5, after the completion of the previous step. Start Menu → run cmd → input: telnet 127.0.0.1 1433, (note that there is a space between Telnet and 127, there is a space between 1 and 1433). such as:

Figure 5

6, if the prompt "cannot open the connection to the host, the Port 1433: connection Failed", then 1433 port is not open, you need to re-configure the above. If the connection is successful, the display is shown in 6:

Figure 6

Second, the 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

SQL Server supported by version 4.0 is:

Microsoft®sql server®2012

Microsoft®sql server®2008 R2

Microsoft®sql server®2008

Microsoft®sql server®2005

Microsoft®sql Azure

Download sqljdbc_4.0.2206.100_chs.tar.gz (2.2M), unzip the file, get Sqljdbc.jar and Sqljdbc4.jar. If you're using the jre1.7 version, ignore Sqljdbc.jar (because it doesn't work, and if it goes wrong with Sqljdbc4.jar), leave only Sqljdbc4.jar.

The following settings are for the jre1.7 version (1.7 should also apply):

Create a new folder on the D disk, named Sqljdbc4, and copy the Sqljdbc4.jar into one.

Figure 7

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), append to the last face ";D: \sqljdbc4 \sqljdbc4.jar" (note first ; ) If classpath is not present, create a new CLASSPATH variable and set its value to "D:\SQLJDBC4 \ Sqljdbc4.jar ". 8 is shown below:

Figure 8

3 . Click OK continuously to exit the environment variable configuration.

4, the next work is very important (because did not do I struggle for a few days did not succeed)!!

There are several places 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. (see which disk you installed, if it is C, then the first d is changed to C, the same as below)

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

Best of all, as long as the JRE folder, copy a Sqljdbc4.jar to Jre7\lib\ext!!

(3) If you are using Tomcat as a server (I am using TOMCAT7), then we need to copy the Sqljdbc4.jar class library files to the C:\apache-tomcat-7.0.11\lib directory.

(4) If you are using Tomcat as a server, then we need to copy the Sqljdbc4.jar class library file to D:\apache-tomcat-7.0.11\webapps\gaofei\WEB-INF\ Lib directory (Gaofei directory is my app, this path is sure you will see)

Attention, only Sqljdbc4.jar !! If you put Sqljdbc.jar and Sqljdbc4.jar together, then even if you do it all right, it will persist. "This driver does not support JRE1.7, please use the Sqljdbc4.jar class library that supports JDBC4.0" issue. Because the JDK chooses Sqljdbc.jar by default (as I mentioned earlier, leaving only Sqljdbc4.jar).

Third, use the Eclipse test to connect to the 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 on src, select build path → Configure build path, select the libraries label to the right of the open window, and click Add External JARs to find Sqljdbc4.jar The file and open it, then click OK to complete the configuration of the build path. 9 (I am a Chinese version of):

Figure 9

4, in test new package pkg, in the pkg of a new class main, in which the code is entered as follows:

packagepkg;

importjava.sql.*;

Publicclass Main {

publicstatic void Main (String [] args)

{

stringdrivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";

stringdburl= "Jdbc:sqlserver://localhost:1433;databasename= your database name";

String username= "Fill in your username, mine is sa";

String userpwd= "Fill in your password";

Try

 {

Class.forName (drivername);

connectiondbconn=drivermanager.getconnection (dburl,username,userpwd);

System.out.println ("Connection to Database Success");

 }

catch (Exception e)

 {

e.printstacktrace ();

System.out.print ("Connection Failed");

 }    

}

}

Tip: If you want to manipulate a table in a database, you need to do this like this: String sql = "select* from [database name].[ DBO]. [table name] where xxx "; for example, string sql = "select* from [metro].[ DBO]. [4] wherexxx " . Note that the brackets are necessary and cannot be removed.

5, right click, select Run As-->java application, the console appears the connection is successful!

Figure 10

It took one hours to edit the article, the first post!

Java via JDBC Link SQLServer2012

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.