Important alert: (current site time 7/15/2013 4:19:27 PM EDT)
 

VB icon

Java Address Book Database

Email
Submitted on: 3/29/2000 11:19:38 PM
By: Steven Jacobs 
Level: Intermediate
User Rating: By 14 Users
Compatibility: Java (JDK 1.1)
Views: 56483
author picture
(About the author)
 
     Simple Address Book database. Some functionality like database grid view, submit new entries, delete entries, clear fields, find entries. Used MSAccess as data "holder". Good for beginners who want to get familiar with SQL commands and jdbc/odbc. Feedback would be cool. Used JBuilder3/jdk 1.2 to develop.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Java Address Book Database
// Description:Simple Address Book database. Some functionality like database grid view, submit new entries, delete entries, clear fields, find entries. Used MSAccess as data "holder". Good for beginners who want to get familiar with SQL commands and jdbc/odbc. Feedback would be cool. Used JBuilder3/jdk 1.2 to develop.
// By: Steven Jacobs
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1795&lngWId=2//for details.//**************************************

/////////////////////////////////////////////////////////////////////////////
//Name of File: AddressBook.java
//Purpose:Input data/Find data from an Access database
//Author:Steven Jacobs
//Date: 03/29/2000
//Comments: Feel free to change/improve/etc. Any questions or concerns,
//email me at sjcplus@aol.com
/////////////////////////////////////////////////////////////////////////////
package untitled20;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import com.borland.jbcl.control.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;
public class AddressBook extends Applet
{
 FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
 boolean isStandalone = false;
 Label nameLabel = new Label("Name: ");
 Label addressLabel = new Label("Address: ");
 Label emailLabel = new Label("Email: ");
 Label isConnectLabel = new Label("Connected??");
 TextField nameField = new TextField(40);
 TextField addressField = new TextField(40);
 TextField emailField = new TextField(40);
 TextField connectField = new TextField(50);
 Button submitButton = new Button("Submit");
 Button findButton = new Button("Find");
 Button clearButton = new Button("Clear Fields");
 Button gridButton = new Button("Show Database Grid");
 Button deleteButton = new Button("Delete Record");
 Button exitButton = new Button("Exit");
 private String url;
 private Connection connect;
 //Construct the applet
 public AddressBook()
 {
 }
 //Initialize the applet
 public void init()
 {
try 
{
 jbInit();
}
catch(Exception e) 
{
 e.printStackTrace();
}
 }
 //Component initialization
 private void jbInit() throws Exception
 {
this.setLayout(flow);
add(nameLabel);
add(nameField);
add(addressLabel);
add(addressField);
add(emailLabel);
add(emailField);
add(isConnectLabel);
add(connectField);
add(submitButton);
add(findButton);
findButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
try
 {
Statement state = connect.createStatement();
if (nameField.getText().equals(""))
 {
isConnectLabel.setText("No Value");
connectField.setText("You must enter a value in the 'Name' field");
state.close();
 }
else
{
String query = "SELECT * FROM info " +
"WHERE Name = '" +
nameField.getText() + "'";
isConnectLabel.setText("Querying??");
connectField.setText("Sending Query!!" + connect.nativeSQL(query));
ResultSet rS = state.executeQuery(query);
display(rS);
connectField.setText("Successfull Query");
state.close();
}
 }
 catch (SQLException sqlex)
{
 sqlex.printStackTrace();
 connectField.setText(sqlex.toString());
}
}
public void display(ResultSet rS)
 {
try
 {
rS.next();
nameField.setText(rS.getString(1));
addressField.setText(rS.getString(2));
emailField.setText(rS.getString(3));
 }
 catch (SQLException sqlX)
{
 sqlX.printStackTrace();
 connectField.setText(sqlX.toString());
}
}
 });
add(clearButton);
clearButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
nameField.setText("");
addressField.setText("");
emailField.setText("");
isConnectLabel.setText("Clearing Fields??");
connectField.setText("Fields Cleared..You may enter a new record..");
 }
});
add(gridButton);
gridButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
AddressBookGrid adg = new AddressBookGrid();
Dimension dlgSize = adg.getSize();
Dimension frmSize = getSize();
Point loc = getLocation();
adg.setLocation((frmSize.width = dlgSize.width)/2 + loc.x, (frmSize.height - dlgSize.height)/2 + loc.y);
adg.show();
 }
 });
