如何从Java EE Spring应用程序以Windows身份验证模式连接到SQL Server?

时间:2014-07-04 08:36:56

标签: java sql-server spring jdbc windows-authentication

我想在Windows身份验证模式下将我的Java EE Web应用程序连接到SQL Server数据库。我有两个 Spring.xml 文件, Spring-Datasource.xml Spring-Customer.xml ,它们都是在中导入的弹簧Module.xml 即可。这是我的Spring-Datasource.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>        
        <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=Spring;" />
    </bean>

</beans>

这是我的主要课程:

package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.dao.CustomerDAO;
import com.mkyong.customer.model.Customer;

public class App {

    public static void main(String[] args) {
        try {
            ApplicationContext context;

            context = new ClassPathXmlApplicationContext("Spring-Module.xml");

            CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
            Customer customer;
            customer = new Customer("mkyong", 28);
            customerDAO.insert(customer);

            Customer customer1 = customerDAO.findByCustomerId(1);
            System.out.println(customer1);
        } catch (Exception e) {
            System.out.println("********************************************");
            System.out.println(e.getMessage());
            System.out.println("********************************************");
        }
    }
}

但我总是得到这个错误:

******************************************** org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' ********************************************

我该如何解决?

注意: 我是Java的初学者

1 个答案:

答案 0 :(得分:1)

您的类路径中是否有JDBC驱动程序库?对于j2ee,它应该在lib文件夹中。