0

i got error when run my application, the error is:

Specified JDBC Driver com.microsoft.sqlserver.jdbc.SQLServerDriver class not found

my souce: pom.xml

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-convention-plugin</artifactId>
        <version>2.3.20</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-rest-plugin</artifactId>
        <version>2.3.20</version>
    </dependency> 

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.1.0.CR2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver.jdbc</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/main/lib/sqljdbc4.jar</systemPath>
    </dependency>

hibernate.cfg.xml

<hibernate-configuration>     
<session-factory> 

 <!-- Database connection settings -->  
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>  
    <property name="connection.url">jdbc:sqlserver://.;database=db</property>  
    <property name="connection.username">sa</property>  
    <property name="connection.password">12345</property>   
    <property name="connection.pool_size">1</property>   
    <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>   
    <property name="show_sql">false</property>   
    <property name="hbm2ddl.auto">update</property>  

    <!-- Mapping files --> 
    <mapping resource="employee.hbm.xml"/> 

</session-factory> 

code that call Hibernate:

public static List<Employee> SelectAll(){ 
    Configuration cfg = new Configuration();  
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();  
    Session session = factory.openSession(); 

    //String sql_query= "from Employee where Name = 'Kevin'";
    String sql_query= "from Employee";
    Query query = session.createQuery(sql_query);
    List<Employee> employees = query.list();

    session.close();  
    factory.close(); 
    return employees;
}

Anyone know why? the error happened in SessionFactory factory = cfg.buildSessionFactory();

Directory in:

enter image description here

When i check the library, it Has SQLServerDriver

enter image description here

4 Answers 4

2

@Lumanyun you're using a jar that don't exist in repository Maven :

<dependency>
        <groupId>com.microsoft.sqlserver.jdbc</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
    </dependency>

Go check here a version that can helps you

Sign up to request clarification or add additional context in comments.

1 Comment

i dont know whats wrong my dependency is... but with this it can running correctly... thanks bro!
2

It is because of there is not com.microsoft.sqlserver.jdbc.SQLServerDriver in the class path.

Please, check sqljdbc4.jar in the class path and check com.microsoft.sqlserver.jdbc.SQLServerDriver class inside of the sqljdbc4.jar.

To find is any jar in the class path with SQLServerDriver, try to execute this code before the Hibernate configuration code.

URL driverUrl = Thread.currentThread().getContextClassLoader()
    .getResource(
    "com/microsoft/sqlserver/jdbc/SQLServerDriver.class");
System.out.println(driverUrl);

4 Comments

hi,,, it has SQLServerDriver in class path... is there any problem in Maven? because before i using sqljdbc4.jar without maven and its doing fine
i already try driverURL, the error is same... i try import com.microsoft.sqlserver.jdbc.SQLServerDriver and it can... :<
@Lumanyun Which path do you see with System.out.println(driverUrl);?
dirverUrl return error Specified JDBC Driver com.microsoft.sqlserver.jdbc.SQLServerDriver class not found
1

Please check your classpath. The driver jar must be on the classpath. This issue is related to java not able to find the class in the specified classpath. Put the jar file in the same directory or make it findable.

1 Comment

hi,,, it has SQLServerDriver in class path... is there any problem in Maven? because before i using sqljdbc4.jar without maven and its doing fine
1

As already said in above comments that first you check that either the class is there in classpath or not. Because as per the exception it seems that class file is missing in classpath.

Other than this Here I want to point out something different. As per the convention, you should have to use property name inside hibernate.cfg.xml file as hibernate.connection.driver_class, hibernate.connection.url, etc. Here You have used only connection.driver_class, connection.url, etc.

2 Comments

hi,,, it has SQLServerDriver in class path... is there any problem in Maven? because before i using sqljdbc4.jar without maven and its doing fine
Using hibernate prefix in the hibernate.cfg.xml is not mandatory (unlike hibernate.properties), but it is a good style, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.