在mysql DB中使用ibatis添加新人

时间:2015-02-12 15:20:40

标签: mysql xml servlets

我想在j2EE应用程序中使用ibatis在数据库中添加人员。 我在mySQL,servlet,SqlMapConfix.xml,Person.xml和Person.java中有一个表人。 我做了很多测试,但我有同样的错误。

java.lang.RuntimeException:发生错误。原因:com.ibatis.common.xml.NodeletException:解析XML时出错。原因:java.lang.RuntimeException:解析XPath'/ sqlMapConfig / sqlMap'时出错。原因:java.io.IOException:找不到资源config / Person.xml

这里的代码: 的index.html

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
</head>
<body>
    <form action="Controller" >
        <input type="text"  name="nom">
        <input type="text"  name="pren">
        <input type="submit" value="ok">
    </form> 
</body>

servlet:Controller.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
 PrintWriter out = response.getWriter();
 String a = request.getParameter("nom");
  String b = request.getParameter("pren");

try{

 Reader rd;
  rd = Resources.getResourceAsReader("config/SqlMapConfig.xml");


  SqlMapClient smc; 
  smc = SqlMapClientBuilder.buildSqlMapClient(rd);
    out.println("Going to insert record.....");
  Person em = new Person(a, b);

  smc.insert("Person.insert", em); 

   out.println("Record Inserted Successfully ");

   }catch(SQLException e)
  {e.printStackTrace();}

 }

Person.java

package controleur;

public class Person {
String nom,prenom;

public Person(String nom, String prenom) {
    this.nom = nom;
    this.prenom = prenom;
}

public String getNom() {
    return nom;
}

public void setNom(String nom) {
    this.nom = nom;
}

public String getPrenom() {
    return prenom;
}

public void setPrenom(String prenom) {
    this.prenom = prenom;
}

}

Person.xml

   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibis.apache.org//DTD SQL Map 2.0//EN"    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Person">
<insert id="insert" parameterClass="controleur.Person" >
insert into person(nom, pren)values(#nom#, #prenom#)
</insert>
</sqlMap>

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">

<settings useStatementNamespaces="true"></settings>
<transactionManager type="JDBC">
<dataSource type="simple">
 <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"      value="jdbc:mysql://localhost:3306/parc"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="config/Person.xml "/>

我使用了两个包:package config包含* .xml文件,包controleur包含servlet和java类。 感谢

1 个答案:

答案 0 :(得分:0)

将xml文件移动到Pojo所在的位置,在您的示例中将其移动到Person.java所在的controleur包下,然后按如下所示更新sqlMap。

<sqlMap resource="controleur/Person.xml" />