Java. SQL. sqlexception: no suitable driver found for http://maven.apache.org"

Source: Internet
Author: User

Today, we use Maven and mybatis to develop J2EE programs on Eclipse. I have just learned mybatis and am still familiar with Maven. I have read books for a short time. I wanted to try selectlist of mybatis. After the program is compiled, an error is returned:

org.apache.ibatis.exceptions.PersistenceException: ### Error opening session.  Cause: java.sql.SQLException: No suitable driver found for http://maven.apache.org### Cause: java.sql.SQLException: No suitable driver found for http://maven.apache.orgat org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:83)at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)at databaseAdapter.CollegeAdapter.selectCollege(CollegeAdapter.java:28)at temp.RunSqlSession.main(RunSqlSession.java:30)Caused by: java.sql.SQLException: No suitable driver found for http://maven.apache.orgat java.sql.DriverManager.getConnection(DriverManager.java:602)at java.sql.DriverManager.getConnection(DriverManager.java:185)at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:64)at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:349)at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:55)at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:73)... 3 more

Depressed!

I have seen an error in sqlexception, saying that classnotfound or the link cannot be opened, but I have never seen it.

Java. SQL. sqlexception: no suitable driver found for http://maven.apache.org

I read the Java code again, and did not find the http://maven.apache.org in the Java code, using Maven MVN clean compile again, found the problem is still, suddenly thought, go to target and check the configuration file of mybatis. The reason is found!

The source code of my mybatis configuration file is as follows:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><properties>  <property name="url" value="jdbc:postgresql://localhost/mydb3"/>  <property name="driver" value="org.postgresql.Driver"/>  <property name="username" value="1234"/>  <property name="password" value="1234"/></properties><typeAliases><typeAlias alias="College" type="pojo.College"/></typeAliases><environments default="development">  <environment id="development">    <transactionManager type="JDBC"/>    <dataSource type="POOLED">      <property name="driver" value="${driver}"/>      <property name="url" value="${url}"/>      <property name="username" value="${username}"/>      <property name="password" value="${password}"/>    </dataSource>  </environment></environments><mappers>  <mapper resource="mybatis/mapper/CollegeMapper.xml"/></mappers></configuration>

It can be changed:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><properties>  <property name="url" value="jdbc:postgresql://localhost/mydb3"/>  <property name="driver" value="org.postgresql.Driver"/>  <property name="username" value="1234"/>  <property name="password" value="1234"/></properties><typeAliases><typeAlias alias="College" type="pojo.College"/></typeAliases><environments default="development">  <environment id="development">    <transactionManager type="JDBC"/>    <dataSource type="POOLED">      <property name="driver" value="${driver}"/>      <property name="url" value="http://maven.apache.org"/>      <property name="username" value="${username}"/>      <property name="password" value="${password}"/>    </dataSource>  </environment></environments><mappers>  <mapper resource="mybatis/mapper/CollegeMapper.xml"/></mappers></configuration>

Have you found any problems?

Yes, Maven replaced $ {URL} of mybatis with a http://maven.apache.org!

Why?

Before Maven's compile, there is a phase (phase) That is process-resources, at this time, the resources plug-in will replace the $ {XXX} variable used by XML in src/main/resources with the set value, and the URL value happens to be defined in the project pom file:

<modelVersion>4.0.0</modelVersion><groupId>cn.edu.sdu.ise.leesonlog.lms4g</groupId><artifactId>lms4g-website</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>lms4g-website Maven Webapp</name><url>http://maven.apache.org</url>

Again, I put the configuration file of mybatis under the src/main/resouces directory ...... Then the tragedy happened.

Solution:

Prevent Maven from filtering the mybatis configuration file and Its mapper file under the resources directory!

Modify the POM file of the project:

<Resources> <! -- Filter the Resources Directory, mainly to set the content in the bin directory --> <resource> <directory >$ {project. basedir}/src/main/resources </directory> <filtering> true </filtering> <excludes> <exclude> mybatis/**/* </exclude> </Excludes> </resource> <! -- Do not filter resources/mybatis directories; otherwise, some XML configurations of mybatis may be damaged --> <resource> <directory >$ {project. basedir}/src/main/resources </directory> <filtering> false </filtering> <shortdes> <include> mybatis/**/* </include> </shortdes> </resource> </resources>

Let Maven not filter out the items in the mybatis folder.

Note: I put my mybatis configuration file under the src/main/resouces/mybatis folder.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.