Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Bean Classes in Java and Java Database Connections

What is a Java Bean A class is a bean if it has a constructor that takes no parameters, and if it has methods that start with "get" to read properties, and "set" to set properties. And it's a good idea (!) if the letter after the word get or set is a capital, as this will let it be used within JSP tag libraries. Bean classes may have other methods too ... it's just that they won't be accessible through the bean interface.


How to connect to a database in Java Here's an example of using JDBC to connect to a MySQL database ... from within the code of a bean. Although Java has manager support to connect to databases, it doesn't include the actual support for any specific database - you need to go and download separate packages of classes for that. "How frustrating" you may say (you would be right!) but it means that the Java folks can look after the Java, the database folks can look after the databases, and when a database is updated the user doesn't have to wait for the next release of Java before he can use the new facilities. It also avoids huge arguments in the community about what should (and should not) be included as a standard driver.

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.

(written 2009-05-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J811 - Java - JDBC - Relational Database Access
  [3046] Java Beans, tag libraries and JSPs - what and why. - (2010-11-13)
  [2861] MySQL and Java - connectivity past, present, and future thoughts - (2010-07-09)
  [515] MySQL - an FAQ - (2005-12-03)


Back to
Class Loading and Variable Conversion in Java
Previous and next
or
Horse's mouth home
Forward to
No subject or title?
Some other Articles
On the roof of a Melksham Spa House
From the 51773 family
Stopping forum spam - control of the signup process
No subject or title?
Bean Classes in Java and Java Database Connections
Class Loading and Variable Conversion in Java
My four feet!
What is a JSP tag library?
Routers, Firewalls and multilayer servers
Routing Network Traffic - Proxies, Redirects and DNS
4318 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2154_Bea ... tions.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat