Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 02:42 AM
Member
 
Join Date: Nov 2008
Posts: 33
Rep Power: 0
Bomber_Will is on a distinguished road
Default Making an Applet use a MySQL Database
I made an applet using Java, but now I want it to read information from a MySQL Database. The applet itself is a bunch of drop down menu lists that the user would pick an item from the list and it displays information about the item chosen.

Does anyone know the coding to make an applet be able to connect to a MySQL database?

My code is currently something like:

Code:
import java.awt.*; 
import java.applet.*;

public class Labeling extends Applet
{
     Label nameLabel, resultLabel;
     Choice nameChoice;
     TextField resultText;

     public void init()
    {
         nameLabel = new Label("Name of Box");
         nameChoice = new Choice();
         nameChoice.add("");
         nameChoice.add("Choice One");
         nameChoice.add("Choice Two");
         
        resultLabel = new Label ("Choice Picked: ");
 	    resultText = new TextField(4);
 	    resultText.setEditable(false);

         display();
 		 addIn();
    }
     
     public boolean action(Event event, Object object) 
     {
    	 if(event.target == nameChoice)
    	 {
    		 if (true)
   	      	 {
    			 putValue();
   	      	 }
   	      	
    		 return(true);
    	 } 
    	 else
    	 {
    		 return(false);
    	 }
     }
     
     public void putValue()
     {
    	 int choices = 0;
    	 
    	 String name = nameChoice.getSelectedItem();
    	 
    	 if(name.equals("Choice One"))
    		 choices = 1;
    	 else if(name.equals("Choice Two"))
    		 choices = 2;
    	 
    	 resultText.setText("" + choices);
     }
     
     public void display()
 	 {
                 // Use a grid bag layout.
 	    GridBagLayout gbag = new GridBagLayout();
 	    GridBagConstraints gbc = new GridBagConstraints();
 	    setLayout(gbag);
 	    
 	    // Define the grid bag.
 	    gbc.weighty = 1.0; // use a row weight of 1
 	    gbc.gridwidth = GridBagConstraints.REMAINDER;
 	    gbc.anchor = GridBagConstraints.NORTH;
 	    
 	    gbc.anchor = GridBagConstraints.EAST;
 	    gbc.gridwidth = GridBagConstraints.FIRST_LINE_START;
 	    gbag.setConstraints(nameLabel, gbc);
 	    gbc.gridwidth = 0;
 	    gbc.anchor = GridBagConstraints.WEST;
 	    gbag.setConstraints(nameChoice, gbc);

 	    gbc.anchor = GridBagConstraints.EAST;
	    gbc.gridwidth = GridBagConstraints.FIRST_LINE_START;
	    gbag.setConstraints(resultLabel, gbc);
	    gbc.gridwidth = 0;
	    gbc.anchor = GridBagConstraints.WEST;
	    gbag.setConstraints(resultText, gbc);
 	 }
     
     public void addIn()
     {
    	 add(nameLabel);
    	 add(nameChoice);
    	 add(resultLabel);
    	 add(resultText);
     }
}
Pretty much instead of my code having the part:

Code:
if(name.equals("Choice One"))
   choices = 1;
else if(name.equals("Choice Two"))
   choices = 2;
I want the applet to get the value of choice somehow in the database without having the if else statements because its around 2,000 choices and coding it would take an insanely long time.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2008, 04:05 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
I write code to write the choice as formatted java source code, then open it in the editor. For what you are asking there are almost always Selection.getIndex() methods which return an int. That can be used to index an array or a Collection.

Writing SQL statments may be done directly in the applet code as however they are normally done.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2008, 07:39 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by Bomber_Will View Post
I made an applet using Java, but now I want it to read information from a MySQL Database.
Where does the MySql database live? On the same machine as the browser?

Why are you using applets for this? Applets live in a sandbox that prohibits tons of things.

OT: Actually, I don't understand why anyone uses applets for anything this century.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-24-2008, 08:41 AM
Member
 
Join Date: Nov 2008
Posts: 33
Rep Power: 0
Bomber_Will is on a distinguished road
Default
Applets are simple for me to make and manipulate. The database should be in the same machine thats hosting the website the applet is in.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-30-2008, 12:59 PM
Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
kaybest is on a distinguished road
Default I am new to java
"hello World!"
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-30-2008, 04:28 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
Default
Originally Posted by kaybest View Post
"hello World!"
And clearly new to forums. What you did is called "thread hijacking" where one posts stuff clearly unrelated to the topic at hand. Its is bad form, impolite, etc.

There is a forum section for introductions. Post your statement there, not here
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access MySQL Database in Eclipse 3.2 nijava Eclipse 2 12-12-2008 03:04 PM
Displaying data from mysql database. Haresh gurav JavaServer Pages (JSP) and JSTL 2 07-09-2008 02:54 AM
database with mysql using the netbeans 6.0 kwesiaryee New To Java 2 05-02-2008 04:27 AM
MySQL Database and Java shaggymac Advanced Java 1 05-01-2008 09:01 PM
connecting to mysql database javagal NetBeans 2 08-04-2007 12:36 PM


All times are GMT +2. The time now is 10:16 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org