Solved

Connecting a Table with Java and be able to add and delete users in the table

Posted on 2004-03-27
4
164 Views
Last Modified: 2010-03-31
Can you assist me. How to you connect a database(ms access table ) with java. And also how you add and delete the users in the database using that program.
0
Comment
Question by:muskingala
4 Comments
 
LVL 6

Accepted Solution

by:
dorothy2 earned 38 total points
Comment Utility
You need to set up an ODBC connection to the Access database on your machine. Once that is done you can access the database via a Java API known as JDBC (Java DataBase Connection). You need to use the JDBC/ODBC driver within the JDBC API.

To add and delete data, you will need to embed SQL calls in a PreparedStatement.

This URL should get you started.

http://java.sun.com/developer/technicalArticles/Interviews/StartJDBC/

The Sun developer site also has a lot of tutorials, technical articles and code snippets available. There are also a lot of good books available which cover this topic. Look for O"Reilly Java Programming Examples In A Nutshell, or the Core Java Programming series. Either volume 1 or volume 2 has a chapter on JDBC/ODBC.

Finally, search the experts-exchange database for a reference to "Northwind". This is the Access sample database, and you are likely to find a code example for getting code out of Northwind in a previous answer that you can easily modify for your own situation.

Good luck,

Dorothy
0
 
LVL 4

Assisted Solution

by:91mustang
91mustang earned 37 total points
Comment Utility
here is some code that will work for you:
Just change the word database to the path of your database:
(ie. C:/data/database)
change the password to your password (only nessecary if the DB is password protected;

Modify the query to reflect the query you want.
This will display you the users in the DB
try{
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //load the Jdbc driver
                        Connection con1 = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=Database","dba","sql"); //get a connection
                        Statement stmt = con1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
 String query ="select f_name,l_name from users";
                        ResultSet rs1 = stmt.executeQuery(query); //execute query
                        while(rs1.next()){
                        System.out.println( rs1.getString("f_name")+" "rs1.getString("l_name")+" is is the database");
                                         
                                               
                                               
                        }


To insert records use this query :

String deletequery="insert into users (f_name, l_name) values ("'+firstname+"','"+lastname+"')";
then call:
 stmt.executeQuery(query); //execute query



where firstname and lastname are strings containing the new names to insert.

to delete from database use this query:

String query ="delete from users where l_name='"+lastname+"' and f_name ='"+firstname;

then call:
stmt.executeQuery(query); //execute query


where firstname and lastname are strings containing the names to delete.

obviously you will need a application to insert this code into. that will dictate how users actually interact.

A suggestion would by an application that displays all records in database in a combobox, then have to 2 textboxes, and 2 buttons. one button would delete the record associated with the name currently selected in the combo box. the other button would insert the text in the text areas to the database.
just a suggestion though... if you need more help just ask

hope it helps
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now