add(deleteButton);
deleteButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
try
 {
Statement state = connect.createStatement();
if (nameField.getText().equals(""))
 {
isConnectLabel.setText("No Value to Delete");
connectField.setText("You must enter a value in the 'Name' field to delete the record");
state.close();
 }
else
{
String query = "DELETE * FROM info " +
"WHERE Name = '" +
nameField.getText() + "'";
isConnectLabel.setText("Deleting??");
connectField.setText("Sending Query Delete!!" + connect.nativeSQL(query));
state.executeQuery(query);
connectField.setText("Successfull Deletion");
state.close();
}
 }
 catch (SQLException sqlex)
{
 sqlex.printStackTrace();
 connectField.setText(sqlex.toString());
}
}
});
add(exitButton);
exitButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
System.exit(0);
 }
 });
submitButton.addActionListener(new ActionListener()
 {
public void actionPerformed(ActionEvent e)
 {
try
 {
Statement state = connect.createStatement();
if (nameField.getText().equals("") &&
addressField.getText().equals("") &&
emailField.getText().equals(""))
{
 isConnectLabel.setText("No Values!!");
 connectField.setText("You must enter values in the field");
}
 else
{
String query = "INSERT INTO info (" +
"Name, " +
"Address, " +
"Email" +
") VALUES ('" +
nameField.getText() + "', '" +
addressField.getText() + "', '" +
emailField.getText() + "')";
connectField.setText("Sending query: " + connect.nativeSQL(query));
int result = state.executeUpdate(query);
if (result == 1)
 {
isConnectLabel.setText("Submitted??");
connectField.setText("Insertion successful");
 }
else
 {
isConnectLabel.setText("Submitted??");
connectField.setText("Insertion unsuccessful");
 }
 } }
 catch (SQLException sqlex)
{
 sqlex.printStackTrace();
 connectField.setText(sqlex.toString());
}
}});
try
 {
 url = "jdbc:odbc:AddressBook";
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 connect = DriverManager.getConnection(url);
 connectField.setText("Connection successful");
 }
 catch(ClassNotFoundException cnfx)
{
 cnfx.printStackTrace();
 connectField.setText("Connection unsuccessful" + cnfx.toString());
}
 catch (SQLException sqlx)
{
 sqlx.printStackTrace();
 connectField.setText("Connection unsuccessful" + sqlx.toString());
}
 catch (Exception ex)
{
 ex.printStackTrace();
 connectField.setText("Connection unsuccessful" + ex.toString());
}
 }
//Get Applet information
 public String getAppletInfo()
 {
return "Applet Information";
 }
 //Get parameter info
 public String[][] getParameterInfo()
 {
return null;
 }
 //Main method
 public static void main(String[] args)
 {
AddressBook applet = new AddressBook();
applet.isStandalone = true;
DecoratedFrame frame = new DecoratedFrame();
frame.setTitle("Address Book Applet");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,250);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
 }
}
/////////////////////////////////////////////////
//beginning of second source file
/////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//Name of File: AddressBookGrid.java
//Purpose:Shows the contents of your database in a java JTable
//Author:Steven Jacobs
//Date: 03/29/2000
//Comments: Feel free to change/improve/etc. Any questions or concerns,
//email me at sjcplus@aol.com
/////////////////////////////////////////////////////////////////////////////
package untitled20;
import java.awt.*;
import javax.swing.JFrame;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class AddressBookGrid extends JFrame
{
 private Connection connection;
 private JTable table;
 private Vector gridColumns;
 private Vector gridRows;
 AddressBook Ab;
 public AddressBookGrid()
 {
String url = "jdbc:odbc:AddressBook";
try
 {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url);
 }
 catch (ClassNotFoundException cnf)
{
Ab.connectField.setText("Failed to Connect to Driver");
}
 catch (SQLException sqlx)
{
 Ab.connectField.setText("Unable to connect");
}
 showDatabaseTable();
 setSize(450,150);
 setTitle("Address Book Grid View");
 show();
 }
 private void showDatabaseTable()
 {
try
 {
Statement state = connection.createStatement();
String query = "SELECT * FROM info";
ResultSet result = state.executeQuery(query);
displayDatabaseRecords(result);
state.close();
 }
 catch (SQLException sqlx)
{
 sqlx.printStackTrace();
}
}
 private void displayDatabaseRecords(ResultSet rS) throws SQLException
 {
boolean moreRecords = rS.next();
if (!moreRecords)
 {
Ab.connectField.setText("No records to display");
 }
gridColumns = new Vector();
gridRows = new Vector();
try
 {
ResultSetMetaData rs = rS.getMetaData();
for (int i = 1; i <= rs.getColumnCount(); ++i)
 gridColumns.addElement(rs.getColumnName(i));
do
 {
gridRows.addElement(getNextRow(rS,rs));
 }while (rS.next());
table = new JTable (gridRows, gridColumns);
JScrollPane scroller = new JScrollPane(table);
getContentPane().add(scroller, BorderLayout.CENTER);
validate();
}
catch(SQLException sqlx)
 {
sqlx.printStackTrace();
 }
}
private Vector getNextRow(ResultSet rS, ResultSetMetaData rs) throws SQLException
 {
Vector currentRow = new Vector();
for (int i = 1; i <= rs.getColumnCount(); ++i)
 if (rs.getColumnType(i) == Types.VARCHAR)
{
 currentRow.addElement(rS.getString(i));
}
 else
{
 Ab.connectField.setText("Type was: " + rs.getColumnTypeName(i));
}
return currentRow;
}
}


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/30/2000 11:53:36 AMsjgenius

