Java connection to SQL Server 2012

Source: Internet
Author: User
Tags rar

SQL Server prepares:

1. Right-click Database Open Properties

2. Security--sql identity and Windows authentication

3. In data login sa, select attribute Change Password: for example 123456

4. Open Configuration Manager: Enable TCP/IP and Names pipes protocol

5. Restart the service to the end of the database configuration!

Next:

I. Configuring an ODBC data source

To configure ODBC:

Control Panel-----Management Tools-----Data Sources (ODBC)

System DSN-----Add----SQL Server-----name MSSQL, server: 127.0.0.1----Fill in the User name password test, the most critical is to configure the client:

Server name is 127.0.0.1, choose TCP/IP, the following basic is OK, finally pay attention to test the data source


Step Two: Guide package test (test using Sqljdbc4.rar)

Decompression Sqljdbc4.rar

Find this Sqljdbc4.jar bag, cut to where you want it to go ( recommended jre\lib in the JDK installation directory to match Eclipse is good management , this is only the Eclipse environment configuration, of course, not to carry the environment variable classpth can also be

2. Configure Eclipse

Window------Preferences-----Java------Installed JRE----Add an external jar find the newly moved Sqljdbc.jar file and add it in.

Finally crazy press Finish OK what!


Code Verification:

public static void Main (String...arg) {String driver= "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url= "Jdbc:sqlserver://localhost:1433;databasename=master"; String user= "sa", password= "123456"; Here is your sa account password try {class.forname (driver);} catch (ClassNotFoundException e) {System.out.println ("Driver load Failed");} Java.sql.Connection con=null;try {con=drivermanager.getconnection (Url,user,password);} catch (SQLException e) { System.out.println ("Connection Failed");} if (con!=null) System.out.println ("Connection succeeded:" +con);}


What is your result?

Of course not very normal, if not well, it doesn't matter, landlord for small white prepare an illustrated with the jar Package Tutorial: in the resources:

Special Tutorial number password is 20144206199

Come on, get it done.

Although Jdbc+mysql relatively fast, but inevitably encountered a tricky SQL Server, MSSQL PDO+SQLSVR is the big hole! Share it next time.




Java Connect SQL Server

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.