Solved

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

Posted on 2004-03-27
4
173 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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