ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From philip brown <brown_phi...@yahoo.com>
Subject Sunil Patels ibataisDAO tutorial - help please
Date Tue, 22 Nov 2005 10:26:01 GMT
I am new to Java  and would really appreciate any help
with this as it probably is only a beginners
oversight/mistake.
I have submitted all code (see below or
http://www.oceanair.co.nz/phil/sunil.zip  for all
files including libs)

MySql database is set up as in SQLMap.xml and
Contact.xml

I added contactDetail.class as I recieved messages
"bean  "contactDetail" not found within scope"

"Is it connecting to Database at all?" I looked at 
ibatisDAO docs as best as I could and feel that I may
have a mistake here in SQLMap.xml as follows?

<-- /WEB-INF/classes/net/primera/dao/sql/SqlMap.xml +
/WEB-INF/src//net/primera/dao/sql/SqlMap.xml
------------------------------------------>

<?xml version="1.0" encoding="UTF-8"?>
<!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"
errorTracingEnabled="false" maxRequests="32"  
maxSessions="10"    maxTransactions="5"   
useStatementNamespaces="false" />
  	<transactionManager type="JDBC">
	    <dataSource type="SIMPLE" >
		<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
		<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost/tomcat"/>
		<property name="JDBC.Username" value="phil"/>
		<property name="JDBC.Password" value="5E1te1c0s"/>
		<property name="JDBC.DefaultAutoCommit" value="true"
/>
	  	</dataSource>
	</transactionManager>
  <sqlMap
resource="net/primera/dao/sqlmap/Contact.xml"/>
</sqlMapConfig>

<--------------------------------------------------------------------------------------------------------------------------------------->

 Secondly,
 If it is contacting, i think the major culprit wiuld
be in this class. Was it right to add contactDetail
class?
 
  <--
/WEB-INF/src/net/primera/ContactSelectAction.java 
------------------------------------------------------------------------->

package net.primera;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import net.primera.dao.ContactDAO;
import net.primera.Contact;
public class ContactSelectAction extends Action {
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception {
		try {
			Contact contactForm = (Contact) form;
			Reader reader =
Resources.getResourceAsReader("DAOMap.xml");
			DaoManager daoManager =
DaoManagerBuilder.buildDaoManager(reader);
			ContactDAO contactDAO =
				(ContactDAO) daoManager.getDao( ContactDAO.class,
"sqlmap" );
			request.setAttribute("contactDetail",
contactDAO.selectContact(contactForm));
		} catch (Exception e) {
			mapping.findForward("failure");
		}
		return mapping.findForward("success");
	}
}

<--------------------------------------------------------------------------------------------------------------------------------------->

Thanks again if you can look at this,

Phil

full code to follow below
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
<-- /Select.jsp
------------------------------------------------------------------------------------>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld"
prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<META http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type"
content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
type="text/css">
<TITLE>Select.jsp</TITLE>
</HEAD>
<BODY>
<h3>Enter ContactId for Contact you want to
search</h3>
<html:form action="selectContact">
	<TABLE border="0">
		<TBODY>
			<TR>
				<TH>Contact Id</TH>
				<TD><html:text property='contactId' /></TD>
			</TR>
			<TR>
				<TD><html:submit property="submit" value="Submit"
/></TD>
				<TD><html:reset /></TD>
			</TR>
		</TBODY>
	</TABLE>
</html:form>
</BODY>
</html:html>

<-- /Success.jsp
------------------------------------------------------------------------------------>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<HTML>
<HEAD>
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<META http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type"
content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
	type="text/css">
<TITLE>Success.jsp</TITLE>
</HEAD>
<BODY>
<jsp:useBean scope="request" id="contactDetail"
type="net.primera.Contact"/> 
<h3>Update Contact Details</h3>
<table>
<tr>
	<td>Contact Id</td>
	<td><jsp:getProperty name="contactDetail"
property="contactId"/></td>
</tr>
<tr>
	<td>First Name</td>
	<td><jsp:getProperty name="contactDetail"
property="firstName"/></td>
</tr>
<tr>
	<td>Last Name</td>
	<td><jsp:getProperty name="contactDetail"
property="lastName"/></td>
</tr>
<tr>
	<td><a href="index.jsp">Go back to summary
page</a></td>
</tr>
</table>
</BODY>
</HTML>

<-- /WEB-INF/struts-config.xml
--------------------------------------------------------------------------------------------------->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software
Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
	<form-beans>
		<form-bean name="contact"
type="net.primera.Contact">
		</form-bean>
		<form-bean name="contactDetail"
type="net.primera.ContactDetail">
		</form-bean>	
	</form-beans>
		<action path="/selectContact"
type="net.primera.ContactSelectAction" name="contact"
scope="request" input="/Select.jsp">
			<forward name="success" path="/Success.jsp">
			</forward>
			<forward name="failure" path="/failure.jsp">
			</forward>
		</action>
	</action-mappings>
	<message-resources
parameter="resources.MessageResources"/>
</struts-config>

<-- /WEB-INF/classes/DAOMap.xml +
/WEB-INF/src/DAOMap.xml
--------------------------------------------------------------------------------->

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE daoConfig PUBLIC "-//iBATIS.com//DTD DAO
Configuration 2.0//EN"
"http://www.ibatis.com/dtd/dao-2.dtd">
<daoConfig>
	<context id="sqlmap">
		<transactionManager type="SQLMAP">
			<property name="SqlMapConfigResource"
