我在TomCat的context.xml里配置好
[code="java"]<Resource name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="20"
maxWait="5000"
username="root"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myblog" />[/code]
在项目的web.xml里也设置好了
[code="java"]<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysqlds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> [/code]
同时把MySql的jar包也放到Tomcat的lib目录下了,但是当连接数据库的时候
[code="java"]conn = ds.getConnection();[/code]语句报错说:
[code="java"]The method getConnection() is undefined for the type DataSource[/code]
这是为什么啊?
[b]问题补充:[/b]
不行的 刚试了
[b]问题补充:[/b]
原因以找到,是导错包了,正确的应该是下面这个:
[code="java"]import javax.sql.DataSource; [/code]
所以会导致找不到ds的Connection()方法。