Quick Search for:  in language:    
SQL,Simple,Address,Book,database,Some,functio
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 92. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Java/ Javascript.
Click here to see a screenshot of this code!vok - The vocabulary trainer
By Thorsten Stärk on 1/7

(Screen Shot)

Java, Calculator
By Rockwell on 1/4


Eatable Interface
By Rockwell on 1/4


Superclass Person
By Rockwell on 1/4


Draws Cube Function
By Rockwell on 1/4


Rectangle Class
By Rockwell on 1/4


Find Number of Upper and Lower Case Letters in a Command Line Argument String
By Rockwell on 1/4


anagrams
By Rockwell on 1/4


Text Reader with Tokenizer
By Rockwell on 1/4


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Java Address Book Database

Print
Email
 
VB icon
Submitted on: 3/29/2000 11:19:38 PM
By: Steven Jacobs 
Level: Intermediate
User Rating: By 12 Users
Compatibility:Java (JDK 1.1)

Users have accessed this code 33094 times.
 

(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 datab
//     ase. Some functionality like database gr
//     id view, submit new entries, delete entr
//     ies, clear fields, find entries. Used MS
//     Access as data "holder". Good for beginn
//     ers who want to get familiar with SQL co
//     mmands 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/Sh
//     owCode.asp?txtCodeId=1795&lngWId;=2//for details.//**************************************
//     

////////////////////////////////////////
/////////////////////////////////////
//Name of File: AddressBook.java
//Purpose:Input data/Find data from an A
//     ccess 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 dat // abase 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 10 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:sjgenius
Pretty Good.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/30/2000 12:03:33 PM:keiko
Thanx for the code...it helps
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/7/2000 1:07:07 AM:Deris
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/7/2000 1:07:19 AM:Deris
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...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/26/2000 4:44:05 PM:Kelly 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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/4/2000 1:51:45 PM:Joel Leon
ugly
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2001 7:44:56 PM:Hooligank
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/23/2002 4:08:39 AM:Danny
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. 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/21/2002 12:37:36 AM:Jonathan
Very useful!It helps me to understand 
about JDBC more.Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/14/2002 6:57:08 AM:
Excellent code - thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.