iifast

Revision Project 2 - MyApplication.java

Mar 13th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. // Revision Project 2
  2. // https://imgur.com/a/iNltksZ
  3. // MyApplication.java
  4.  
  5. package com.mycompany.myapp;
  6.  
  7. import com.codename1.db.Cursor;
  8. import com.codename1.db.Database;
  9. import com.codename1.db.Row;
  10. import com.codename1.ui.Display;
  11. import com.codename1.ui.Form;
  12. import com.codename1.ui.Dialog;
  13. import com.codename1.ui.Label;
  14. import com.codename1.ui.plaf.UIManager;
  15. import com.codename1.ui.util.Resources;
  16. import com.codename1.io.Log;
  17. import com.codename1.ui.Button;
  18. import com.codename1.ui.TextField;
  19. import com.codename1.ui.Toolbar;
  20. import com.codename1.ui.events.ActionEvent;
  21. import com.codename1.ui.events.ActionListener;
  22. import com.codename1.ui.layouts.BoxLayout;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25.  
  26. /**
  27.  * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose
  28.  * of building native mobile applications using Java.
  29.  */
  30. public class MyApplication {
  31.  
  32.     private Form current;
  33.     private Resources theme;
  34.      public void init(Object context) {
  35.         theme = UIManager.initFirstTheme("/theme");
  36.  
  37.         // Enable Toolbar on all Forms by default
  38.         Toolbar.setGlobalToolbar(true);
  39.        
  40.         // Pro only feature, uncomment if you have a pro subscription
  41.         // Log.bindCrashProtection(true);
  42.     }
  43.    
  44.     Database db;
  45.     boolean created =false;
  46.     public void start() {
  47.    
  48.         created = Database.exists("2cinfo");
  49.        
  50.         try {
  51.             db = Database.openOrCreate("2cinfo");
  52.            
  53.            if(created == false) db.execute("create table user (id INTEGER, nom TEXT, prenom TEXT);");
  54.            
  55.            
  56.         } catch (IOException ex) {
  57.         }
  58.        
  59.         System.out.println(Database.getDatabasePath("2cinfo"));
  60.         Form f = new Form(BoxLayout.y());
  61.         TextField tfNom = new TextField();
  62.         TextField tfPrenom = new TextField();
  63.         Button btnOk = new Button("Valider");
  64.        
  65.         f.add(tfNom);
  66.         f.add(tfPrenom);
  67.         f.add(btnOk);
  68.        
  69.         btnOk.addActionListener(new ActionListener() {
  70.  
  71.             @Override
  72.             public void actionPerformed(ActionEvent evt) {
  73.            
  74.                 try {
  75.                     String nom = tfNom.getText();
  76.                     String prenom = tfPrenom.getText();
  77.                    
  78.                     db.execute("insert into user (nom, prenom) values ('"+nom+"', '"+prenom+"' );");
  79.                     System.out.println("OK");
  80.                
  81.                 } catch (IOException ex) {
  82.                 }
  83.             }
  84.         });
  85.        
  86.        
  87.         Button btnAfficher = new Button("Affichage");
  88.         f.add(btnAfficher);
  89.         btnAfficher.addActionListener(new ActionListener() {
  90.           @Override
  91.             public void actionPerformed(ActionEvent evt) {
  92.         Form f2 = new Form(BoxLayout.y());
  93.                 try {
  94.                     Cursor c= db.executeQuery("select * from user");
  95.                    while (c.next()) {
  96.                         Row r = c.getRow();
  97.                         String nom = r.getString(1);
  98.                         String prenom = r.getString(2);
  99.                         Label l = new Label(nom+ " "+ prenom);
  100.                         f2.add(l);
  101.                       }
  102.                
  103.                 } catch (IOException ex) {
  104.                 }
  105.            
  106.                 f2.show();
  107.             }
  108.         });
  109.        
  110.        
  111.        f.show();
  112.        
  113.     }
  114.  
  115.     public void stop() {
  116.         current = Display.getInstance().getCurrent();
  117.         if(current instanceof Dialog) {
  118.             ((Dialog)current).dispose();
  119.             current = Display.getInstance().getCurrent();
  120.         }
  121.     }
  122.    
  123.     public void destroy() {
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment