1. To use a database in Java, you need to use JDBC (java database connection) to connect and operate.
Download address: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
2. After completion, extract it to any disk, such as: G:/Program Files/sqljdbc;
3.Microsoft JDBC Drivr 4.0 for SQL Server only supports jdk1.6 and above, and contains two packages internally: sqljdbc4.jar and sqljdbc.jar
4. Put sqljdbc4.jar into the java installation package
For example: 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, not sqljdbc.jar, because there can only be one jdbc, and an error will be reported if two are placed at the same time.
5. In Start->Microsoft Sql server 2008 R2 (or other versions)->Configuration Tools->SQL Server Configuration Manager
->SQL Server 2008 r2 network configuration->MSSQLSERVER protocol;
Enable "TCP/IP"; double-click "TCP/IP" -> "IP Address" -> "IPAll" -> "TCP Port" item to add the default "1433".
6. Modify the default login authentication mode (because the installation process is based on the "Windows Authentication Mode" by default, the sa login is disabled)
① First log in with Windows identity, then go to Security->Login name->right-click "sa"->Properties->General->
Set the password and confirmation password of login name sa to 123456->Status->Login->Enable->OK;
② Then exit, restart the database, and log in using "SQL Server and Windows Authentication Mode". The user name is sa and the password is 123456. If the login is successful, the database settings are completed.
7. Open eclipse and create a new java project
Copy the code 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=sample"; //Connect to the 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 is successful, the console outputs Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
8. If the operation is successful, the configuration is successful!