Monday, August 31, 2009

Connecting to a MySQL database using JDBC

I found this article to be pretty useful when learning to connect to a MySQL database using JDBC.

http://www.kitebird.com/articles/jdbc.html

Thursday, April 16, 2009

Mo' quotes

"Public opinion, or what passes for public opinion, is not invariably a moderating force in the jungle of politics. It may be true, and I suspect it is, that the mass of people everywhere are normally peace-loving and would accept many restraints and sacrifices in preference to the monstrous calamities of war. But I also suspect that what purports to be public opinion in most countries that consider themselves to have popular government is often not really the consensus of the feelings of the mass of the people at all, but rather the expression of the interests of special highly vocal minorities — politicians, commentators, and publicity-seekers of all sorts: people who live by their ability to draw attention to themselves and die, like fish out of water, if they are compelled to remain silent." - George F. Kennan

"Condemn me, it does not matter: history will absolve me." - Fidel Castro

Wednesday, April 8, 2009

Favorite Aristotle quotes

"A tyrant must put on the appearance of uncommon devotion to religion. Subjects are less apprehensive of illegal treatment from a ruler whom they consider god-fearing and pious. On the other hand, they do less easily move against him, believing that he has the gods on his side."

"Money was intended to be used in exchange, but not to increase at interest. And this term interest, which means the birth of money from money, is applied to the breeding of money because the offspring resembles the parent. Wherefore of all modes of getting wealth this is the most unnatural."

"It is simplicity that makes the uneducated more effective than the educated when addressing popular audiences."

"The young have exalted notions, because they have not been humbled by life or learned its necessary limitations; moreover, their hopeful disposition makes them think themselves equal to great things—and that means having exalted notions. They would always rather do noble deeds than useful ones: Their lives are regulated more by moral feeling than by reasoning.... All their mistakes are in the direction of doing things excessively and vehemently. They overdo everything; they love too much, hate too much, and the same with everything else. "

"Even when laws have been written down, they ought not always to remain unaltered."

"The basis of a democratic state is liberty."

Friday, April 3, 2009

Update a particular number of rows

There are times when you only want to update a specific number of rows. In Oracle, the way to do this is to use the rownum variable in the following way:

Update x
set x.foo = 'hello'
where rownum <= 500;

This way, Oracle will only update the first 500 records it finds.

You can use rownum in virtually any DML statement. Try it on a select statement:

select x.foo
from x
where rownum <= 10;

Your result set will only have the first 10 records displayed on the screen.

Thursday, April 2, 2009

Joins in an Update statement

I spent a great deal of time trying to find out how to do an efficient JOIN in an UPDATE clause on Oracle. Tom has some great input on this:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:164612348068

Friday, March 20, 2009

Internet Explorer 8

I thought I'd never say this about any version of IE, but I like it. It borrows heavily from Google's Chrome browser, and with good reason; Chrome is fast, reliable and has a nice set of features. The slices (borrowed from Safari) and accelerators (borrowed from Firefox) are pretty good ideas and it's great that you can manage them by adding or removing whichever accelerators you'd like. It's also faster than IE7 (still slower than Chrome, though) and, lo and behold, if an application inside a tab crashes, you get the chance to just close that tab and not lose all your other tabs. That is of course also borrowed from Chrome. Based upon this and their next big release, Windows 7, it looks like perhaps Microsoft is moving in a new direction in which they give usability much more importance than before. If it is, It is a welcome change. It takes the aggressive competition of Apple for them to wake up.

Monday, February 9, 2009

User input in SQL

Sometimes you need to accept user input in an Oracle SQL script. There are a couple of ways to achieve this. 

Suppose you have a Student table that holds information about all students in a school. You want to display all students that share a specific first name. You want the user to be able to enter this first name and the script should take care of the rest. You could use:

SELECT 
std_id
std_fname, 
std_lname 
FROM 
student
WHERE 
std_fname = '&fname';

Oracle will automatically ask the user to enter the desired value to substitute &fname with.
In a script in which you only reference &fname once, this works perfectly. However, sometimes you want to refer to this value more than once in the script. Oracle would ask you for the value each time. 

A solution to this is to use double ampersands &&. The script would then say 

WHERE
std_fname = '&&fname';

What Oracle does in this case is store the &&fname value in memory for the duration of the current session. That way, if in subsequent commands you need to use the same user-provided value, it will be readily available and require no further user input. The problem with this is, sometimes you want to use this same value, but you may at other times in the same session require the user to provide another value for it. If you call this script twice within the same session, it will use the same value the second time, without requiring the user's input.

The way around this is to use the PROMPT...ACCEPT clause.

PROMPT
ACCEPT FNAME PROMPT "Please enter the student's first name: "

Then you'd reference fname in this way:

WHERE 
std_fname = '&fname';

This method will allow for user input every time the script is run, and allow for different first names to be used for comparison within the same session.