Intro to Orion, Part 2
jdbc\lib
folder under your Oracle installation folder. For my installation, it is c:\Oracle\Ora81\jdbc\lib
.
Copy the .zip files in that folder to c:\orion\lib
.create user orionapp identified by orionapp;
and press Enter.grant connect, resource to orionapp;
commit;
connect orionapp/orionapp;
create table visitor ( visitor_id integer not null, first_name varchar2(20) not null, last_name varchar2(30) not null, email varchar2(60) not null, constraint pk_visitor primary key (visitor_id)); create sequence visitor_seq increment by 1 start with 1; |
commit;
DBAccess.java
. Make sure you save it in the orionapp\classes\orionapp\utils\
folder.
package orionapp.utils; import java.sql.*; import javax.naming.Context; import javax.sql.DataSource; public class DBAccess { private DataSource dataSource = null; private Connection con = null; private Statement st = null; public void getConnection(Context ctx) { try { DataSource dataSource = (DataSource)ctx.lookup("jdbc/OraclePooledDS"); con = dataSource.getConnection(); st = con.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public void releaseConnection() { try { if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet executeQuery(String p_strSql) { ResultSet rs = null; try { rs = st.executeQuery(p_strSql); } catch (Exception e) { e.printStackTrace(); } return rs; } public void executeUpdate(String p_strSql) { try { st.executeUpdate(p_strSql); } catch (Exception e) { e.printStackTrace(); } } } |
Visitor.java
. Make sure you save it in the orionapp\classes\orionapp\objects\
folder.
package orionapp.objects; public class Visitor implements java.io.Serializable { public String visitorId = null; public String firstName = null; public String lastName = null; public String email = null; public Visitor(String vId, String fNam, String lNam, String eml) { visitorId = vId; firstName = fNam; lastName = lNam; email = eml; } public String display() { return lastName + ", " + firstName + " (<i>" + email + "</i>)"; } } |
VisitorSet.java
. Make sure you save it in the orionapp\classes\orionapp\objects\
folder.
package orionapp.objects; import java.util.Vector; public class VisitorSet implements java.io.Serializable { public Vector visitorSet = new Vector(); public void add(Visitor vis) { visitorSet.add(vis); } public boolean isEmpty() { return visitorSet.isEmpty(); } public String display() { String rtn = "<TABLE WIDTH=\"400\" BORDER=\"0\">\r\n"; int Size = visitorSet.size(); for (int i = 0; i < Size; i++) { rtn += "<TR><TD>\r\n"+ ((Visitor)visitorSet.get(i)).display()+ "</TD></TR>\r\n"; } rtn += "</TABLE></DIV>\r\n"; return rtn; } } |
InfoHome.java
at all.Info.java
, adding the lines in blue to your code.
package orionapp.beans.Info; import javax.ejb.EJBObject; import java.rmi.RemoteException; |
insertVisitor
will insert a new visitor record into the database.
The method getVisitorSet
will return a VisitorSet
object containing the set of visitor records already entered in the database.InfoBean.java
, adding the lines in blue to your code.
package orionapp.beans.Info; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.InitialContext; |
visitor.jsp
in the orionapp\public_html
folder. This is a new JSP that we will add to our application.
<%@ page import=" javax.naming.*, javax.ejb.*, java.rmi.*, javax.rmi.PortableRemoteObject, orionapp.objects.VisitorSet, orionapp.beans.Info.*" %> <HTML><HEAD><TITLE>Test</TITLE></HEAD> <BODY> <% // variables for the Info bean InfoHome iHome = null; Info iBean = null; // check for form data String firstName = request.getParameter("txtFirstName"); String lastName = request.getParameter("txtLastName"); String email = request.getParameter("txtEmail"); // VisitorSet object returned from the Info bean VisitorSet vSet = null; try { InitialContext ctx = new InitialContext(); iHome = (InfoHome)PortableRemoteObject.narrow(ctx.lookup("java:comp/env/ejb/orionapp/beans/Info"), InfoHome.class); iBean = iHome.create(); } catch(Exception e) { e.printStackTrace(); } // if form data was submitted.... if ( (firstName != null) && (lastName != null) && (email != null) && (!firstName.equals("")) && (!lastName.equals("")) && (!email.equals("")) ) { iBean.insertVisitor(firstName, lastName, email); %> Visitor added. <A HREF="visitor.jsp">OK</A> <% } // end if form data was submitted else { vSet = iBean.getVisitorSet(); %> <FORM METHOD="get" action="visitor.jsp"> <BR>Enter first name: <INPUT TYPE="text" name="txtFirstName"><BR> <BR>Enter last name: <INPUT TYPE="text" name="txtLastName"><BR> <BR>Enter email: <INPUT TYPE="text" name="txtEmail"><BR> <BR><INPUT TYPE="submit" value="Submit"><BR> </FORM><BR><BR> <% if (vSet.isEmpty()) out.println("No visitor records in database"); else { out.println("<b>Existing Visitor Records:</b><br>"); out.println(vSet.display()); } } // end else %> </BODY></HTML> |
cmd
under Start > Run),
type cd\orionapp\classes
to get to your classes folder.javac orionapp\beans\Info\*.java
to recompile your Info bean classes.javac orionapp\objects\*.java
javac orionapp\utils\*.java
jar cvf objects.jar orionapp\objects\*.class
jar cvf utils.jar orionapp\utils\*.class
c:\orion\lib
folder for Orion to detect them.
Also note that Orion does not automatically detect changes to files in the lib folder, so you must shut down, then restart Orion after making changes to these files. If anyone knows a better way to do this, please share your knowledge. =)deploytool
, if you do not have it running already.cd\orionapp\classes
) and running it from there.orionapp.ear
if it is not already open (you should have saved it in c:\orion\applications
).orionapp
if public_html
is not showing. Click on public_html
, then click Add
under the Contents window of the General tab. (If you cannot see any files in the Contents window, enlarge the deploytool console.)
Set your Root Directory to c:\orionapp\public_html
and click on visitor.jsp
. Click Add
, then Next
, then Finish
. You have just added the new JSP to your application.data-sources.xml
file in the Orion config folder. Add the following block of text before the </data-sources> tag:
<data-source class="com.evermind.sql.DriverManagerDataSource" name="Oracle" location="jdbc/OracleCoreDS" xa-location="jdbc/xa/OracleXADS" ejb-location="jdbc/OracleDS" pooled-location="jdbc/OraclePooledDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="orionapp" password="orionapp" url="jdbc:oracle:thin:@ |
computername
with the name of your computer, or the name of the computer on your network that is running Oracle.
Replace databasename
with the name you gave your database. If you do not remember, go to Start > Programs > Oracle > Database Administration > Oracle Administration Assistant. Under the Tree tab, keep expanding the folders until you see one called "Databases". Expand that one to see the name of your database.http://localhost/orionapp/visitor.jsp
as the URL. Enter some data. If all goes well, your new application will work! Congratulations, you have just created a 3-tier J2EE application! Start submitting resumes now. ;)
Start Service
. Give it a moment to start up.