java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection

异常是酱紫的:

java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
	at org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:315)
	at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:495)
	at com.hanxiao.jdbc.util.TestUtils.TestInsert(TestUtils.java:19)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

一般地,用java代码对数据库进行操作,需要先创建连接池,然后再进行查询
此处,问题在于,创建QueryRunner时,未将连接池传入QueryRunner
错误示例:

import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
public class TestUtils {
@Test
public void TestInsert() throws SQLException {
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	QueryRunner queryRunner = new QueryRunner();	//此处未传入连接池
	......
	......
	......

正确示例:

import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
public class TestUtils {
@Test
public void TestInsert() throws SQLException {
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	QueryRunner queryRunner = new QueryRunner(dataSource);		//此处传入连接池
	String sql = "select * from account where id=?";
	Account account = queryRunner.query(sql, new BeanHandler(Account.class), 10);	//传入Account泛型    //id=10
	System.out.println(account);
	}

}

你可能感兴趣的:(Java,异常解决)