本人初學ibatis,仿照ibatis官方網站的文檔作了個例子,可是系統始終出現一個問題,源碼如下,還請高手指教
1. SqlMapConfig.xml
<?xml version="1.0" encoding="big5" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"
maxRequests="32" maxSessions="10" maxTransactions="5"
useStatementNamespaces="true" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE" >
<property name="JDBC.Driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:Microsoft:sqlserver://10.186.33.43:1433;database=IBatis"/>
<property name="JDBC.Username" value="sa"/>
<property name="JDBC.Password" value="danny" />
</dataSource>
</transactionManager>
<sqlMap resource="hello.xml"/>
</sqlMapConfig>
2. hello.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap>
<resultMap id="votes" class="examples.domain.Person">
<result property="id" column="id"/>
<result property="firstName" column="firstName"/>
<result property="lastName" column="lastName"/>
<result property="birthDate" column="birthDate"/>
<result property="weightInKilograms" column="weightInKilograms"/>
<result property="heightInMeters" column="heightInMeters"/>
</resultMap>
<select id="getContacts" parameterClass="java.lang.Integer" resultMap="votes">
SELECT PER_ID AS id, PER_FIRST_NAME AS firstName, PER_LAST_NAME AS lastName,
PER_BIRTH_DATE AS birthDate, PER_WEIGHT_KG AS weightInKilograms,
PER_HEIGHT_M AS heightInMeters FROM person
WHERE PER_ID =#value#
</select>
</sqlMap>
3. Person.java
package examples.domain;public class Person {
public Person() {
}
public int getid(){
return id;
}
public void setId(int iId){
id = iId;
}
public String getfirstName(){
return firstName;
}
public void setfirstName(String strName){
firstName = strName;
}
public String getlastName(){
return lastName;
}
public void setlastName(String strName){
lastName = strName;
}
public String getbirthDate(){
return birthDate;
}
public void setbirthDate(String strDate){
birthDate = strDate;
}
public int getweightInKilograms(){
return weightInKilograms;
}
public void setweightInKilograms(int iCount){
weightInKilograms = iCount;
}
public int getheightInKilograms(){
return heightInMeters;
}
public void setheightInMeters(int iCount){
heightInMeters = iCount;
} private int id;
private String firstName;
private String lastName;
private String birthDate;
private int weightInKilograms;
private int heightInMeters;
}
4. index.jsp
<%@ page contentType="text/html; charset=Big5" %>
<%@ page import="examples.domain.Person,com.ibatis.sqlmap.client.SqlMapClient,com.foxconn.MyAppSQLConfig" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<%
SqlMapClient sqlMap = MyAppSQLConfig.getSqlMapInstance();
String strName = "864373907";
Integer id = new Integer(33);
Person person = (Person)sqlMap.queryForObject("getContacts",id);
//out.print(person.getid());%>
</body>系統報的錯誤為:
javax.servlet.ServletException:
--- The error occurred in hello.xml.
--- The error occurred while applying a parameter map.
--- Check the getContacts-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid object name 'person'.
1. SqlMapConfig.xml
<?xml version="1.0" encoding="big5" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"
maxRequests="32" maxSessions="10" maxTransactions="5"
useStatementNamespaces="true" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE" >
<property name="JDBC.Driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:Microsoft:sqlserver://10.186.33.43:1433;database=IBatis"/>
<property name="JDBC.Username" value="sa"/>
<property name="JDBC.Password" value="danny" />
</dataSource>
</transactionManager>
<sqlMap resource="hello.xml"/>
</sqlMapConfig>
2. hello.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap>
<resultMap id="votes" class="examples.domain.Person">
<result property="id" column="id"/>
<result property="firstName" column="firstName"/>
<result property="lastName" column="lastName"/>
<result property="birthDate" column="birthDate"/>
<result property="weightInKilograms" column="weightInKilograms"/>
<result property="heightInMeters" column="heightInMeters"/>
</resultMap>
<select id="getContacts" parameterClass="java.lang.Integer" resultMap="votes">
SELECT PER_ID AS id, PER_FIRST_NAME AS firstName, PER_LAST_NAME AS lastName,
PER_BIRTH_DATE AS birthDate, PER_WEIGHT_KG AS weightInKilograms,
PER_HEIGHT_M AS heightInMeters FROM person
WHERE PER_ID =#value#
</select>
</sqlMap>
3. Person.java
package examples.domain;public class Person {
public Person() {
}
public int getid(){
return id;
}
public void setId(int iId){
id = iId;
}
public String getfirstName(){
return firstName;
}
public void setfirstName(String strName){
firstName = strName;
}
public String getlastName(){
return lastName;
}
public void setlastName(String strName){
lastName = strName;
}
public String getbirthDate(){
return birthDate;
}
public void setbirthDate(String strDate){
birthDate = strDate;
}
public int getweightInKilograms(){
return weightInKilograms;
}
public void setweightInKilograms(int iCount){
weightInKilograms = iCount;
}
public int getheightInKilograms(){
return heightInMeters;
}
public void setheightInMeters(int iCount){
heightInMeters = iCount;
} private int id;
private String firstName;
private String lastName;
private String birthDate;
private int weightInKilograms;
private int heightInMeters;
}
4. index.jsp
<%@ page contentType="text/html; charset=Big5" %>
<%@ page import="examples.domain.Person,com.ibatis.sqlmap.client.SqlMapClient,com.foxconn.MyAppSQLConfig" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<%
SqlMapClient sqlMap = MyAppSQLConfig.getSqlMapInstance();
String strName = "864373907";
Integer id = new Integer(33);
Person person = (Person)sqlMap.queryForObject("getContacts",id);
//out.print(person.getid());%>
</body>系統報的錯誤為:
javax.servlet.ServletException:
--- The error occurred in hello.xml.
--- The error occurred while applying a parameter map.
--- Check the getContacts-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid object name 'person'.
解决方案 »
- 怎样在POI中HSSFWorkBook中的Sheet拷贝到另一个HSSFWorkBook中去
- 启动server时爆两个Warning
- 提一个菜问题,jsp页面相关
- 在Tomcat的admin中如何连接MYSQL数据库
- jsp 知道一张图片的路径,怎么下载到本地的电脑上
- winME和winXP下是否可以配置JSP服务器?我试了多次都不成功,特此请教!
- 我该选择ASP 还是JSP好?
- 我对JSP这个论坛真是失望!!!!!!!!!
- 请教:关于英文,简体中文,繁体中文在JSP/JAVA中的整体解决方案!!!!!
- 什么是集中认证?什么是联邦认证?
- 求jsp时间控件
- 使用JBuild2006,我该如何创建能实现自动编译的效果呢?具体步骤是什么呢?
return id;
}
public void setId(int iId){
id = iId;
}
public String getfirstName(){
return firstName;
}
public void setfirstName(String strName){
firstName = strName;
}
public String getlastName(){
return lastName;
}
public void setlastName(String strName){
lastName = strName;
}
public String getbirthDate(){
return birthDate;
}
public void setbirthDate(String strDate){
birthDate = strDate;
}
public int getweightInKilograms(){
return weightInKilograms;
}
public void setweightInKilograms(int iCount){
weightInKilograms = iCount;
}
public int getheightInKilograms(){
return heightInMeters;
}
public void setheightInMeters(int iCount){
heightInMeters = iCount;
}这一截代码是你手写的吗?get和set方法中属性名第一个字母怎么不是大写。
如getid(),应该是getId()
剛才照你說的把getid()改為getId(),可是問題依然存在.
源碼該後:
package examples.domain;public class Person {
public Person() {
}
public int getId(){
return id;
}
public void setId(int iId){
id = iId;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String strName){
firstName = strName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String strName){
lastName = strName;
}
public String getBirthDate(){
return birthDate;
}
public void setBirthDate(String strDate){
birthDate = strDate;
}
public int getWeightInKilograms(){
return weightInKilograms;
}
public void setWeightInKilograms(int iCount){
weightInKilograms = iCount;
}
public int getHeightInKilograms(){
return heightInMeters;
}
public void setHeightInMeters(int iCount){
heightInMeters = iCount;
} private int id;
private String firstName;
private String lastName;
private String birthDate;
private int weightInKilograms;
private int heightInMeters;}
HttpConnector[8083] Starting background threadStandardWrapperValve[debugjsp]: Servlet.service() for servlet debugjsp threw exceptionjavax.servlet.ServletException:
--- The error occurred in hello.xml.
--- The error occurred while applying a parameter map.
--- Check the getContacts-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid object name 'person'.javax.servlet.ServletException:
--- The error occurred in hello.xml.
--- The error occurred while applying a parameter map.
--- Check the getContacts-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid object name 'person'. at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471) at org.apache.jsp.index$jsp._jspService(index$jsp.java:81) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174) at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943) at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027) at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125) at java.lang.Thread.run(Thread.java:536)
你也可以这样写:
<statement id="getPerson" parameterClass=”int” resultClass="examples.domain.Person">
SELECT PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</statement>
問題已經解決了,原因是:
<property name="JDBC.ConnectionURL" value="jdbc:Microsoft:sqlserver://10.186.33.43:1433;database=IBatis"/>
應寫為:
<property name="JDBC.ConnectionURL" value="jdbc:Microsoft:sqlserver://10.186.33.43:1433;databasename=IBatis"/>
,希望大家以後寫程序的時候,也格外小心些,有時候一個很小的錯誤,也許要花好長的時間才能發現!