Using Different Statement store data in MySql

Using Different Statement store data in MySql

mysql> DELIMITER $$ ;
mysql> CREATE PROCEDURE insertExpens(in c varchar(30),in q int,in p float,in n v
archar(30),in d varchar(30))
-> insert into values(c,q,p,n,d);
-> $$

mysql> DELIMITER $$ ;
mysql> CREATE PROCEDURE getExpens()
-> SELECT * FROM expens;
-> $$

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;

public class ExpensManager extends JFrame {

 private JPanel contentPane;
 private JTextField textField;
 private JTextField textField_1;
 private JTextField textField_2;

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

 /**
  * Create the frame.
  */

 public ExpensManager() {

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 410);
  contentPane = new JPanel();

  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);

  JComboBox comboBox = new JComboBox();
  comboBox.setModel(
    new DefaultComboBoxModel(new String[] { "Select Category", "Food", "Bill", "Shopping", "Others" }));
  comboBox.setBounds(202, 97, 133, 22);
  contentPane.add(comboBox);

  JLabel lblSelectCategory = new JLabel("Select Category");
  lblSelectCategory.setBounds(79, 105, 108, 14);
  contentPane.add(lblSelectCategory);

  JLabel lblSelectQuantity = new JLabel("Select Quantity");
  lblSelectQuantity.setBounds(79, 148, 108, 14);
  contentPane.add(lblSelectQuantity);

  JComboBox comboBox_1 = new JComboBox();
  comboBox_1.setModel(new DefaultComboBoxModel(
    new String[] { "Select Quantity", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
  comboBox_1.setBounds(202, 144, 133, 22);
  contentPane.add(comboBox_1);

  JLabel lblSelectPrice = new JLabel("Unit Price");
  lblSelectPrice.setBounds(79, 197, 108, 14);
  contentPane.add(lblSelectPrice);

  textField = new JTextField();
  textField.setBounds(202, 191, 133, 20);
  contentPane.add(textField);
  textField.setColumns(10);

  JLabel lblProductName = new JLabel("Product Name");
  lblProductName.setBounds(79, 244, 108, 14);
  contentPane.add(lblProductName);

  textField_1 = new JTextField();
  textField_1.setColumns(10);
  textField_1.setBounds(202, 238, 133, 20);
  contentPane.add(textField_1);

  JLabel lblSelectDate = new JLabel("Select Date");
  lblSelectDate.setBounds(79, 288, 108, 14);
  contentPane.add(lblSelectDate);

  textField_2 = new JTextField();
  textField_2.setColumns(10);
  textField_2.setBounds(202, 285, 133, 20);
  contentPane.add(textField_2);

  JButton btnSubmit = new JButton("Submit");

  btnSubmit.setBounds(151, 338, 89, 23);
  contentPane.add(btnSubmit);

  btnSubmit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String category = comboBox.getSelectedItem().toString();
    String quantity = comboBox_1.getSelectedItem().toString();
    String price = textField.getText();
    String name = textField_1.getText();
    String date = textField_2.getText();

    /*
     * try { Class.forName("com.mysql.jdbc.Driver"); Connection
     * con=DriverManager.getConnection("jdbc:mysql://localhost:3306/expensmanager",
     * "root","root"); Statement smt=con.createStatement(); int
     * i=smt.executeUpdate("insert into expens values('"+category+"',"+Integer.
     * parseInt(quantity)+","+Float.parseFloat(price)+",'"+name+"','"+date+"')");
     * if(i>0) { JOptionPane.showMessageDialog(contentPane, "Successfully Store");
     * }else { JOptionPane.showMessageDialog(contentPane,
     * "Error-Message","There is problem",JOptionPane.ERROR_MESSAGE); } }catch
     * (Exception e1) { System.out.println(e1); }
     */
    /*
     * try { Class.forName("com.mysql.jdbc.Driver"); Connection
     * con=DriverManager.getConnection("jdbc:mysql://localhost:3306/expensmanager",
     * "root","root"); PreparedStatement
     * ps=con.prepareStatement("insert into expens values(?,?,?,?)");
     * ps.setString(1,category); ps.setInt(2, Integer.parseInt(quantity));
     * ps.setFloat(3,Float.parseFloat(price)); ps.setString(4, name);
     * ps.setString(5, date); int i=ps.executeUpdate(); //int
     * i=smt.executeUpdate("('"+category+"',"+Integer.parseInt(quantity)+","+Float.
     * parseFloat(price)+",'"+name+"','"+date+"')"); if(i>0) {
     * JOptionPane.showMessageDialog(contentPane, "Successfully Store"); }else {
     * JOptionPane.showMessageDialog(contentPane,
     * "Error-Message","There is problem",JOptionPane.ERROR_MESSAGE); } }catch
     * (Exception e1) { System.out.println(e1); }
     */

    try {
     Class.forName("com.mysql.jdbc.Driver");
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/expensmanager", "root",
       "root");
     CallableStatement ps = con.prepareCall("{call insertExpens(?,?,?,?,?)}");
     ps.setString(1,category); 
     ps.setInt(2, Integer.parseInt(quantity));
     ps.setFloat(3,Float.parseFloat(price));
     ps.setString(4, name);
     ps.setString(5, date);
     int i=ps.executeUpdate();
     if(i>0) {
       JOptionPane.showMessageDialog(contentPane, "Successfully Store"); }else {
       JOptionPane.showMessageDialog(contentPane,
       "Error-Message","There is problem",JOptionPane.ERROR_MESSAGE); }
    } catch (Exception e1) {
     System.out.println(e1);
    }

   }
  });
 }
}


Fetch Data from Database and add into JTable

No comments: