Results 1 to 6 of 6
  1. #1
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    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:

    Java 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:

    Java 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.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote 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.

  4. #4
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    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.

  5. #5
    kaybest is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default I am new to java

    "hello World!"

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote 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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •