Use Eclipse to test the connection to the SQL Server 2008 database:
1. Open SQL Server 2008, create a new database Test in it, and then exit SQL Server 2008.
2. Run Eclipse and create a new Java Project named Test.
3. Right-click the project name, select Build Path → Configure Build Path, select the Libraries tab on the right side of the opened window, then click Add External JARs, find the sqljdbc4jar file and open it, and then click OK Complete the configuration of the build path. As shown in the picture:
Tips: You can also put the sqljdbc4jar class under jre/lib/ext and put it under tomcat/lib
This way you can Apply to all jsp files to reduce the loading of sqljdbc4jar for each project;
4. Create a new package pkg in Test, create a new class Main in pkg, and enter the following code in it:
package pkg; import java.sql.*; public class Main { public static void main(String [] args) { String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=填写你的数据库名"; String userName="填写你的用户名,我的是sa"; String userPwd="填写你的密码"; try{Class.forName(driverName); System.out.println("加载驱动成功!");} catch(Exception e){e.printStackTrace(); System.out.println("加载驱动失败!");} try{Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);System.out.println("连接数据库成功!"); }catch(Exception e){e.printStackTrace(); System.out.print("SQL Server连接失败!");}} }
5. Right-click and select run as——>Java Application. If Figure 11 below appears, the connection is successful!
The above is the detailed content of How to connect eclipse to sql?. For more information, please follow other related articles on the PHP Chinese website!