Please note, new blog at http://www.acheron.org/darryl/

ColdFusion vs. Java: Connecting to ODBC database

In my last post, I was replicating an ColdFusion array of structures in Java. This time around, I thought I would compare connecting to an Access database, executing a query, and then outputting the resultset to screen. The main thing to appreciate is how much ColdFusion does behind the scenes. You really don't have to do much, whereas in Java there is a little more to it. The tag syntax for CFML allows you to easily write complex queries, whereas this would be harder with Java. ColdFusion code <cfquery datasource="cfartgallery" name="qArtists"> SELECT * FROM artists ORDER BY lastname, firstname </cfquery> <cfoutput query="qArtists"> ID: #qArtists.artistid#<br/> Last name: #qArtists.lastname#<br/> First name: #qArtists.firstname#<br/> <br/> </cfoutput> Java code try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:odbc:artgallery"); Statement smnt = conn.createStatement(); ResultSet res = smnt.executeQuery("SELECT * FROM artists " + "ORDER BY lastname, firstname"); while(res.next()) { System.out.println("ID:" + res.getInt("artistid")); System.out.println("Last name:" + res.getString("lastname")); System.out.println("First name:" + res.getString("firstname")); System.out.println(); } conn.close(); } catch (SQLException se) { System.out.println("SQLException: " + se.getMessage()); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); }

By Anonymous Anonymous, at 4/21/2005 02:21:00 pm  

You have looked at ASP haven't you?

-
Scott.



By Blogger Darryl Lyons, at 4/21/2005 06:59:00 pm  

Not recently. Do you mean, ASP is pretty similar, or?

Looking at an example from W3CSchools.com:

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn

do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br />")
next
Response.Write("<br />")
rs.MoveNext
loop

rs.close
conn.close
%>



By Anonymous Anonymous, at 6/23/2006 02:41:00 am  

So easy with Coldfusion, right? Try to make a select over a table with 1 milion rows (or much less).. Good by coldfusion..

[]s



By Anonymous Anonymous, at 6/26/2006 04:16:00 am  

Why would you want to return one million rows to a client app?

That's bad programming no matter what language you're using. Most DBAs would shoot you.



» Post a Comment