Bean Classes in Java and Java Database Connections
Archive - Originally posted on "The Horse's Mouth" - 2009-05-02 08:25:27 - Graham Ellis

So the database manager as shipped is rather like building a hotel without guests, and the individual drivers you load are like the guests. You need the hotel for the guests, but until you have guests it's really not fulfilling any sensible purpose.
Once the classes are there, how does the connectivity work? Here are the steps that we used in the example show here, written during yesterday's course:
Class.forName("xxxx").newInstance();
where xxxx is the driver class
Loads in the driver class into the driver manager
Connection cnamedb = driverManager.getConnection("yyyy");
where yyyy is a pseudoURL which describes the connection
Log in to the appropriate database
Statement bdstate = cnamedb.createStatement();
Get ready to perform an access (query) to the database
ResultSet dbresult = bdstate.executeQuery("zzzz");
where zzzz is the SQL query to be run
Run the actual query - or rather start it, and return an object that will let you iterate through the result
while (dbresult.next()) { Value = dbresult.getString("wwww"); }
where wwww is the field (column) that you want
Extract the data that you want from each row in turn. ... you can now add your code to process the Value variable further ...
Java Database Connections are also covered on our Java Bootcamp and Learning to Program in Java courses.