Download Microsoft JDBC Driver 4.0 for SQL Server
Download here: http://www.microsoft.com/zh-cn/download/details.aspx?id=11774
1.
Create a new folder on disk E, named sqljdbc 42, and copy sqljdbc 42.jar into it.
Right click my computer properties advanced system settings environment variable, double-click the CLASSPATH variable (or select CLASSPATH and edit),
Add "; E: \ sqljdbc 42 \ sqljdbc 42. Jar" to the last side (note the first one;)
If there is no CLASSPATH, create a new CLASSPATH variable and set its value to "E: \ sqljdbc 42 \ sqljdbc 42. Jar"
If Tomcat is used as the server, we need to copy the sqljdbc 42.jar class library file to e: \ Apache Tomcat - * * * (version No.) \ lib directory
In addition, you need to copy the sqljdbc 42.jar class library file to the C:\Program Files\Java\jre8\lib\ext directory
Then create a new java project in ecilipse, right-click "BuildPath" in src directory, - > "configure build path" - > "libraries" - > "add external jars"
Add sqljdbc 42.jar file of sqljdbc 42 in E disk
Then create a new class to run
2. Test connection operation
package Test; import java.sql.*; public class Test { public static void main(String[] args) { update(); } public static void update() { Connection connection=null; String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL database engine String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=tjl";//Data source!!! Note that if loading or connecting to the database fails, it is usually a problem here String Name="sa"; String Pwd="19990713"; try{ Class.forName(driverName); connection=DriverManager.getConnection(dbURL,Name,Pwd); System.out.println("Successfully connected to database"); }catch(Exception e){ e.printStackTrace(); System.out.println("connection failed"); } Statement stmt = null; try { stmt = connection.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Add, delete and modify String sql_1="insert into student values('990403','Page','male','12','1','2101')"; int count_1 = 0; try { count_1 = stmt.executeUpdate(sql_1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }//The return value indicates adding, deleting and modifying several pieces of data //Processing result if(count_1>0){ System.out.println("Update success!"); } //Query operation String sql_2="select * from student"; //Close try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }