Use jdbc to connect to sql server

Setting up sql server

I use sql server 2005 standard version sp2, these are all default,Generally do not need to configure.If you need to configure the port, please see below.

1. "Start" → "Programs" → "Microsoft SQL Server 2005" → "Configuration Tools" → "SQL Server Configuration Manager" → "SQL Server 2005 Network Configuration" → "mssqlserver protocol"

2. If "tcp/ip" is not enabled,Right-click and select Start.

3. Double click "tcp/ip" to enter the property settings.In "ip address", you can configure "tcp port" in "ipall". The default is 1433.

4. Restart the sql server or restart the computer.

Create database

Open "sql server management studio", log in to connect to the sql server server, create a new database,Named test

Test in eclipse

1. Open eclipse, "File" → "New" → "Project" → "java project", the project name is test

2. In eclipse, select "Window" → "Preferences ..." → "java" → "Installed jre", select the installed jre, click "Edit" → "Add External", select%programfiles%\ sqljdbc_1.1 \ chs \ sqljdbc.jar

3. You can see sqljdbc.jar in the "jre system library" of the test project. If not, you can right-click the project test → "build path" → "configure build path.".. "→" java build path "→" library "→" add external jar ... ", select%programfiles%\ sqljdbc_1.1 \ chs \ sqljdbc.jar

4. Write java code as follows:

import java.sql. *;
public class test {
public static void main (string [] srg) {
 string drivername="com.microsoft.sqlserver.jdbc.sqlserverdriver";//Load the jdbc driver
 string dburl="jdbc:sqlserver://localhost:1433;databasename=test";//Connect to the server and database test
 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 is successful, the console outputs connection successful!
 } catch (exception e) {
  e.printstacktrace ();


1. Because the server of this version of SQL Express is disabled by default and the port number is not configured,So we need to reset

2. If you used to connect to sql server 2000 with java, please pay attention to it:

The statement to load the driver and URL path in SQL Server 2000 is

string drivername="com.microsoft.jdbc.sqlserver.sqlserverdriver";

string dburl="jdbc:microsoft:sqlserver://localhost:1433;databasename=sample";

The statement for loading the driver and URL in SQL Server 2005 is

string drivername="com.microsoft.sqlserver.jdbc.sqlserverdriver";

string dburl="jdbc:sqlserver://localhost:1433;databasename=sample";

If written incorrectly, the driver will not be found.

jtds connect to sqlserver

jtds is an open source 100%pure java for jdbc 3.0 driver microsoft sql server and sybase (version 10, 11, 12, 15) driver. jtds is based on freetds and is currently the fastest production ready jdbc driver for sql server and sybase.

jtds is fully compatible with jdbc 3.0 and supports forward only,Parallel and completely independent statements in scrollable/updatable resultsets, and can implement all databasemetadata and resultsetmetadata methods.

jtds-sql server and sybase jdbc driver

package sqlserver_jtds;
import java.sql. *;
public class sqlserver {
 string dburl="jdbc:jtds:sqlserver:// ;;databasename=test";
 string user="sa";
 string password="123456";
 connection conn;
 public sqlserver () {
 this.connect ();
 public void connect () {
 try {
  try {
  class.forname ("net.sourceforge.jtds.jdbc.driver");
  } catch (exception e) {
  e.printstacktrace ();
  //drivermanager.registerdriver(new net.sourceforge.jtds.jdbc.driver ());
  conn=drivermanager.getconnection (dburl, user, password);
  databasemetadata metadata=conn.getmetadata ();
  system.out.print (metadata.getdatabaseproductversion ());
 } catch (exception e) {
  e.printstacktrace ();
 public static void main (string [] args) {
 new sqlserver ();
  • Previous Parse default parameters of constructors and overloads of constructors in C ++
  • Next Java development environment under Linux and the first HelloWorld