刚学ibatis,遇到一个插入问题
三味书屋 2010-02-10 04:38:49 SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig >
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<properties resource="ibatis/SqlMap.properties"/>
<!-- commitRequired="true" -->
<transactionManager type="JDBC" >
<dataSource type="SIMPLE" >
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="ibatis/Student.xml"/>
<!-- List more here...
<sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/>
-->
</sqlMapConfig>
Student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Student">
<typeAlias alias="Student" type="ibatis.Student"/>
<resultMap id="StudentResult" class="Student">
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="selectAllStudent" resultMap="StudentResult">select * from student</select>
<select id="selectStudentById" parameterClass="int" resultClass="Student">
select * from student where age=#ag#
</select>
<insert id="insertStudent" parameterClass="Student">
insert into student (name, age) values (
#name#, #age#);
</insert>
<delete id="deleteStudentById" parameterClass="int">
delete from student where age=#id#
</delete>
<update id="updateStudent" parameterClass="Student">
update student set name=#name# where age=#age#
</update>
<select id="selectStudentByName" parameterClass="String" resultClass="Student">
select *from student where name like '%$name$%'
</select>
</sqlMap>
实现
package ibatis;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
public class StudentDaoImple implements IStudentDao {
private static SqlMapClient sqlMapClient;
static {
try {
Reader reader = com.ibatis.common.resources.Resources
.getResourceAsReader("ibatis/SqlMapConfig.xml");
sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder
.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addStudent(Student student) {
try {
sqlMapClient.insert("insertStudent",student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteStudentById(int id) {
try {
sqlMapClient.delete("deleteStudentById",id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Student> queryAllStudent() {
List<Student> list = null;
try {
list = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public Student queryStudentById(int id) {
Student stu = null;
try {
stu = (Student) sqlMapClient
.queryForObject("selectStudentById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
public List<Student> queryStudentByName(String name) {
List<Student> list=null;
try {
list=sqlMapClient.queryForList("selectStudentByName",name);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void updateStudentById(Student student) {
try {
sqlMapClient.update("updateStudent",student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
异常
com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in ibatis/Student.xml.
--- The error occurred while applying a parameter map.
--- Check the insertStudent-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLException: ORA-00911: 无效字符
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:107)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:58)
at ibatis.StudentDaoImple.addStudent(StudentDaoImple.java:27)
at ibatis.StudentDaoImple.main(StudentDaoImple.java:99)
Caused by: java.sql.SQLException: ORA-00911: 无效字符
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:952)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1160)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3390)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
... 5 more
就添加有问题,其余的都没问题,也不知道是什么问题