Using a database in 1.java requires the use of JDBC (Java database connection) to connect and manipulate
Download Address: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
2. After the completion of the decompression to a plate, such as: G:\Program FILES\SQLJDBC;
3.Microsoft JDBC DRIVR 4.0 for SQL Server supports only jdk1.6 and above, with two packages internally: Sqljdbc4.jar and Sqljdbc.jar
4. Put the Sqljdbc4.jar into the Java installation package
such as: G:\Program Files\java\jdk1.7.0_05\jre\lib\ext (Must be:. \jdk1.7.0_05\jre\lib\ext)
PS: Only put Sqljdbc4.jar into the package, Sqljdbc.jar is not put, because there can only be a JDBC, two of the same put the error
5. Start->microsoft SQL Server 2008 R2 (or other version)-> Configuration Tool->sql Server Configuration Manager
->sql Server 2008 R2 Network Configuration->mssqlserver protocol;
Enable TCP/IP, and double-click TCP/IP->-> "ipall"-> "TCP port" to add the default "1433".
6. Modify the Default logon authentication mode (the sa login is disabled because it was installed by default in Windows authentication mode during the installation process)
① first log in with Windows identity, then right-click the "SA"-> property-> the security-> logon name->->
Set the password and confirmation password for the sign-in name SA to 123456-> State-> login-> enable-> OK;
② then exits, restarts the database, uses SQL Server and Windows Authentication mode to log in, user name SA, password 123456, login successful database set up
7. Open Eclipse, New Java Project
Copy Code code as follows:
Import java.sql.*;
public class Test {
public static void Main (string[] SRG) {
String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; Load JDBC Driver
String Dburl = "jdbc:sqlserver://localhost:1433;" Databasename=sample "; Connecting Servers and Databases sample
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 ();
}
}
}
8. If the operation is successful, it indicates that the configuration is successful!