Eclipse connects to SQL Server 2012 databases

Source: Internet
Author: User
Tags exit microsoft sql server table name tomcat

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

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

1. After installing SQL Server 2012, run start → All programs →microsoft SQL Server 2012→ Configuration Tool →sql Server Configuration Manager, as shown in Figure 1 below:

Figure 1

2, the open window as shown in the following figure. Find the SQL Server network configuration option in the left column and click on its small arrow to see "Your Database Name" protocol (the ERIC2012 protocol), select it, and look at the right column. As shown in Figure 2 below:

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 → attribute), 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 all "IPx" enabled to be. Next, drag the Drop-down bar to the bottom, set the TCP port in the Ipall to 1433, and the rest unchanged. As shown in Figure 3 and figure 4 below:

Figure 3

Figure 4

3, restart the computer.

4. Next, use the Telnet command to test that port 1433 is open. The first step is to ensure that the Telnet service is turned on. The way to open Win7 Telnet is here:

5. After the completion of the previous step. Start menu → run cmd→ input: Telnet 127.0.0.1 1433, (note that there are spaces between Telnet and 127, 1 and 1433 have spaces). The following figure:

Figure 5

6, if the prompt "can not open to the host of the connection, in port 1433: Connection Failed", then the 1433 port is not open, you need to restart the above configuration. If the connection succeeds, it appears as shown in Figure 6:

Figure 6

Second, the environment variable CLASSPATH configuration:

1. Download Microsoft JDBC Driver 4.0 for SQL Server

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), extract files, get Sqljdbc.jar and Sqljdbc4.jar. If you are using a jre1.7 version, ignore the Sqljdbc.jar (because it won't work, and if it will go wrong with Sqljdbc4.jar), leave only the Sqljdbc4.jar.

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

Create a new folder in D, named Sqljdbc4, and copy the Sqljdbc4.jar one in.

Figure 7

2, right click My computer → properties → advanced system settings (advanced) → environment variables, in the system variable double-click the CLASSPATH variable (or select Classpath → edit), appended to the last ";D: Sqljdbc4 Sqljdbc4.jar" (Note that there is a front;) If no classpath exists, create a new CLASSPATH variable and set its value to "D:sqljdbc4 Sqljdbc4.jar". As shown in Figure 8:

Figure 8

3, continuous Click to determine to exit the environment variable configuration.

4, the next work is very important (because did not do I struggled for several 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 Filesjavajdk1.7.0jrelibext directory. (See which disk you install, if it is C, then the first d change to C, the same below)

(2) We need to copy the Sqljdbc4.jar class library file to the D:program Filesjavajre7libext directory

Preferably, as long as the JRE folder, copy a Sqljdbc4.jar to Jre7libext!!

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

(4) If you are using Tomcat to do the server, then we need to copy the Sqljdbc4.jar class library files to the D:apache-tomcat-7.0.11webappsgaofeiweb-inflib directory (Gaofei directory is my application, This path I believe you will understand)

Attention, only Sqljdbc4.jar!! If you put Sqljdbc.jar and Sqljdbc4.jar together, that way, even if you're all right, there's going to be a problem with "this driver does not support JRE1.7, please use the Sqljdbc4.jar class library that supports JDBC4.0." Because the JDK chose Sqljdbc.jar by default (as I mentioned before, leave only Sqljdbc4.jar).

Use eclipse testing to connect to a 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, locate the Sqljdbc4.jar file and open it, and then click OK completes the configuration of the build path. As shown in Figure 9 (I am a Chinese version):

Figure 9

4. In test, create a new package pkg, create a new class in Pkg, in which you enter the following code:

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= your database name";

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

String userpwd= "Fill in your password";

Try

{

Class.forName (drivername);

Connection dbconn=drivermanager.getconnection (DBURL,USERNAME,USERPWD);

SYSTEM.OUT.PRINTLN ("Connection database succeeded");

}

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 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, click the right button, choose Run As-->java application, the console appears the next picture is connected successfully!

Related Article

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.