找不到适合jdbc:sqlserver的驱动程序

时间:2015-07-21 20:16:34

标签: sql-server grails jdbc groovy ggts

我意识到这是一个重复的主题,但我已经按照同样性质的其他主题中的建议步骤进行操作,但没有成功。

我正在使用GGTS 3.6.4和

  • Grails 2.3.0
  • jdk1.7.0_80
  • Groovy编译器级别2.3
  • Microsoft SQL Server 2012

我有一个grails-app,用于验证使用Apache Shiro登录LDAP服务器的用户,并且我有以下代码(在Shiro中生成AuthController.groovy)以尝试从外部数据库中存储一些信息。会话。 (注意:关于用户名,密码和数据库名称,我出于隐私原因在这里更改了所有这些名称)

def signIn = {
    Subject subject = SecurityUtils.getSubject();
    String lowerCaseUserName=params.username.toLowerCase();
    def authToken = new UsernamePasswordToken(lowerCaseUserName, params.password)

    // Support for "remember me"
    if (params.rememberMe) {
        authToken.rememberMe = true
    }

    try{
        subject.login(authToken)

        if (subject.isAuthenticated()) 
        {
            session.username = lowerCaseUserName

            // Attempting to get employee id from MS SQL
            Sql Database = Sql.newInstance(
                'jdbc:sqlserver://myserver;DatabaseName=mydatabase',
                'user',
                'password',
                'com.microsoft.sqlserver.jdbc.SQLServerDriver'
            );
            Database.eachRow('select empid from table_name where username=${session.username}') { row ->
                session.empid = row.empid
            }
            Database.close();   

            def targetUri = params.targetUri ?: "/home"
            log.info "Redirecting to '${targetUri}'."
            redirect(uri: targetUri)
        }
    }
    ...
}

但是,我收到以下错误

SQLException occurred when processing request: [POST] /app/auth/signIn - parameters:
username: user
_rememberMe: 
targetUri: 
password: ***
No suitable driver found for jdbc:sqlserver://myserver;DatabaseName=mydatabase. Stacktrace follows:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://myserver;DatabaseName=mydatabase
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at app.AuthController$_closure3.doCall(AuthController.groovy:45)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

我尝试过以下操作但没有成功:

  • 将sqljdbc4.jar添加到/ app / lib /
  • 手动将/ app / lib /添加到classpath(通过.classpath)
  • 通过Properties>将sqljdbc4.jar添加到类路径中Java Build Path> Add JARs

我已尝试使用sqljdbc4.jar,sqljdbc.jar和sqlserverjdbc.jar及其每个组合进行这些变化。

我基本上被困住了。我在这里或其他地方读到的修复都没有解决我的错误。任何帮助将不胜感激!

编辑1:在Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")调用之上添加newInstance会产生以下错误:

ClassNotFoundException occurred when processing request: [POST] /app/auth/signIn - parameters:
username: user
_rememberMe: 
targetUri: 
password: ***
com.microsoft.sqlserver.jdbc.SQLServerDriver. Stacktrace follows:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName(Class.java:195)
at isec.AuthController$_closure3.doCall(AuthController.groovy:45)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

这是前进,后退,还是我们正在运行?

编辑2:我最不得不做的是将DataSource.groovy改为此

dataSource {
    pooled = true
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    //cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "validate"
            url = "jdbc:sqlserver://myserver:1433;databaseName=mydatabase;"
            driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
            username = "myusername"
            password = "mypassword"
        }
    }
....
}

并将我的AuthController.groovy数据访问权限更改为

    try{
        subject.login(authToken)
        if (subject.isAuthenticated()) 
        {
            ShiroUser currentUser = new ShiroUser()

            def targetUri = params.targetUri ?: "/home"
            log.info "Redirecting to '${targetUri}'."
            redirect(uri: targetUri)
        }
    }

我已经使用修改后的ShiroUser.groovy文件

成功访问了我的数据库
class ShiroUser {

    static hasMany = [ roles: ShiroRole, permissions: String ]

    User_Data userData;

    static constraints = {
    }

    def getUsername() {
        return userData.username
    }
}

其中User_Data.groovy是包含

的新域类
class User_Data {

    static mapping = {
        table "mytablename"
    }
    ...
}

所以现在我正在搞乱方法!不知道为什么JDBC的东西没有用,但GORM是我现在要走的路。

1 个答案:

答案 0 :(得分:0)

你肯定需要将sqljdbc4.jar添加到你已经完成的/ app / lib /中。

您是否尝试过将数据库连接添加到BuildConfig.groovy

dataSource {
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
        dialect = "org.hibernate.dialect.SQLServerDialect"
        url = "jdbc:sqlserver://localhost:1433;databaseName=dbName"
        username = "sa"
        password = ""
    }

还要确保SQL Server配置为接受端口1433上的连接。默认情况下禁用它。