Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 45
servlet filter example 37 63
servlet example 17 32
jboss wildfly 10.1 10 94
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

809 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