A brief introduction to Java Connection SQL Server under Windows system _java

Source: Internet
Author: User
Tags microsoft sql server microsoft sql server 2005 sybase management studio sql server management sql server management studio

Using JDBC to connect to SQL Server

Set up SQL Server server

I'm using the SQL Server 2005 Standard Version SP2, which is all by default, and is generally not configured. If you need to configure the port, see below.

1, "Start" → "program" → "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 to select "Start".
3, double click "TCP/IP" to enter the property setting, in "IP address", can configure "TCP port" in "Ipall", default is 1433.
4. Restart SQL Server or reboot the computer.


Creating a Database

Open SQL Server Management Studio, log on to connect to the SQL Server server, create a new database, name test


Testing in Eclipse

1, open Eclipse, "file" → "new" → "Project" → "Java Project", 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 you don't have the option to 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 D Rivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC driver String dburl = "jdbc:sqlserver://localhost:1433;" Databasename=test "; Connect server and database Test String userName = "sa"; Default User name 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 SQL Express this version of the server is disabled by default and the port number is not configured, so to reset
2, if you have previously used Java to connect SQL Server 2000, you should pay attention to:
The statement that loads the driver and URL paths in SQL Server 2000 is
String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";
String Dburl = "jdbc:microsoft:sqlserver://localhost:1433;" Databasename=sample ";
The statements that load the driver and URL in SQL Server 2005 are
String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
String Dburl = "jdbc:sqlserver://localhost:1433;" Databasename=sample ";
If the spelling error will not find the driver.


JTDS connection to SQL Server
Jtds is an open source 100% pure Java, for JDBC 3.0 drivers for Microsoft SQL Server and Sybase (10, 11, 12, 15 versions). Jtds is based on FreeTDS, and is currently the fastest production preparation JDBC driver for SQL Server and Sybase.
Jtds is fully compatible with JDBC 3.0, supports parallel (fully independent) statements in forward-only, scrollable/updatable result sets (ResultSets), and implements all DatabaseMetaData and ResultSetMetaData methods.
Jtds-sql Server and Sybase JDBC driver

Package Sqlserver_jtds; 
 
Import java.sql.*; 
 
public class SQL Server { 
 String dburl = ' jdbc:jtds:sqlserver://127.0.0.1:1433;;D Atabasename=test "; 
 String user = "sa"; 
 String password = "123456"; 
  
 Connection Conn; 
  
 Public SQL Server () { 
 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 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.