JDBC Connection SQL Server 2005 Summary _mssql2005

Source: Internet
Author: User
Tags microsoft sql server sql server driver management studio sql server management sql server management studio port number

1. Preparation: Prepare related software (except Eclipse, open source software can be downloaded from the website)

<1>. Microsoft SQL Server Edition Express
Download Address: Http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE

<2>. SQL Server Management Studio
Download Address: http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID= c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796

<3>. SQL Server driver for JDBC
Download Address: Http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe

2. All downloaded after the start of the installation, the first two are database software, normal installation can (note that the database login do not use Windows authentication)

<1> extract JDBC to any location, such as decompression to the C disk under Program files, and in the installation directory to find Sqljdbc.jar file, get the path to start configuration environment variables
Append C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar after environment variable classpath

<2> set up SQLExpress server:
A. Open SQL Server Configuration Manager-> sqlexpress Protocol-> TCP/IP

B. Right-click to start TCP/IP

C. Double-click the Enter attribute to set the TCP port in IP all in IP address to 1433

D. Restart the SQLEXPRESS server in the SQL Server 2005 service

E. Shutting down SQL Server Configuration Manager
<3> open a newly installed SQL Server Management Studio, connect the SQLExpress server, create a new database, name sample
<4> Open Eclipse
A. New Project-> Java-> Java project, named Test

B. Select Eclipse-> window-> Preferences->java->installed JRE Edit already installed JDK, find directory add Sqljdbc.jar

C. Right-click Test in the Table of Contents window, select Build path->configure builds path ..., add extension jar file, add Sqljdbc.jar to it

<5> write Java code to test the connection database

Program code:

Import java.sql.*; 
public class Test {public
static void Main (string[] srg) {
 String drivername = ' Com.microsoft.sqlserver.jdbc.SQL Serverdriver "; Load JDBC Driver
 String dburl = "jdbc:sqlserver://localhost:1433;" Databasename=sample "; Connect server and Database sample
 string userName = "sa";//default username
 string userpwd = "123456";//password
 Connection dbconn;
 try {
  class.forname (drivername);
  Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
  System.out.println ("Connection successful!"); If the connection succeeds to the console output connection successful!
 } catch (Exception e) {
  e.printstacktrace ();}}}

Note :

1. Because the SQLExpress server is disabled by default and the port number is not configured, reset
2. If you have previously used java to connect SQL Server 2000, you should note:
The statement that loads the driver and URL paths in SQL Server 2000 is
String drivername = "Com.microsoft. jdbc. SQL Server . SQLServerDriver ";
String dburl = " jdbc: Microsoft:SQL Server://localhost:1433;" Databasename=sample ";
The statements that load the driver and URL in SQL Server 2005 are
String drivername = "Com.microsoft. SQL Server. JDBC . SQLServerDriver ";
String Dburl = " jdbc:sqlserver://localhost:1433; Databasename=sample ";
if the spelling error will not find the driver .
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.