Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-03-27
4
Medium Priority
?
176 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 152 total points
ID: 10694422
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 148 total points
ID: 10695554
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

972 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