Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Inserimento dati da Java a SQL

    Salve!!!

    Sto creando un programma con Window Builder che consente di immettere dati su una tabella in SQL (magazzino).
    Tale tabella ha, come campi non chiave: tipo_mat (che rappresenta il tipo di materiale), nome_mat (nome del materiale), qta_mat (quantità del materiale) e prezzo_unit (ovvero il prezzo unitario per il materiale).
    Il mio problema è quello di trascrivere nei campi tabella il corrispettivo dei TextField dell'applicazione.
    Spero mi possiate aiutare a comprenderne la logica, in quanto con quantità e prezzo che non sono stringhe - e che quindi necessitano di parse - la faccenda si complica.
    Di seguito il codice:



    import java.awt.EventQueue;


    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.awt.event.ActionEvent;


    public class Magazzino {


    private JFrame frame;
    private JTextField textFieldMatType;


    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Magazzino window = new Magazzino();
    window.frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }


    /**
    * Create the application.
    */
    public Magazzino() {
    initialize();
    }


    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getContentPane().setLayout(null);

    JLabel lblNewLabel = new JLabel("Tipo Materiale");
    lblNewLabel.setBounds(22, 43, 87, 14);
    frame.getContentPane().add(lblNewLabel);

    textFieldMatType = new JTextField();
    textFieldMatType.setBounds(105, 40, 86, 20);
    frame.getContentPane().add(textFieldMatType);
    textFieldMatType.setColumns(10);

    JButton btnInserisci = new JButton("Inserisci");
    btnInserisci.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

    String url = "jdbc:mysql://localhost:3306/demo";
    String user = "pma";
    String password = "Beltane_79";

    try{
    //1. Get a connection to database
    Connection myConn = DriverManager.getConnection(url, user, password);
    //2. Create a statement
    Statement myStmt = myConn.createStatement();
    //3. Execute SQL query

    //Insert data
    String tipo_mat = textFieldMatType.toString();
    String sql = "insert into magazzino " + " (tipo_mat)"
    + " values (tipo_mat)";
    myStmt.executeUpdate(sql);
    System.out.println("Insert complete.");



    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    });
    btnInserisci.setBounds(222, 39, 89, 23);
    frame.getContentPane().add(btnInserisci);
    }
    }

  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,304
    Sposto nel forum Java.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.