Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Revision Project 2
- // https://imgur.com/a/iNltksZ
- // MyApplication.java
- package com.mycompany.myapp;
- import com.codename1.db.Cursor;
- import com.codename1.db.Database;
- import com.codename1.db.Row;
- import com.codename1.ui.Display;
- import com.codename1.ui.Form;
- import com.codename1.ui.Dialog;
- import com.codename1.ui.Label;
- import com.codename1.ui.plaf.UIManager;
- import com.codename1.ui.util.Resources;
- import com.codename1.io.Log;
- import com.codename1.ui.Button;
- import com.codename1.ui.TextField;
- import com.codename1.ui.Toolbar;
- import com.codename1.ui.events.ActionEvent;
- import com.codename1.ui.events.ActionListener;
- import com.codename1.ui.layouts.BoxLayout;
- import java.io.IOException;
- import java.util.ArrayList;
- /**
- * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose
- * of building native mobile applications using Java.
- */
- public class MyApplication {
- private Form current;
- private Resources theme;
- public void init(Object context) {
- theme = UIManager.initFirstTheme("/theme");
- // Enable Toolbar on all Forms by default
- Toolbar.setGlobalToolbar(true);
- // Pro only feature, uncomment if you have a pro subscription
- // Log.bindCrashProtection(true);
- }
- Database db;
- boolean created =false;
- public void start() {
- created = Database.exists("2cinfo");
- try {
- db = Database.openOrCreate("2cinfo");
- if(created == false) db.execute("create table user (id INTEGER, nom TEXT, prenom TEXT);");
- } catch (IOException ex) {
- }
- System.out.println(Database.getDatabasePath("2cinfo"));
- Form f = new Form(BoxLayout.y());
- TextField tfNom = new TextField();
- TextField tfPrenom = new TextField();
- Button btnOk = new Button("Valider");
- f.add(tfNom);
- f.add(tfPrenom);
- f.add(btnOk);
- btnOk.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- try {
- String nom = tfNom.getText();
- String prenom = tfPrenom.getText();
- db.execute("insert into user (nom, prenom) values ('"+nom+"', '"+prenom+"' );");
- System.out.println("OK");
- } catch (IOException ex) {
- }
- }
- });
- Button btnAfficher = new Button("Affichage");
- f.add(btnAfficher);
- btnAfficher.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- Form f2 = new Form(BoxLayout.y());
- try {
- Cursor c= db.executeQuery("select * from user");
- while (c.next()) {
- Row r = c.getRow();
- String nom = r.getString(1);
- String prenom = r.getString(2);
- Label l = new Label(nom+ " "+ prenom);
- f2.add(l);
- }
- } catch (IOException ex) {
- }
- f2.show();
- }
- });
- f.show();
- }
- public void stop() {
- current = Display.getInstance().getCurrent();
- if(current instanceof Dialog) {
- ((Dialog)current).dispose();
- current = Display.getInstance().getCurrent();
- }
- }
- public void destroy() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment