Advertisement
Guest User

Untitled

a guest
May 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import com.mysql.jdbc.*;
  6.  
  7. public class SuiviPoids extends JFrame{
  8.  
  9.     private JCheckBox muscu;
  10.     private JCheckBox footing;
  11.     private JTextField poids;
  12.     private JTextArea observation;
  13.  
  14.     public SuiviPoids(){
  15.         super();
  16.         init();
  17.         JPanel principal = new JPanel();
  18.         creerInterface(principal);
  19.         add(principal);
  20.     }
  21.    
  22.     private void init(){
  23.         setTitle("Suivi du poids");
  24.         setSize(265,200);
  25.         setLocationRelativeTo(null);
  26.         //setResizable(false);
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         setVisible(true);
  29.     }
  30.    
  31.     private void creerInterface(JPanel p){
  32.         // Création des éléments d'interface
  33.         muscu = new JCheckBox();
  34.         JLabel lMuscu = new JLabel("Muscu");
  35.         footing = new JCheckBox();
  36.         JLabel lFooting = new JLabel("Footing");
  37.         poids = new JTextField(5);
  38.         JLabel lPoids = new JLabel("Poids");
  39.         observation = new JTextArea(3,20);
  40.         JLabel lObservation = new JLabel("Observation");
  41.         JButton envoyer = new JButton("Envoyer");
  42.         envoyer.addActionListener(new EnvoiDonnees());
  43.        
  44.         JLabel feedback = new JLabel("");
  45.        
  46.         // Placement des éléments d'interface
  47.         p.setLayout(new FlowLayout());
  48.         p.add(lPoids);
  49.         p.add(poids);
  50.        
  51.         p.add(lMuscu);
  52.         p.add(muscu);
  53.        
  54.         p.add(lFooting);
  55.         p.add(footing);
  56.        
  57.         p.add(lObservation);
  58.         p.add(observation);
  59.        
  60.         p.add(envoyer);
  61.         p.add(feedback);
  62.     }
  63.     private class EnvoiDonnees implements ActionListener{
  64.         public void actionPerformed(ActionEvent e){
  65.             // Connexion bdd
  66.             String dbname="ccc";
  67.             String login="cccc";
  68.             String mdp="ccc";
  69.             String host="cccccc.com";
  70.             String port="3306";
  71.  
  72.             try {
  73.                 Class.forName("com.mysql.jdbc.Driver");
  74.  
  75.                 String connectionUrl = "jdbc:mysql://"+host+":"+port+"/"+dbname+"?user="+login+"&password="+mdp;
  76.                 java.sql.Connection con = (java.sql.Connection) DriverManager.getConnection(connectionUrl);
  77.             }
  78.             catch (ClassNotFoundException cE) {
  79.                 System.out.println("Class Not Found Exception: "+ cE.toString());
  80.             }
  81.             catch (SQLException sqle) {
  82.                 System.out.println("SQL Exception: "+ sqle.toString());
  83.             }
  84.            
  85.             // Préparation requete
  86.             String requete = "INSERT INTO Poids VALUES(0,?,?,?,?)";
  87.             /*try{
  88.                 PreparedStatement pst = (PreparedStatement) bdd.prepareStatement(requete);
  89.                 pst.setDate(1, getCurrentJavaSqlDate());
  90.                 pst.setFloat(2, Float.parseFloat(poids.getText()));
  91.                 Byte b;
  92.                 b = (muscu.isSelected())?new Integer(1).byteValue():new Integer(0).byteValue();
  93.                 pst.setByte(3, b);
  94.                 b = (footing.isSelected())?new Integer(1).byteValue():new Integer(0).byteValue();
  95.                 pst.setByte(4, b);
  96.                
  97.                 // Envoi données
  98.                 //int numRowsChanged = pst.executeUpdate();
  99.                 pst.close();
  100.             }catch(SQLException sqle){sqle.printStackTrace();}
  101.             */
  102.             // Envoi feedback
  103.             /*
  104.             System.out.println(muscu.isSelected());
  105.             System.out.println(footing.isSelected());
  106.             System.out.println(getCurrentJavaSqlDate());
  107.             System.out.println(poids.getText());
  108.             */
  109.         }
  110.     }
  111.    
  112.     public static java.sql.Date getCurrentJavaSqlDate() {
  113.         java.util.Date today = new java.util.Date();
  114.         return new java.sql.Date(today.getTime());
  115.      }
  116.    
  117.     public static void main(String[] args){
  118.         new SuiviPoids();
  119.     }
  120.  
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement