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.