Ciao,
sto elaborando la mia prima applicazione per sviluppare una piccola mailing list. il problema è che se lancio l'applicazione da Eclipse mi funziona correttamente,mentre se la racchiudo in una pagina HTML, si comporta in modo anomalo.
Funziona tutto tranne che non salva il nick e l'email nel file "mail.mdb".
Ecco il codice java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class SubscribeForm extends JApplet implements ActionListener{
private String nick;
private String eMail;
private JLabel messageLabel;
private JTextField nickField;
private JTextField eMailField;
public static void sendToDB(String nick, String eMailAddress){
String DBPath = "db/mail.mdb"; // cambia DBPath per settare l'indirizzo del file *.mdb
String database = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + DBPath.trim();
Connection con = DriverManager.getConnection(database);
Statement stmt = con.createStatement();
stmt.execute("INSERT INTO Indirizzi VALUES('" + nick + "','" + eMailAddress + "')");
stmt.close();
con.close();
}
catch (ClassNotFoundException ex1){
System.out.println("Error: " + ex1.getMessage());
}
catch(SQLException ex2){
System.out.println("Error: " + ex2.getMessage());
}
}
public void init(){
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new BorderLayout());
// Inizializzo il pannello messaggio e lo riempio con una label
JPanel messagePanel = new JPanel();
messagePanel.setBackground(Color.WHITE);
messageLabel = new JLabel();
messageLabel.setText("Subscribe our newsletter"); // testo di benvenuto
messagePanel.add(messageLabel);
//Inizializzo il pannello principale con gli input del nuck e e-mail
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.WHITE);
mainPanel.setLayout(new FlowLayout());
JLabel nickLabel = new JLabel("Nick: ");
nickField = new JTextField(20);
JLabel eMailLabel = new JLabel("eMail:");
eMailField = new JTextField(20);
mainPanel.add(nickLabel);
mainPanel.add(nickField);
mainPanel.add(eMailLabel);
mainPanel.add(eMailField);
// Inizializzo il pannello pulsanti
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
JButton subscribe = new JButton("Subscribe");
subscribe.addActionListener(this);
buttonPanel.add(subscribe);
// aggiungo i vari pannelli al contentPane
contentPane.add(messagePanel, BorderLayout.NORTH);
contentPane.add(mainPanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Subscribe")){
if (nickField.getText().equals("")){
nickField.setBackground(Color.YELLOW);
eMailField.setBackground(Color.WHITE);
messageLabel.setText("You have to enter a valid nick");
messageLabel.setForeground(Color.RED);
}else if(eMailField.getText().equals("")){
eMailField.setBackground(Color.YELLOW);
nickField.setBackground(Color.WHITE);
messageLabel.setText("You have to enter a valid e-mail address");
messageLabel.setForeground(Color.RED);
}else{
nick = nickField.getText();
eMail = eMailField.getText();
sendToDB(nick, eMail);
messageLabel.setText("Thank you for subscribing our news letter");
messageLabel.setForeground(Color.BLACK);
nickField.setText("");
nickField.setBackground(Color.WHITE);
eMailField.setText("");
eMailField.setBackground(Color.WHITE);
}
}
else{
System.out.println("Unaxpected Error Occured");
messageLabel.setText("Unaxpected Error Occured");
messageLabel.setForeground(Color.RED);
}
}
}
nella pagina HTML:
"<applet code="SubscribeForm.class" codebase="../../../Java/MailingList/" width="300" height="150">
</applet>"
Mi potete aiutare? c'è qualche accorgimento da prendere nel codice Java o HTML?
Grazie

Rispondi quotando
