Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

An applet retrieving data from an access db.

Posted on 2003-03-30
4
Medium Priority
?
261 Views
Last Modified: 2010-03-31
Hello,
I would very much appreciate receiving some help with my flags applet. Actually MyApplet is supposed to read data from an access db which is composed of 2 cols: country(the name of the country) and icon(an animated gif representing the flag of the country).
The applet itself is composed of a combo box supposed to be filled with the countries'names from the db; when I make a choice and click the button, the flag should be displayed.
The problem I'm confronting here is that I can't fill the combo box from the db, however I've tested the connection and it's working. I believe that the problem is with the code of the combo. Anyway, Im sending along a copy of my code.
Thanks in advance for your help.

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.applet.*;
import java.net.URL.*;
import sun.jdbc.odbc.* ;

/*
<applet code="MyApplet" width="400" Height="300">
<param name="img" value="Austria.gif">
</applet>
*/
public class MyApplet extends Applet
     implements ActionListener, ItemListener {

// Componenets of the applet
Choice combo;
Button but1;
Image img;

public void init() {
// Here comes the initialization of the layout, combo, button, starting
// image and the connection

setLayout (new FlowLayout (FlowLayout.CENTER,50,150));
img = getImage(getDocumentBase(),getParameter("img"));
but1 = new Button ("Click");
add (but1);
but1.addActionListener(this);
combo = new Choice();
new sun.jdbc.odbc.JdbcOdbcDriver();
String url="jdbc:odbc:dsn1";
String country;

try {
          Connection con = DriverManager.getConnection(url);
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery ("SELECT country FROM country");

          while (rs.next()) {
          country=rs.getString("country");
          combo.addItem(country);
          rs++;
          }
     stmt.close();
} catch (SQLException e) {
  System.out.println("Cannot create SQL statement");
}
add (combo);
combo.addItemListener(this);
}

public void itemStateChanged (ItemEvent ie) {
repaint();
}

public void actionPerformed (ActionEvent ae) {
if (ae.getSource()==Click){
Connection con = DriverManager.getConnection(url);
String selection=combo.getSelectedItem;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ICON FROM COUNTRY WHERE COUNTRY="+selection);
img=getImage(rs);
repaint();
}

public void paint (Graphics g) {
g.drawImage(img,150,40,this);
     }
}

0
Comment
Question by:dtaifour
3 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 8235435
I reckon it's throwing a SecurityException, as you shouldn't be able to look up DSNs from an Applet...

I think with an Access database, you'll either have to write a servlet to sit next to it, and have:

  Applet <-> Servlet <-> Access

Or, use RMI, to do the same...

I would recommend in this case that you use Postgresql, or MySQL instead of access, as they can be accessed via the internet and JDBC type 4 drivers :-)

Both Databases are free:

Postgresql:
http://www.postgresql.org

Postgresql JDBC driver:
http://www.postgresql.org

MySQL:
http://www.mysql.com/

MySQL Driver:
http://www.mysql.com/downloads/api-jdbc-stable.html
0
 

Expert Comment

by:CleanupPing
ID: 9058384
dtaifour:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9735699
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept TimYates' comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
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.
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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