Home arrow Java arrow Page 3 - Database Programming With Java + JDBC: Part 1/2
JAVA

Database Programming With Java + JDBC: Part 1/2


No Java programmer is complete without knowledge of Java database connectivity, or JDBC for short. In this 2 part article Nitin teaches us JDBC from the inside out.

Author Info:
By: Nitin Patil
Rating: 4 stars4 stars4 stars4 stars4 stars / 44
August 20, 2002
TABLE OF CONTENTS:
  1. · Database Programming With Java + JDBC: Part 1/2
  2. · What is JDBC?
  3. · Before We Start Coding
  4. · Our Address Book App (contd.)
  5. · The Sample Code... Explained
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Database Programming With Java + JDBC: Part 1/2 - Before We Start Coding
(Page 3 of 6 )

In order to do some hands on coding, you need to have access to the following:
  1. A database management system, such as Microsoft Access
  2. A JDBC driver
If you don't have access to a DBMS, you can download the open source MySQL database at http://www.mysql.com. You can also download the JDBC driver for MySQL from this site. 

You need to have the driver classes in your CLASSPATH statement in order to load the driver.

A simple example
What's better than developing a small application to drill down into JDBC programming! Lets develop a small address book application that has the following features:
  1. Lets you create the address book table
  2. Lets you add an entry to the book
  3. Lets you search for an entry in the book
This simple application will let us demonstrate the use of various JDBC classes and different types of SQL statements. It uses a database named "test_db" without any user ID or password. The database driver used is a type 4 driver for MySQL, however you can replace it with any other JDBC driver of your choice.

A word of caution: do not be overwhelmed by the lines of code below. Instead, concentrate on the core logic. Also, this is just a sample application to demonstrate the steps involved in JDBC programming. It's not designed for a production system.

Having said that, lets get started:

// AddressBookEntry.java

/** Class to represent an addressbook entry. */
public class AddressBookEntry {

/** The nickname. */
private String nickName;
/** The name. */
private String name;
/** The email. */
private String email;

/** Initializes an entry. */
public AddressBookEntry(String nickName, String name, String email) {
this.nickName = nickName;
this.name = name;
this.email = email;
}

/** Returns the nickname. */
public String getNickName() {
return nickName;
}

/** Returns the name. */
public String getName() {
return name;
}

/** Returns the email. */
public String getEmail() {
return email;
}

/** Sets the nickname. */
public void setNickName(String nickName) {
this.nickName = nickName;
}

/** Sets the name. */
public void setName(String name) {
this.name = name;
}

/** Sets the email. */
public void setEmail(String email) {
this.email = email;
}

/** Returns a string representation of the entry. */
public String toString() {

// The return value
StringBuffer buf = new StringBuffer();

buf.append("Nickname: ");
buf.append(nickName);
buf.append(", Name: " );
buf.append(name);
buf.append(", Email: ");
buf.append(email);

return buf.toString();

}

}

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials