发新话题

Spring框架的 JdbcTemplate对象没有execute()方法吗?

我现在直接想用spring连接数据库,执行各种数据库的操作,用类来建一张数据表,然后通过JdbcTemplate类来执行相应的操作,可是却报JdbcTemplate没有execute()方法,后来我在网上查了一下JdbcTemplate类的API上面找到了这个方法哦!可是写的时候却没有!我想也有可能是版本的问题!可能原来的版本有execute()方法,新的版本就没有这个方法,可是新的版本又是用什么来代替execute()方法的呢?后来我又照书上写了一个测试的测了一下,还是那样的.
下面是我的所有测试代码:
myspring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="dbtest" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>org.git.mm.mysql.Driver</value>
</property>

<property name="url">
<value>jdbc:mysql://127.0.0.1/mytest</value>
</property>

<property name="username">
<value>root</value>
</property>

<property name="password">
<value>zgy01</value>
</property>
</bean>

</beans>

SqlStatement.java

package test;


public abstract class SqlStatement
{
final public static String createSql = "create table mytable(id int,cname varchar(50))";
final public static String insertData1 = "insert into mytable values(1,'name1')";
final public static String insertData2 = "insert into mytable values(2,'test2')";
final public static String insertData3 = "insert into mytable values(3,'test3')";
final public static String selectSql = "select * from mytable";
}

SpringtoResultSetInfo.java

package test;

public class SpringtoResultSetInfo
{
private int id;
private String templatename;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTemplatename() {
return templatename;
}
public void setTemplatename(String templatename) {
this.templatename = templatename;
}


}

Test.java

package test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowCountCallbackHandler;


public class Test
{
public static void main(String[] args)
{
Resource resource = new ClassPathResource("myspring.xml");
BeanFactory factory = new XmlBeanFactory(resource);
JdbcTemplate jt = (JdbcTemplate)factory.getBean("dbtest");

jt.execute(SqlStatement.createSql); //报错


}
}


报错信息:
Severity and Description Path Resource Location Creation Time Id
The project was not built since its build path is incomplete. Cannot find the class file for org.springframework.dao.DataAccessException. Fix the build path then try building this project SpringDBTest Unknown 1210571456953 26

Severity and Description Path Resource Location Creation Time Id
The type org.springframework.dao.DataAccessException cannot be resolved. It is indirectly referenced from required .class files SpringDBTest/src/test Test.java line 24 1210571456953 25

可是我在网上看了一下spring JdbcTemplate类的API 上面有那个方法啊!

java.lang.Object
org.springframework.jdbc.support.JdbcAccessor
org.springframework.jdbc.core.JdbcTemplate


void execute(String sql)
Issue a single SQL execute, typically a DDL statement.

http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html#execute(java.lang.String)



编辑 回复 快速回复 TOP
The project was not built since its build path is incomplete. Cannot find the class file for org.springframework.dao.DataAccessException.

你的Build Path 设置问题,你看看你更新了你的Spring,是不是对应的jar也更新了!
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP
谢谢竹子!我后来加了很多相应的 jar包就好了哦!
编辑 回复 快速回复 TOP
发新话题