刚学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

就添加有问题,其余的都没问题,也不知道是什么问题
...全文
572 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
henry_fuzr 2010-02-26
  • 打赏
  • 举报
回复
接分 无罪 接分 无罪 接分 无罪 接分 无罪 接分 无罪
chenzhg_01 2010-02-25
  • 打赏
  • 举报
回复
大哥,好歹说几句话吧,你想让我们一行一行的扣你的代码么?
liguangwen86 2010-02-25
  • 打赏
  • 举报
回复
恭喜。。。。。。。。。。
jypapgl 2010-02-11
  • 打赏
  • 举报
回复
还没说问题在哪呢 分在哪里
mucrea 2010-02-11
  • 打赏
  • 举报
回复
insert into student (name, age) values (
#name#, #age#);
去掉“;”就好了
道光2008 2010-02-11
  • 打赏
  • 举报
回复
insert into student后的逗号去掉
licip 2010-02-11
  • 打赏
  • 举报
回复
是的。要细心一点呀。
三味书屋 2010-02-10
  • 打赏
  • 举报
回复
解决了,细节真让人头痛,谢谢了
钱不是问题 2010-02-10
  • 打赏
  • 举报
回复
我运到过这个问题,找了1个多小时才找到原因
sotom 2010-02-10
  • 打赏
  • 举报
回复
晕额,把插入语句那贴出来就算了,弄这么多 谁看啊。
jypapgl 2010-02-10
  • 打赏
  • 举报
回复


mapping 中 insert 最后的那个 “;” 分号 还真不知道 带着有没有影响 没试过

或者 看看 parameterClass 是不是需要指定包名 再就是对应下 实体类与 mapping中的 参数 是否能对上 不过这个原因的几率很小 报错不这样报~~~~~
三味书屋 2010-02-10
  • 打赏
  • 举报
回复
引用 2 楼 sggsg 的回复:
insert into student (name,  age)  values (
      #name#, #age#);
不用找了,去掉;就可以了


去掉什么啊,没看清楚
钱不是问题 2010-02-10
  • 打赏
  • 举报
回复
insert into student (name, age) values (
#name#, #age#);
不用找了,去掉;就可以了
钱不是问题 2010-02-10
  • 打赏
  • 举报
回复
sql语句错了,打印下sql语句,然后运行下就知道哪错了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