Java Connection SQL Server 2008

Source: Internet
Author: User

Please make sure you have set up the SA, if not, you can refer to the following link to modify http://jingyan.baidu.com/article/8cdccae9452b3c315513cd52.html

Then restart the database and after the reboot is complete, use the command to test whether port 1433 is open.

Then start menu → Run cmd→ at the command prompt input: telnet 127.0.0.1 1433, (note that there is a space between Telnet and 127, 1 and 1433 have a space) if you are prompted to "cannot open a connection to the host, port 1433: Connection Failed", Indicates that port 1433 is not open and needs to be re-configured. If the display Telnet is not an internal command, follow the steps in the figure below to set it, if the connection is successful, display 8:

Open the Control Panel, enter the program, then click Open or Close windows, pull down, you can see the Telnet client, select. Then repeat the fifth step above. http://jingyan.baidu.com/article/6525d4b1377913ac7d2e94eb.html

Configure the environment variable classpath, place the SQLJDBC4 in jdk1.7 (your version) Lib, http://jingyan.baidu.com/article/cbcede07cf5fef02f40b4ddd.html

This is a setup link for environment variables.

Right-click on the computer's properties and click Advanced Properties Settings, click Environment variable settings, add the jar to the previously installed directory, and if not, create a new, and set the address directory for it.

Third, use the Eclipse test to link the SQL Server 2008 database:

1. Open the SQL Server 2008 database and create a new database JSP.

2. Run eclipse to create a new project, and then create a new package with a new Java file in the package, the main file.

3. Sqljdbc4.jar copy into the project, then right click on it and click Build Path. (You can also put the Sqljdbc4jar class under the Jre/lib/ext, and put it under Tomcat/lib, so that it can be applied to all JSP files, reducing the loading of Sqljdbc4jar for each project)

4, enter the Main.java, copy the following code into

1 package pkg; 2  3 import java.sql.*; 4  5 public class Main {6 public  static void Main (String [] args) 7  {8   string Drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; 9   string Dburl= "jdbc:sqlserver://127.0.0.1:1433;databasename=jsp"; ten   string username= "sa";   string Userpwd= "123";  try13 {     class.forname (drivername);     System.out.println ("Load driver succeeded!") ");}catch (Exception e) {     e.printstacktrace ();     System.out.println (" Load driver failed! ");}20 try{21     Connection dbconn=drivermanager.getconnection (dburl,username,userpwd);         SYSTEM.OUT.PRINTLN ("Connection database succeeded! ");}catch (Exception e) {     e.printstacktrace ();     System.out.print (" SQL Server Connection failed! ");        28}29 30}

If there is talnet 127.0.0.1 post 1433 no response in the connection process, first check whether the settings are wrong, if not, then restart the computer, because it may not be updated, I also encountered this problem, and then successfully connected to the database after reboot.

Reference link http://blog.163.com/jackie_howe/blog/static/19949134720125173539380/

Java Connection SQL Server 2008

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.