codice:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ProdottoInserisci extends JFrame implements ActionListener {
private static final int LARGHEZZA = 430;
private static final int ALTEZZA = 400;
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost/magazzino?user=root&password=xyz";
Connection con;
Statement st;
ResultSet rs;
JTextField nomeprodottoText, categoriaText, prezzoText, disponibilitaText, maxordinabileText, produttoreText, fornitoreText;
JTextArea descrizioneText;
public ProdottoInserisci(){
setTitle("Inserisci prodotto:");
setSize(LARGHEZZA,ALTEZZA);
JPanel pannello = new JPanel();
pannello.setLayout(null);
JLabel nomeprodottoLabel = new JLabel("Nome Prodotto: ");
nomeprodottoLabel.setBounds(10,10,125,25);
pannello.add(nomeprodottoLabel);
nomeprodottoText = new JTextField(20);
nomeprodottoText.setBounds(120,10,160,25);
pannello.add(nomeprodottoText);
JLabel categoriaLabel = new JLabel("Categoria: ");
categoriaLabel.setBounds(10,50,80,25);
pannello.add(categoriaLabel);
categoriaText = new JTextField(20);
categoriaText.setBounds(120,50,160,25);
pannello.add(categoriaText);
JLabel prezzoLabel = new JLabel("Prezzo: ");
prezzoLabel.setBounds(10,90,80,25);
pannello.add(prezzoLabel);
prezzoText = new JTextField(20);
prezzoText.setBounds(120,90,160,25);
pannello.add(prezzoText);
JLabel descrizioneLabel = new JLabel("Descrizione: ");
descrizioneLabel.setBounds(10,130,80,25);
pannello.add(descrizioneLabel);
descrizioneText = new JTextArea(8,20);
descrizioneText.setBounds(120,130,160,40);
pannello.add(descrizioneText);
JLabel disponibilitaLabel = new JLabel("Disponibilita: ");
disponibilitaLabel.setBounds(10,190,80,25);
pannello.add(disponibilitaLabel);
disponibilitaText = new JTextField(20);
disponibilitaText.setBounds(120,190,160,25);
pannello.add(disponibilitaText);
JLabel maxordinabileLabel = new JLabel("Da poter ordinare: ");
maxordinabileLabel.setBounds(10,230,130,25);
pannello.add(maxordinabileLabel);
maxordinabileText = new JTextField(20);
maxordinabileText.setBounds(120,230,160,25);
pannello.add(maxordinabileText);
JLabel produttoreLabel = new JLabel("Produttore: ");
produttoreLabel.setBounds(10,270,80,25);
pannello.add(produttoreLabel);
produttoreText = new JTextField(20);
produttoreText.setBounds(120,270,160,25);
pannello.add(produttoreText);
JLabel fornitoreLabel = new JLabel("Fornitore: ");
fornitoreLabel.setBounds(10,310,80,25);
pannello.add(fornitoreLabel);
fornitoreText = new JTextField(20);
fornitoreText.setBounds(120,310,160,25);
pannello.add(fornitoreText);
JButton signup = new JButton("Sign Up!");
signup.setBackground(Color.LIGHT_GRAY);
signup.addActionListener(this);
signup.setBounds(300,30, 80,25);
pannello.add(signup);
add(pannello);
}
public void connetti(Prodotto prodotto){
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to a selected database...");
con = DriverManager.getConnection(DB_URL);
System.out.println("Connected database successfully...");
st = con.createStatement();
String sql = "INSERT INTO listaprodotti(nomeprodotto, categoria, descrizione, prezzo, disponibilita, maxordinabile, produttore, fornitore) VALUES ('"+ prodotto.getNomeProdotto() + "','" + prodotto.getCategoria() + "','" + prodotto.getDescrizione() + "','" + prodotto.getPrezzo() + "','" + prodotto.getDisponibilita() + "','" + prodotto.getMaxOrdinabile() + "','" + prodotto.getProduttore() +"','" + prodotto.getFornitore() + "')" ;
st.executeUpdate(sql);
} catch(SQLException se) //catch block
{
//Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) //catch block
{
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try //try block
{
if(con!=null)//condition
con.close(); //close connection
System.out.println("Close database successfully...");
}
catch(SQLException se)//Handle errors
{
se.printStackTrace();
}//end finally try
}//end try*/
}
public void actionPerformed(ActionEvent e){
Prodotto prodotto = new Prodotto();
prodotto.setNomeProdotto(nomeprodottoText.getText());
prodotto.setCategoria(categoriaText.getText());
prodotto.setDescrizione(descrizioneText.getText());
prodotto.setPrezzo(Double.parseDouble(prezzoText.getText()));
prodotto.setDisponibilita(Integer.parseInt(disponibilitaText.getText()));
prodotto.setMaxOrdinabile(Integer.parseInt(maxordinabileText.getText()));
prodotto.setProduttore(produttoreText.getText());
prodotto.setFornitore(fornitoreText.getText());
connetti(prodotto);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ProdottoInserisci prodottoinserisci = new ProdottoInserisci();
prodottoinserisci.setVisible(true);
}
}