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:

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:

Installing mytop

July 15, 2005 | 3 Comments

Mysql 100X52-64Anyone who's done much MySQL administration probably knows about mytop. mytop is basically a top clone for MySQL. Hugely useful if you want info about what your database is up to, but don't necessarily have pr want MySQL Administrator installed.

I decided to install this on my FreeBSD box, though I'm sure the instructions would be similar on OSX, Solaris, or any other unix flavor. Sometimes the installation can be cumbersome and everytime I do it I find myself fumbling around trying to remember what I did last time. Unfortunately my systems administration skills aren't always as solid as I like. So here goes.

1) Get mytop
2) unpack it in some directory (tar xvf mytop-1.4.tar.gz)
3) add in the necessary perl modules (assuming you don't already have them)
perl -MCPAN -e shell
then at the cpan prompt:
install DBI
install DBD::mysql (this one may fail if your root password for localhost is set. in this case do `force install DBD::mysql`)
install Term::ReadKey
install Term::ANSIColor
install Time::HiRes
4) In the directory where you extracted mytop execute:
perl Makefile.PL
make
make test
make install
5) You can then execute it like:
./mytop -u<user> -prompt -h<hostname>

6) For additional options see: perldoc mytop. Also try hitting '?' when running mytop for runtime options.

cpan, database, database administration, freebsd, install instructions, mysql, osx, perl, perl cpan, solaris, sql, systems administration, unix

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

Related: