Solved

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

Posted on 2004-03-27
4
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 6

Accepted Solution

by:
dorothy2 earned 38 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 37 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 74
Java exception bubble up 2 24
iterator/ListIterator approach 17 54
batch vs regular insert in spring DAO 2 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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