我正在尝试连接到SQL Server,并将数据插入数据库中。我有一个班级人员,主要和DataBaseInfo。我已经为所有错误下载了maven库,但没有成功。我不知道为什么
<?xml version='1.0' encoding='UTF-8'?>
<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-3.0.xsd">
<bean id='data' class='com.microsoft.sqlserver.jdbc.SQLServerDriver'>
<property name='driverClassName' value ='java.sql.DriverManager'></property>
<property name='dataSource' value ='jdbc:sqlserver://10.222.115.11:1433;databaseName=Test;integratedSecurity=true'></property>
<property name='username' value='HmgDbUser'></property>
<property name='password' value='123456'></property>
</bean>
<bean id='template' class='org.springframework.jdbc.core.JdbcTemplate'>
<property name='dataSource' ref ='data'></property>
</bean>
<bean id='dbi' class='test1.DataBaseInfo'>
<property name='template' ref ='template'></property>
</bean>
</beans>
我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>test1</artifactId>
<name> Spring boots test</name>
<version>0.0.2-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssqljdbc6</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
</project>
我的主要人
package test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
//jdbc:sqlserver:10.200.105.11;databaseName=test;integratedSecurity=true
public static void main(String[] args) {
ApplicationContext conx = new ClassPathXmlApplicationContext("bean.xml");
DataBaseInfo di = (DataBaseInfo)conx.getBean("dbi");
int ins=di.insertPerson(new Person("abdulaziz","riyadh","Saudi","Developer"));
System.out.println(ins);
}
}
运行它时,会发生此错误:
Oct 28, 2018 4:35:01 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.microsoft.sqlserver.jdbc.SQLServerDriver] for bean with name 'data' defined in class path resource [bean.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.microsoft.sqlserver.jdbc.SQLServerDriver] for bean with name 'data' defined in class path resource [bean.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:663)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:630)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1491)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1014)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:826)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at test1.Main.main(Main.java:8)
Caused by: java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1015)
at java.lang.ClassLoader.loadClass(ClassLoader.java:413)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:275)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:437)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1430)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1374)
... 10 more
我的Java版本
答案 0 :(得分:4)
您有两个选择。
使用为JDK版本8编译的JDBC驱动程序版本或与您要使用的JDK版本匹配的版本。但是也要检查数据库的文档,以确保选择正确的驱动程序实现。例如:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
答案 1 :(得分:0)
尝试从Maven使用低版本的jdbc驱动程序