value="net/primera/dao/sqlmap/SqlMapConfig.xml"/>
		</transactionManager>
		<dao interface="net.primera.dao.ContactDAO"
implementation="net.primera.dao.sqlmap.SQLMapContactDAO"
/>
	</context>
</daoConfig>

<-- /WEB-INF/classes/net/primera/dao/sql/SqlMap.xml +
/WEB-INF/src//net/primera/dao/sql/SqlMap.xml
------------------------------------------>

<?xml version="1.0" encoding="UTF-8"?>
<!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"
errorTracingEnabled="false" maxRequests="32"  
maxSessions="10"    maxTransactions="5"   
useStatementNamespaces="false" />
  	<transactionManager type="JDBC">
	    <dataSource type="SIMPLE" >
		<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
		<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost/tomcat"/>
		<property name="JDBC.Username" value="phil"/>
		<property name="JDBC.Password" value="5E1te1c0s"/>
		<property name="JDBC.DefaultAutoCommit" value="true"
/>
	  	</dataSource>
	</transactionManager>
  <sqlMap
resource="net/primera/dao/sqlmap/Contact.xml"/>
</sqlMapConfig>

<-- /WEB-INF/classes/net/primera/dao/sql/Contact.xml +
/WEB-INF/src//net/primera/dao/sql/Contact.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 namespace="Contact">
  <typeAlias alias="contact"
type="com.sample.contact.Contact"/>
  <select id="getContact" parameterClass="int"
resultClass="contact">
    select FIRSTNAME as firstName,LASTNAME  as
lastName, CONTACTID as contactId from tomcat.contact
where CONTACTID = #contactId#
  </select>
  <insert id="insertContact" parameterClass="contact">
  	INSERT INTO
tomcat.contact(CONTACTID,FIRSTNAME,LASTNAME)
values(#contactId#,#firstName#,#lastName#)
  </insert>
  <update id="updateContact" parameterClass="contact">
  	UPDATE tomcat.contact SET FIRSTNAME=#firstName#,
LASTNAME = #lastName#  where CONTACTID=#contactId#
  </update>
  <delete id="deleteContact" parameterClass="int">
  	DELETE FROM tomcat.contact WHERE
CONTACTID=#contactId#
  </delete>
 </sqlMap>
 
 <--
/WEB-INF/src/net/primera/dao/sqlmap/SQLMapContactDAO.java

------------------------------------------------------------------------->
 
package net.primera.dao.sqlmap;
import com.ibatis.dao.client.DaoManager;
import
com.ibatis.dao.client.template.SqlMapDaoTemplate;
import net.primera.Contact;
import net.primera.dao.ContactDAO;
public class SQLMapContactDAO extends
SqlMapDaoTemplate implements ContactDAO {
	public SQLMapContactDAO(DaoManager arg0) {
		super(arg0);
	}
	public Contact selectContact(int contactId) {
		return (Contact)super.queryForObject("getContact",
new Integer(contactId));
	}
}

  <-- /WEB-INF/src/net/primera/dao/ContactDAO.java 
------------------------------------------------------------------------->
  
  package net.primera.dao;
import com.ibatis.dao.client.Dao;
import net.primera.*;
public interface ContactDAO extends Dao{
	public int insertContact(Contact contact);
	public int updateContact(Contact contact);
	public Contact selectContact(int contactId);
	public int deleteContact(int contactId);
}

  <-- /WEB-INF/src/net/primera/Contact.java 
------------------------------------------------------------------------->
  
package net.primera;
import org.apache.struts.action.ActionForm;
public class Contact extends ActionForm{
	private int contactId;
	private String firstName;
	private String lastName;
	public int getContactId() {
		return contactId;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setContactId(int contactId) {
		this.contactId = contactId;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

  <-- /WEB-INF/src/net/primera/ContactDetail.java 
------------------------------------------------------------------------->
  
package net.primera;
import org.apache.struts.action.ActionForm;
public class ContactDetail extends ActionForm{
	private int contactId;
	private String firstName;
	private String lastName;
	public int getContactId() {
		return contactId;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setContactId(int contactId) {
		this.contactId = contactId;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

  <--
/WEB-INF/src/net/primera/ContactSelectAction.java 
------------------------------------------------------------------------->

package net.primera;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import net.primera.dao.ContactDAO;
import net.primera.Contact;
public class ContactSelectAction extends Action {
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception {
		try {
			Contact contactForm = (Contact) form;
			Reader reader =
Resources.getResourceAsReader("DAOMap.xml");
			DaoManager daoManager =
DaoManagerBuilder.buildDaoManager(reader);
			ContactDAO contactDAO =
				(ContactDAO) daoManager.getDao( ContactDAO.class,
"sqlmap" );
			request.setAttribute("contactDetail",
contactDAO.selectContact(contactForm));
		} catch (Exception e) {
			mapping.findForward("failure");
		}
		return mapping.findForward("success");
	}
}


		
___________________________________________________________ 
Yahoo! Model Search 2005 - Find the next catwalk superstars - http://uk.news.yahoo.com/hot/model-search/

Mime
View raw message