210 likes | 374 Views
MDCFUG Is Java in Your Future?. Tyler Williams Principal Consultant @ dataTerrace twilliams@dataterrace.com. Agenda. The Java Learning Curve Why This is a Good Thing J2EE Breakdown Sample Program Q & A. My Background. Consultant 2 1/2 years Certified CF Developer 4 years CF
E N D
MDCFUGIs Java in Your Future? Tyler Williams Principal Consultant @ dataTerrace twilliams@dataterrace.com
Agenda • The Java Learning Curve • Why This is a Good Thing • J2EE Breakdown • Sample Program • Q & A
My Background • Consultant 2 1/2 years • Certified CF Developer • 4 years CF • 1 year Java • 6 years Oracle
Java Language Barriers • Non-Tag Based • Lower Level Than CF (more code) • Object Oriented • Strongly Typed & Case Sensitive • Large “Function Library” • Powerful, Complex and Less Forgiving
Power of Perception • CF is Easy to Learn • Ease of Entry Brings Less Qualified Programmers to the Language • More Low Grade Programs are Written • CF Language Gets a Bad Rap
The Good Side of Barriers • Less Competition • Higher Rates • Protected Reputation
J2EE - Enterprise Java • Server-side Web Development • Standard Created by Sun • Many Competing Application Servers • App Servers Must Pass a Compatibility Test Suite • Vendors Add Extensions & JSP Tag Libraries
J2EE Technologies • Lower Complexity • Java Server Pages (JSP) • Servlets • Higher Complexity • JDBC (Database Connectivity) • Enterprise Java Beans (EJB) • Java Messaging Service (JMS)
JSP Example <%@ page info="Simple JSP Page example" %> <% String str = "8th"; %> <html> <head><title>Simple JSP Page</title></head> <body bgcolor="#ffffff"> <h1>Welcome to the <%= str %> dimension</h1> </body> </html>
Servlet Example import java.io.*; import servlet.*; import servlet.http.*; public class SimpleServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(“<html><head><title>Simple Servlet Example</title></head>”); out.println(“<body bgcolor="#ffffff">”); String str = "8th"; out.println(“<h1>Welcome to the ” + str + “ dimension</h1>”); out.println(“</body></html>”); } }
JDBC Example Class.forName("oracle.jdbc.driver.OracleDriver"); String stUrl_= "jdbc:oracle:thin:@host:1521:sid"; Connection connection_ = DriverManager.getConnection(stUrl_, ”username",”password"); PreparedStatement statement_ = connection_.prepareStatement("SELECT fname, lname FROM EMPLOYEE"); ResultSet rs = statement_.executeQuery(); //execute query while (rs.next()) { String firstName = rs.getString(1); String lastName = rs.getString(2); strOut = strOut + firstName + " " + lastName + "<BR>"; } rs.close(); statement_.close(); connection_.close();
CF Comparison <cfquery name="main_select" datasource="MYDS"> SELECT fname, lname FROM EMPLOYEE </cfquery> <cfoutput query="main_select"> #main_select.fname# #main_select.lname# </cfoutput>
JDBC Metadata • getColumnCount - Returns the number of columns in this Result Set • getColumnName - Get the designated column's name • getColumnType - Retrieve the designated column's SQL type • isAutoIncrement - Indicates whether the designated column is automatically numbered, thus read-only
Model-View-Controller (MVC) • Model = Data = JDBC • View = Presentation = JSP • Controller = Framework = Servlet
MVC Flow Client 1) Client submits a page request 2) Servlet interprets request 3) Servlet retrieves necessary info from JDBC 4) Servlet hands off request and details to JSP for formatting and presentation Servlet JDBC JSP
Complete MVC Example • DimensionApp.java (Servlet) • DimensionVO.java (JDCB Class) • Display.jsp (JSP)
DimensionApp.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import DimensionVO; public class DimensionApp extends HttpServlet { public void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { DimensionVO dvo = new DimensionVO(); String mydim = dvo.getDimension(); req.setAttribute("dimension", mydim); RequestDispatcher rd; rd = getServletContext().getRequestDispatcher("/jsp/Display.jsp"); rd.forward(req, res); } }
DimensionVO.java import java.sql.*; import java.util.*; import java.io.*; public class DimensionVO { String strOut; String stUrl_= "jdbc:odbc:mdcfug"; private Connection connection_ = null; private PreparedStatement statement_ = null; private ResultSet rs; public String getDimension () throws IOException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection_ = DriverManager.getConnection(stUrl_, "Admin",""); statement_ = connection_.prepareStatement("SELECT dimension FROM DIMENSIONS"); rs = statement_.executeQuery(); //execute query String mydim = rs.getString(1); strOut = mydim; rs.close(); statement_.close(); connection_.close(); } catch (ClassNotFoundException e) {// Shouldn't happen - it comes with the JRE} catch (SQLException e) {System.err.println("Unable to connect to the DB " + e);} return strOut; } }
Display.jsp <%@ page info="MVC Example" %> <%String str = (String)request.getAttribute("dimension");%> <html> <head><title>MVC Example</title></head> <body bgcolor="#ffffff"> <table> <tr> <td width=150> </td> <td width=250 align=right> <h1>Welcome to the <%= str %> dimension</h1> </td> </tr> </table> </body> </html>
Java Resources • Java’s Home @ Sun • http://java.sun.com • Java Portals • http://www.theserverside.com • http://www.jguru.com/ • Sun’s JDBC Tutorial • http://developer.java.sun.com/developer/onlineTraining/Database/JDBC20Intro/
Java Bookshelf • The Java Language • Java in a Nutshell - Flanagan • Servlets/JSP • Java Servlet Programming - Hunter • Web Development With JavaServer Pages - Fields/Kolb • Core Servlets and JavaServer Pages - Hall • J2EE Application Architecture • Core J2EE Patterns - Alur