Retrieving data from a database

database build in Microsoft Access

Page 1 of 1

2 Replies - 11815 Views - Last Post: 08 July 2009 - 07:45 PM Rate Topic: -----

#1 lenski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-July 09

Retrieving data from a database

Posted 08 July 2009 - 11:19 AM

I have build a database in Microsoft Access. Now I need to use Java swing to retrieve data from my database, How do I do it?
Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving data from a database

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Retrieving data from a database

Posted 08 July 2009 - 11:22 AM

Java Swing doesnt retrieve data from a database, it mainly just acts as a display. You just need to do some basic JDBC. Query the database for the data, store it into a ResultSet, and then display it how you like.
[rules]
[/rules]
Was This Post Helpful? 0
  • +
  • -

#3 ronaldjames  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 29-June 09

Re: Retrieving data from a database

Posted 08 July 2009 - 07:45 PM

View Postlenski, on 9 Jul, 2009 - 12:19 AM, said:

I have build a database in Microsoft Access. Now I need to use Java swing to retrieve data from my database, How do I do it?


Hello friend, u wants to retrieve data from MS Access right..?

But just with swing it is not possible.

First of all u need JDBC to do this.
And import the java.sql.* package.

Now using swings u create the GUI required for u.
For example a Button and some Text fields and when u click the button the required data should displayed in the Text field [SOMETHING LIKE THIS AS UR WISH].

U should be good in SQL queries also.
[Note that u save your MS Access file as .mdb ie, 2002-2003 Format]

Now go to Control panel [I assumes that u use a windows OS]
Control Panel-->Administrative Tools -->Data Sources(ODBC)-->User DSN-->add-->Microsoft Access Driver(*.mdb)-->Finish.

Now u gets ODBC Microsoft Access setup.
Their give a name for ur Data Source.
Then select the Access file which u has created.
Click OK.
Creating Data Source is finished.

Now u should start working in ur java code.

First of all u have to load a JDBC driver.
There r mainly 4 types of JDBC drivers. [JUST GOOGLE FOR IT]
Here we are loading JdbcOdbcDriver.

To load JdbcOdbcDriver u can use the code :

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


Now we are creating a Connection object.

U can use this code :

Connection connection = DriverManager.getConnection("jdbc:odbc:YOURDATASOURCENAME","","");

U should provide the Data Source name which u has created.

Next step is to use SQL statements for working with data in MS Access.
Mainly we have 3 types of statements. [JUST GOOGLE FOR IT]
But we use Prepared Statement here.

For example a code is given here :

PreparedStatement statement=connection.prepareStatement( ("select COLUMN_1,COLUMN_2 from TABLE_NAME where CONDITION_1=? AND CONDITION_2=?);


Here the ? is called as placeholder.
A place holder is replaced by the inputs given by user.

For that we use :

statement.setXXX(placeholderindex,value);


where XXX is any DataType.

Assume CONDITION_1 is of type String and CONDITION_2 is of type int.
We take both the inputs from A JTextField [Using Swings as u need]

Code to input values for placeholder is :

statement.setString(1,textfield_1.getText());
statement.setInt(2, Integer.parseInt(textfield_1.getText()));


Here 1 is the placeholderindex for CONDITION_1 and we pass a string value which is obtained from the textfield_1.

Here 2 is the placeholderindex for CONDITION_2 and we pass an int value which is obtained from the textfield_2.[As u know a JTextField returns a String value we have to parse it into int]

Now we have to execute this SQL statements.

Mainly we have 3 execute statements.
1.execute – for queries which uses CREATE,ALTER etc.
2.executeQuery – for SELECT queries.
3.executeUpdate – for DELETE,UPDATE queries.

Here we are using select query so we have to use executeQuery.
While executing the SELECT query it returns some values which are retrieved from the DataBase.

We can store those values into a ResultSet object.

Code to execute and store values to ResultSet object is :

ResultSet resultset=statement.executeQuery();


Now to retrieve the values from ResultSet object we can use methods like
resultset.next();
resultset.previous();
resultset.first();
resultset.last();
resultset.absolute(rownumber); - retrieve the data from the specified rownumber
resultset.relative(rownumber); -here we can pass either +ve or –ve value.ie,to move to front or to back from the current row.


[am I confusing u…??hope not..~!!]

In next step we are assigning the retrieved values to respective JTextFields.

Like setXXX we have getXXX to retrieve values from ResultSet object.

Using SELECT query we have retrieved 2 columns.

COLUMN_1 of type String andCOLUMN_2 of type int.
So we use getString(columnindex) & getInt(columnindex)

The code is like this :

while(resultset.next())
{
textfiled_3.setText(resultset.getString(1));
textfield_4. setText(String.valueOf(resultset.getInt(2)));
}


Now u can see the retrieved values in respective JTextFields.

Note that u should enclose this codes in try-catch block or should throw exceptions.

Entire code :

try
{
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:YOURDATASOURCENAME","","");
PreparedStatement statement=connection.prepareStatement( ("select COLUMN_1,COLUMN_2 from TABLE_NAME where CONDITION_1=? AND CONDITION_2=?);
statement.setString(1,textfield_1.getText());
statement.setInt(2, Integer.parseInt(textfield_1.getText()));
ResultSet resultset=statement.executeQuery();
while(resultset.next())
{
textfiled_3.setText(resultset.getString(1));
textfield_4. setText(String.valueOf(resultset.getInt(2)));
}

}
catch(Exception e)
{
System.out.println(“Error ”+e);
}



Hope u r clear

If not contact for help.

Always happy 2 help.......


Then tryout this and send ur code.
If any prob we can fix it.
Whole D.I.C team is here to support u.

This post has been edited by ronaldjames: 08 July 2009 - 10:27 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1