Pretty Good.
(If this comment was disrespectful, please report it.)

 
3/30/2000 12:03:33 PMkeiko

Thanx for the code...it helps
(If this comment was disrespectful, please report it.)

 
6/7/2000 1:07:07 AMDeris

I did not really understand how this programed work. Can some one tell me where I can learn how to work databases. Thank you. Deristee@aol.com
(If this comment was disrespectful, please report it.)

 
6/7/2000 1:07:19 AMDeris

I did not really understand how this programed work. Can some one tell me where I can learn how to work databases. Thank you. Deristee@aol.com e-mail me...

(If this comment was disrespectful, please report it.)

 
8/26/2000 4:44:05 PMKelly Kaur

This is the most helpful piece yet!
It is exactly what I needed for further study in this area! Thank you very much!
Kelly!
(If this comment was disrespectful, please report it.)

 
10/4/2000 1:51:45 PMJoel Leon

ugly
(If this comment was disrespectful, please report it.)

 
2/6/2001 7:44:56 PMHooligank

Very good sir. Your straight forward code is really appreciated by newbies! Some people really twink their code with un-neccessary stuff.

Hey, anyone for EQ?
(If this comment was disrespectful, please report it.)

 
5/23/2002 4:08:39 AMDanny

Hi Steven Jacobs, i have downloaded your Java address book database for my reference because i'm developing a client e-mail system for my final project. But i couldn't run your program because i can't find the Ms. database (AddressBook.mdb). Can you pls mail to me this database. Thanks.
(If this comment was disrespectful, please report it.)

 
6/21/2002 12:37:36 AMJonathan

Very useful!It helps me to understand about JDBC more.Thanks!
(If this comment was disrespectful, please report it.)

 
12/14/2002 6:57:08 AM

Excellent code - thanks
(If this comment was disrespectful, please report it.)

 
4/25/2003 10:51:31 AM

Hi Steven Jacobs, a favor...could please you send me a copy of your database...so i can see working your code?....Thanks..mecarmona@hotmail.com
(If this comment was disrespectful, please report it.)

 
5/31/2004 3:12:58 PM

Hi Steven Jacobs, I have downloaded
your Java address book database for my
reference because I'm developing a
client e-mail system for my final
project. But i couldn't run your
program because i can't find the Ms.
database (AddressBook.mdb). Can you pls
mail to me this database. Thanks.
(If this comment was disrespectful, please report it.)

 
5/31/2004 3:17:37 PM

Please you help me,My project is a Bookmark and it also include an address book, I aready seen your source code,May you send me a source code of a bookmark which is able to add,save, view,and delete. Have a wonderful Time
(If this comment was disrespectful, please report it.)

 
3/8/2005 9:17:14 PM

Thank you for sharing - very helpfull program.
(If this comment was disrespectful, please report it.)

 
3/8/2005 9:25:25 PM

Please send me the database AddressBook.mdb
thanks
vladeks@gmail.com
(If this comment was disrespectful, please report it.)

 
4/13/2005 12:23:23 PM

The code looks good. I can probably create an .mdf file that would work, but I was wondering if you could send me your file??

Thanks,
Matt
(If this comment was disrespectful, please report it.)

 
8/2/2005 4:36:00 PMrinny

good for beginners in java
(If this comment was disrespectful, please report it.)

 
1/30/2006 9:19:21 AM

Please send me the database AddressBook.mdb
thanks
(If this comment was disrespectful, please report it.)

 
3/18/2008 7:04:45 AMRaja Goswami

Very Very Good
(If this comment was disrespectful, please report it.)

 
3/19/2008 7:21:46 AMRaja Goswami

Excellent code - thanks

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.