Simple JSP JDBC Example

January 29, 2008 | Leave a Comment

I learned to program on Java, but have done little to exercise those skills in the last six years, preferring to leverage PHPs simplicity whenever possible. So... when I had a need to put together a simple JSP script that queried Oracle, I found myself scouring for a truly simple example to start from. Here's the example I put together:

JAVA:
  1. <%@ page import="java.sql.*" %>
  2. <%
  3. try
  4. {
  5.     //this is how you might get a POST or GET variable from the request to use
  6.     //String user_id = request.getParameter("some_var");
  7.  
  8.     Connection  conn = DriverManager.getConnection("jdbc:oracle:thin:@HOSTNAME:PORT:SID","USERNAME","PASSWORD");
  9.     Statement stmt = conn.createStatement();
  10.     ResultSet rset = stmt.executeQuery("SELECT sysdate FROM dual'");
  11.     if (rset.next())
  12.     {
  13.         out.println(rset.getString(1));
  14.     }
  15.     else
  16.     {
  17.         out.println("No records found");
  18.     }
  19.     rset.close();
  20.     stmt.close();
  21. }
  22. catch (SQLException e) {
  23.     out.println("Exception");
  24. }
  25. %>

Certainly for your own purposes you would need to write a lot more code than this. Yet, if you seek a simple example just to gain your bearings, this should do it.

Tags: , , , , , , , , , , ,

Related:

PHP 5 readfile Problem

April 26, 2006 | 3 Comments

PHP LogoIn PHP 5.0.x there is a known bug that can cause large (2MB+) files to be handled incorrectly by the readfile function. We first discovered this while working with Tiki Wiki. Today we ran into it again with the way we stream secured SWF Flash files off the file system.

Luckily, I found this great function on PHP.net in the user comment section. Simply drop this function definition in place and use it instead of readfile and the problem is gone!

PHP:
  1. <?php
  2. function readfile_chunked($filename,$retbytes=true)
  3. {
  4.    $chunksize = 1*(1024*1024); // how many bytes per chunk
  5.    $buffer = '';
  6.    $cnt =0;
  7.    // $handle = fopen($filename, 'rb');
  8.    $handle = fopen($filename, 'rb');
  9.    if ($handle === false)
  10.    {
  11.        return false;
  12.    }
  13.    while (!feof($handle))
  14.    {
  15.        $buffer = fread($handle, $chunksize);
  16.        echo $buffer;
  17.        if ($retbytes)
  18.        {
  19.            $cnt += strlen($buffer);
  20.        }
  21.    }
  22.    $status = fclose($handle);
  23.    if ($retbytes && $status)
  24.    {
  25.        return $cnt; // return num. bytes delivered like readfile() does.
  26.    }
  27.    return $status;
  28. }
  29. ?>

code, development, file, file handling, flash, large file size, php, programming, readfile

Tags: , , , , , , , ,

Related:

Definition: Code Turd

April 7, 2006 | 22 Comments

In a major enterprise system we pay a lot of money for, a recent patch threw us an error based on the following line of code in a shell script:

echo "I am what is running, this is linux" >> /home/ban7/jobsub/for_whatever_purpose.txt

This line was not conditional and was ironically being run on a Solaris system, not Linux. Our DBA Jon Graton defines these little gems of "production" code as "code turds".

programming, code, shell script, definition, unix, linux, solaris, disk turd, jon graton, dba

Tags: , , , , , , , , ,

Related:

A Couple MySQL Administrative Queries

March 27, 2006 | 1 Comment

MySQL LogoThere are a few tasks in MySQL which I do rarely and usually graphically. This in turn leads to sifting through lots of documentation to refresh my memory. Not anymore... here are those commands so I'll always know exactly where to find them.

First is a basic command for granting privileges to a user, the example shows all privs from a specific host to a user from a specific host including the ability to grant further privs to others:
GRANT ALL ON * TO 'username'@'hostname' IDENTIFIED BY 'password' WITH GRANT OPTION;

The above is not a good thing to do willy nilly. The command creates a superuser, though sometimes you need these...

The next thing i do more regularly these days is slip tuning configuration parameters in during runtime. This is useful if you've experienced some significant growth and want to tune you db without a restart. Keep in mind these settings will be lost if the database is restarted:
SET GLOBAL wait_timeout=60;

mysql, sql, code, grant, set global

Tags: , , , ,

Related:

PHP Screenscraping Using Curl

October 22, 2005 | 12 Comments

Php-1Tim was looking to try something new, so I decided to introduce him to Client URL (CURL) functions. As the example at hand, we looked at hitting the USPS site to lookup city and states based on ZIP code.

For the uninitiated, CURL basically lets you programmatically simulate a user browsing a web site. You can POST, GET, PUT, maintain cookies and session information. In the following example we are using a technique called "screen scraping" which is rarely recommended, but a good skill to have because sometimes its the only solution.

The reason its bad is because it is extremely fragile. If a webmaster of the site you are accessing makes even a slight change, it could break your page parsing. The other reason to shy away from this is some web sites really don't like when you do this. As a rule, if the webmaster of the site you are scraping contacts you and wants you to stop, you should, immediately. Though you should also recommend they provide the info you are scraping as a service through something like REST or SOAP. It would be very Web 2.0 of them to comply, it's worth a shot.

Anyway, check out this example code, it's kinda fun.

PHP:
  1. <?php
  2.  
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, "http://zip4.usps.com/zip4/zcl_3_results.jsp");
  5. curl_setopt ($ch, CURLOPT_POST, 1);
  6. curl_setopt ($ch, CURLOPT_POSTFIELDS, "zip5=".$_GET['zip']);
  7.  
  8. $data = curl_exec($ch);
  9. $string = ob_get_contents();
  10.  
  11.  
  12. list(,$second) = explode('Actual City name', $string);
  13. list($first) = explode('images/spacer.gif', $second);
  14. $junk = explode(“\n”,$first);
  15.  
  16. list($city,$state) = explode(', ',trim(strip_tags($junk[6])));
  17.  
  18. $city = ucwords(strtolower($city));
  19.  
  20. print $city.','.$state;
  21. ?>

web20, web2.0, code, programming, screen scraping, php, soap, rest, curl, usps, address information, zip code, zip, zip codes

Tags: , , , , , , , , , , , ,

Related: