Skip to content
Snippets Groups Projects
Commit 1b2edf12 authored by thtamagnau's avatar thtamagnau
Browse files

OUIIIIII

parents
No related branches found
No related tags found
1 merge request!1OUIIIIII
package com.uca.dao;
import com.uca.entity.UserEntity;
import java.sql.*;
import java.util.ArrayList;
public class UserDAO extends _Generic<UserEntity> {
public ArrayList<UserEntity> getAllUsers() {
ArrayList<UserEntity> entities = new ArrayList<>();
try {
PreparedStatement preparedStatement = this.connect.prepareStatement("SELECT * FROM users ORDER BY id ASC;");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
UserEntity entity = new UserEntity();
entity.setId(resultSet.getInt("id"));
entity.setFirstName(resultSet.getString("firstname"));
entity.setLastName(resultSet.getString("lastname"));
entities.add(entity);
}
} catch (SQLException e) {
e.printStackTrace();
}
return entities;
}
@Override
public UserEntity create(UserEntity obj) {
//TODO !
return null;
}
@Override
public void delete(UserEntity obj) {
//TODO !
}
}
package com.uca.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class _Connector {
private static String url = "jdbc:h2:~/test";
private static String user = "sa";
private static String passwd = "";
private static Connection connect;
public static Connection getInstance(){
if(connect == null){
try {
connect = DriverManager.getConnection(url, user, passwd);
} catch (SQLException e) {
e.printStackTrace();
}
}
return connect;
}
}
package com.uca.dao;
import java.sql.Connection;
public abstract class _Generic<T> {
public Connection connect = _Connector.getInstance();
/**
* Permet de créer une entrée dans la base de données
* par rapport à un objet
* @param obj
*/
public abstract T create(T obj);
/**
* Permet la suppression d'une entrée de la base
* @param obj
*/
public abstract void delete(T obj);
}
package com.uca.dao;
import java.sql.*;
public class _Initializer {
public static void Init(){
Connection connection = _Connector.getInstance();
try {
PreparedStatement statement;
//Init articles table
statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS users (id int primary key auto_increment, firstname varchar(100), lastname varchar(100)); ");
statement.executeUpdate();
//Todo Remove me !
statement = connection.prepareStatement("INSERT INTO users(firstname, lastname) VALUES(?, ?);");
statement.setString(1, "Theodore");
statement.setString(2, "Muillerez");
statement.executeUpdate();
} catch (Exception e){
System.out.println(e.toString());
throw new RuntimeException("could not create database !");
}
}
}
package com.uca.entity;
public class Eleve extends UserEntity
{
}
package com.uca.entity;
import java.sql.Timestamp;
public class Gomette
{
public enum Couleur { Verte, Rouge, Blanc, ArcEnCiel }
private Couleur couleur;
public Couleur couleur() { return couleur; }
private String description;
public String description() { return description; }
private Eleve recipiendaire;
public Eleve recipiendaire() { return recipiendaire; }
private Prof attribuant;
public Prof attribuant() { return attribuant; }
private Date date;
public Date date() { return date; }
public Gomette(Couleur couleur, String description, Eleve recipiendaire, Prof attribuant, Date date)
{
this.couleur = couleur;
this.description = description;
this.recipiendaire = recipiendaire;
this.attribuant = attribuant;
this.date = date;
}
}
package com.uca.entity;
public class Prof : UserEntity
{
}
package com.uca.entity;
import java.sql.Timestamp;
import java.util.*;
public class UserEntity {
private String firstName;
private String lastName;
private int id;
private Set<Gomette> gomettes;
public UserEntity() {
//Ignored !
load();
}
private void load()
{
gomettes = new HashSet<Gomette>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean ajouterGomette(Gomette g)
{
return gomettes.add(g);
}
}
package com.uca.gui;
import com.uca.core.UserCore;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class UserGUI {
public static String getAllUsers() throws IOException, TemplateException {
Configuration configuration = _FreeMarkerInitializer.getContext();
Map<String, Object> input = new HashMap<>();
input.put("users", UserCore.getAllUsers());
Writer output = new StringWriter();
Template template = configuration.getTemplate("users/users.ftl");
template.setOutputEncoding("UTF-8");
template.process(input, output);
return output.toString();
}
}
package com.uca.gui;
import com.uca.StartServer;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import java.util.Locale;
public class _FreeMarkerInitializer {
public static Configuration getContext() {
//Configure FreeMarker
Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
configuration.setClassForTemplateLoading(StartServer.class, "/views");
configuration.setDefaultEncoding("UTF-8");
configuration.setLocale(Locale.FRANCE);
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
return configuration;
}
}
<#ftl encoding="utf-8">
<body xmlns="http://www.w3.org/1999/html">
<ul>
<#list users as user>
<li>${user.id} - ${user.firstName} ${user.lastName}</li>
</#list>
</ul>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment