-2

i did added mysql.jar file.but i still got java.lang.ClassNotFoundException: com.jdbc.mysql.Driver.

here is external libraries [enter image description here][1]

this is LoginAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by ccc on 2016/12/2.
 */
public class LoginAction extends ActionSupport {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public String execute() throws Exception {
        //return SUCCESS;
        String sql="SELECT Definition FROM word_test LIMIT 10,10;";
        if(jdbcTemplate==null){
            return ERROR;
        }else {
            jdbcTemplate.execute("UPDATE word_test\n" +
                    "SET Definition='what'\n" +
                    "WHERE id=1;");
            return SUCCESS;
        }
    }

}

this is error

    03-Dec-2016 11:29:15.194 警告 [C3P0PooledConnectionPoolManager[identityToken->1hge1149k1i131k71i7l21o|5b4523ab]-HelperThread-#0] com.mchange.v2.c3p0.DriverManagerDataSource. Could not load driverClass com.jdbc.mysql.Driver
 java.lang.ClassNotFoundException: com.jdbc.mysql.Driver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:143)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:173)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)

this is applicationContext.xml

    <?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.xsd">

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
    </bean>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 连接驱动 -->
    <property name="driverClass" value="com.jdbc.mysql.Driver" />
    <!-- 连接URL -->
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/word" />
    <!-- 用户账号 -->
    <property name="user" value="root" />
    <!-- 用户密码 -->
    <property name="password" value="MySqlccc123456" />
    <!-- 连接池中保留的最大接数。默认值: 0~30   -->
    <property name="maxPoolSize" value="30" />
    <!-- 连接池中保留的最小接数。默认值: 0~30   -->
    <property name="minPoolSize" value="0" />
    <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 2 -->
    <property name="acquireIncrement" value="2" />
    <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default:0 -->
    <property name="maxIdleTime" value="1800" />
    <!-- -->
    <property name="maxConnectionAge" value="300" />
    </bean>
    <bean id="iocLoginAction" class="action.LoginAction">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

this is build.gradle

group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    //compile fileTree(dir:'libs',include:'*.jar')
    //compile fileTree(dir:'libs',include:'*.jar')
    compile group: 'org.apache.struts', name: 'struts2-convention-plugin', version: '2.3.15.1'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.3.RELEASE'
    compile 'org.springframework:spring-context:4.3.3.RELEASE'



    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7'

    compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.1'
    compile 'javax.servlet:javax.servlet-api:3.0.1'

    compile group: 'org.apache.struts', name: 'struts2-core', version: '2.3.15.1'
    compile group: 'org.apache.struts', name: 'struts2-convention-plugin', version: '2.3.15.1'
    compile group: 'org.apache.struts', name: 'struts2-config-browser-plugin', version: '2.3.15.1'
    compile group: 'org.apache.struts', name: 'struts2-spring-plugin', version: '2.3.15.1'

    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.30'

    //compile group: 'com.oracle', name: 'ojdbc14', version: '10.2.0.3.0'

    compile group: 'com.mchange', name: 'c3p0', version: '0.9.5.2'
}

And i took a test. TestMain.java will also lead the error

package action;

/**
 * Created by ccc on 2016/12/2.
 */
import com.mysql.jdbc.Driver
public class TestMain {
    public static void main(String[] args){
        try {
            Class<?> clas = Class.forName("com.jdbc.mysql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.print("can't find jdbc"+"\n"+"\n"+"\n");
            e.printStackTrace();
        }
    }
}

this is the error

java.lang.ClassNotFoundException: com.jdbc.mysql.Driver
can't find jdbc
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)


    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at action.TestMain.main(TestMain.java:9)
    at sun.reflect.NativeMethodAccessorI

Then i change the dataSource to this(applicationContext.xml),and it works,why? how can i use c3p0 datasource.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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.xsd">

    <!--数据源的配置 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/word"></property>
        <property name="username" value="root"></property>
        <property name="password" value="MySqlccc123456"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean name="iocLoginAction" class="action.LoginAction">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>


</beans>
Panup Pong
  • 1,871
  • 2
  • 22
  • 44
CC C
  • 7
  • 1
  • 4
  • Try `com.mysql.jdbc.Driver` instead of `com.jdbc.mysql.Driver`. You got the `import` right, so why flip the packages names in the `Class.forName()` call? – Andreas Dec 03 '16 at 04:29
  • thanks,you are right.I was so careless! – CC C Dec 03 '16 at 15:11

2 Answers2

0

The jar file not in the classpath. check the "External Libraries"

Tan
  • 145
  • 1
  • 10
  • i added a picture of "External Libraries" – CC C Dec 03 '16 at 04:20
  • Are you input the class name by manual? Please amend the `com.jdbc.mysql.Driver` to `com.**mysql.jdbc**.Driver`. – Tan Dec 03 '16 at 13:13
  • Are you input the class name by manual? Please amend the com.jdbc.mysql.Driver to com.**mysql.jdbc**.Driver. – Tan Dec 03 '16 at 13:13
  • Are you input the class name by manual? Please amend the com.jdbc.mysql.Driver to com.*mysql.jdbc**.Driver. – Tan Dec 03 '16 at 13:13
  • Are you input the class name by manual? Please amend the `com.jdbc.mysql.Driver` to `com.mysql.jdbc.Driver`. – Tan Dec 03 '16 at 13:14
  • @Tan You know you can delete your own comments, right? Hover over the comment and click the X at the end. You can even edit them (within 5 minutes), instead of creating new comments. – Andreas Dec 03 '16 at 23:00
0

Class.forName("com.mysql.jdbc.Driver") at line 10 in your TestMain.java

philantrovert
  • 9,904
  • 3
  • 37
  • 61