Connect sqlserver2008 r2/sqlserver2008 with Java

Source: Internet
Author: User

Package com.product.jdbc.dbutil;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;

public class Democonn {
Connection conn = null;

static {
try {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");//Load Database driver
SYSTEM.OUT.PRINTLN ("Database driver loading succeeded!!! --sqlserver ");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}

Public Connection Democonn () {
String url = "jdbc:sqlserver://213-pc:1433; Databasename=movie ";
String userName = "sa";
String PassWord = "123";
try {
conn = drivermanager.getconnection (URL, userName, passWord);//Get Database connection
IF (conn! = null) {
SYSTEM.OUT.PRINTLN ("Database connection succeeded!!! --sqlserver ");
}
} catch (SQLException e) {

E.printstacktrace ();
}
Return conn;//returns the Connection object
}

public static void Main (string[] args) {
Democonn democonn = new Democonn ();//Create Democonn Object
Connection conn = Democonn.democonn ();
}
}

Connecting in this way requires importing the Sqljdbc4.jar jar package.

URL format: jdbc:sqlserver://[servername[\instancename][:p ortnumber]][;p roperty=value[;p Roperty=value]

The default port for SQL Server is 1433, but I am installing R2, which uses a dynamic port.

View method: SQL Server Configuration manager->sqlserver Protocol->tcp/ip->ip address (scroll to the last port, the TCP dynamic port is changed to 1433, we can also change to another port)

Connect sqlserver2008 r2/sqlserver2008 with Java

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.