{"id":595,"date":"2004-05-10T10:25:59","date_gmt":"2004-05-10T14:25:59","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=595"},"modified":"2004-05-10T10:25:59","modified_gmt":"2004-05-10T14:25:59","slug":"coldfusion-mx-and-javasql","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/05\/10\/coldfusion-mx-and-javasql\/","title":{"rendered":"ColdFusion MX and java.sql"},"content":{"rendered":"<p>Last week a reader sent me an interesting problem.  When deleting a record from a database using a standard &lt;cfquery&gt; tag you don&#8217;t get any indication back that let&#8217;s you know whether or not the row or rows in question actually existed, nor do you get back any indication as to how many rows might have been affected. He wanted to be able run a single query that deleted a record AND let him know whether or not the record existed. He thought (correctly) that there was a way to do this in Java and wondered if I could write a CFX tag to do it for him.  I could, but it would be much easier to do it in &lt;cfscript&gt;.  In Java, you would write this:<br \/>\n<code><br \/>\n\/\/ replace the package\/class name of your db driver<br \/>\nClass.forName(\"org.gjt.mm.mysql.Driver\");<br \/>\n\/\/ replace w\/ your server, database name, username &amp; password<br \/>\nString conurl = \"jdbc:mysql:\/\/server\/db?user=u&amp;password=p\";<br \/>\nConnection con = DriverManager.getConnection(conurl);<br \/>\nPreparedStatement ps;<br \/>\nps = con.prepareStatement(\"DELETE from table_name where id = 3045\");<br \/>\nint result = ps.executeUpdate();<br \/>\ncon.close();<br \/>\n<\/code><br \/>\nIn short, you load the appropriate database driver (I&#8217;m using MySQL in the above example), obtain a connection to the database using the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/sql\/DriverManager.html\">DriverManager<\/a> class, create a <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/sql\/PreparedStatement.html\">PreparedStatement<\/a>, and then call the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/sql\/PreparedStatement.html#executeUpdate()\">executeUpdate()<\/a> method.  This method returns an int: &#8220;&#8230; either (1) the row count for INSERT, UPDATE, or DELETE statements or (2) 0 for SQL statements that return nothing.&#8221;<\/p>\n<p>Using the above sample it&#8217;s pretty easy to do the same thing in ColdFusion using &lt;cfscript&gt;:<br \/>\n<code><br \/>\n&lt;cfscript&gt;<br \/>\nclazz = CreateObject(\"java\", \"java.lang.Class\");<br \/>\n\/\/ replace the package\/class name of your db driver<br \/>\nclazz.forName(\"org.gjt.mm.mysql.Driver\");<br \/>\ndriverManager = CreateObject(\"java\", \"java.sql.DriverManager\");<br \/>\n\/\/ replace w\/ your server, database name, username &amp; password<br \/>\nconurl = \"jdbc:mysql:\/\/server\/db?user=u&amp;password=p\";<br \/>\nconnection = driverManager.getConnection(conurl);<br \/>\nquery = \"DELETE from category where id = 3045\";<br \/>\npreparedStatement = connection.prepareStatement(query);<br \/>\nresult = preparedStatement.executeUpdate();<br \/>\nWriteOutput(\"result = \" &amp; result);<br \/>\nconnection.close();<br \/>\n&lt;\/cfscript&gt;<br \/>\n<\/code><br \/>\nFurther, you can test the value of the result variable so that you can show a custom message to a end user or perform some other type of business logic:<br \/>\n<code><br \/>\nif (result == \"0\") {<br \/>\n&nbsp;&nbsp;WriteOutput(\"No records were deleted.\");<br \/>\n} else if (result == \"1\") {<br \/>\n&nbsp;&nbsp;WriteOutput(\"One record was deleted.\");<br \/>\n} else {<br \/>\n&nbsp;&nbsp;WriteOutput(result &amp; \" records were deleted.\");<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week a reader sent me an interesting problem. When deleting a record from a database using a standard &lt;cfquery&gt; tag you don&#8217;t get any indication back that let&#8217;s you know whether or not the row or rows in question actually existed, nor do you get back any indication as to how many rows might &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/05\/10\/coldfusion-mx-and-javasql\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ColdFusion MX and java.sql<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/595"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}