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:
-
<%@ page import="java.sql.*" %>
-
<%
-
try
-
{
-
//this is how you might get a POST or GET variable from the request to use
-
//String user_id = request.getParameter("some_var");
-
-
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@HOSTNAME:PORT:SID","USERNAME","PASSWORD");
-
if (rset.next())
-
{
-
out.println(rset.getString(1));
-
}
-
else
-
{
-
out.println("No records found");
-
}
-
rset.close();
-
stmt.close();
-
}
-
out.println("Exception");
-
}
-
%>
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: code, driver, java, jdbc, jsp, oracle, oracle thin, programming, query, sql, thin, web
PHP 5 readfile Problem
April 26, 2006 | 3 Comments
In 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
-
function readfile_chunked($filename,$retbytes=true)
-
{
-
$chunksize = 1*(1024*1024); // how many bytes per chunk
-
$buffer = '';
-
$cnt =0;
-
// $handle = fopen($filename, 'rb');
-
if ($handle === false)
-
{
-
return false;
-
}
-
{
-
echo $buffer;
-
if ($retbytes)
-
{
-
}
-
}
-
if ($retbytes && $status)
-
{
-
return $cnt; // return num. bytes delivered like readfile() does.
-
}
-
return $status;
-
}
-
?>
Tags: code, development, file, file handling, flash, large file size, php, programming, readfile
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".
Tags: code, dba, definition, disk turd, jon graton, linux, programming, shell script, solaris, unix
A Couple MySQL Administrative Queries
March 27, 2006 | 1 Comment
There 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;
Tags: code, grant, mysql, set global, sql
PHP Screenscraping Using Curl
October 22, 2005 | 12 Comments
Tim 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
-
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, "http://zip4.usps.com/zip4/zcl_3_results.jsp");
-
curl_setopt ($ch, CURLOPT_POST, 1);
-
curl_setopt ($ch, CURLOPT_POSTFIELDS, "zip5=".$_GET['zip']);
-
-
$data = curl_exec($ch);
-
-
-
-
-
-
?>
Tags: address information, code, curl, php, programming, REST, screen scraping, soap, usps, web20, zip, zip code, zip codes
