Reputation: 63
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:
When i check the library, it Has SQLServerDriver
Upvotes: 0
Views: 1499
Reputation: 1635
@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
Upvotes: 2
Reputation: 19956
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);
Upvotes: 2
Reputation: 13
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.
Upvotes: 1
Reputation: 32
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.
Upvotes: 1