Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
  • develop
  • v1.0.0
  • v1.1.0
  • v1.2.0
5 results

Target

Select target project
  • nicolas.dagneaux.etu / Utils / Java Utils / Java Web Utils
1 result
Select Git revision
  • main
  • develop
  • v1.0.0
  • v1.1.0
  • v1.2.0
5 results
Show changes

Commits on Source 2

8 files
+ 169
0
Compare changes
  • Side-by-side
  • Inline

Files

java-utils @ 31e85409

Original line number Original line Diff line number Diff line
Subproject commit 31e85409a0d548c7d6c13033a09be1c2a7ab5dfd
+7 −0
Original line number Original line Diff line number Diff line
@@ -20,4 +20,11 @@
			</plugin>
			</plugin>
		</plugins>
		</plugins>
	</build>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>42.7.1</version>
		</dependency>
	</dependencies>
</project>
</project>
Original line number Original line Diff line number Diff line
package fr.univlille.iut.info.webutils.model.data;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

abstract class DAOBasic<T>
{
    private static final Map<Class, Method> sqlGetters;
    static
    {
        try
        {
            sqlGetters = Map.of(
                    String.class, ResultSet.class.getDeclaredMethod("getString", String.class)
            );
        } catch (NoSuchMethodException e)
        {
            throw new RuntimeException(e);
        }
    }

    private Class<T> pojoClass;
    private Field[] pojoAttributes;
    private Constructor<T> pojoConstructor;
    private List<JDBCReflection> jdbcReflectionsMethods;
    private Object[] pojoConstructorParams;
    private String tableName;

    public DAOBasic(Class<T> pojoClass) throws NoSuchMethodException
    {
        this.pojoClass = pojoClass;
        this.pojoAttributes = pojoClass.getDeclaredFields();
        this.pojoConstructor = pojoClass.getConstructor(pojoClass.getConstructors()[0].getParameterTypes());
        this.jdbcReflectionsMethods = new ArrayList<JDBCReflection>(pojoClass.getDeclaredFields().length);
        this.pojoConstructorParams = new Object[pojoClass.getDeclaredFields().length];
        for (Field attribute : this.pojoAttributes)
        {
            this.jdbcReflectionsMethods.add(new JDBCReflection(DAOBasic.sqlGetters.get(attribute.getType()), attribute.getName()));
        }
        this.tableName = pojoClass.getSimpleName().toLowerCase();
    }

    public List<T> findAll() throws SQLException, IOException, ClassNotFoundException
    {
        String sqlRequest = "SELECT * FROM " + this.tableName;
        DBLoop<List<T>> loop = new DBLoop<List<T>>(new ArrayList<T>(), sqlRequest, (((list, resultSet) ->
        {
            for (int i = 0; i < this.pojoAttributes.length; i++)
            {
                try
                {
                    this.pojoConstructorParams[i] = this.jdbcReflectionsMethods.get(i).execute(resultSet);
                }
                catch (InvocationTargetException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            try
            {
                list.add(this.pojoClass.cast(this.pojoConstructor.newInstance(this.pojoConstructorParams)));
            }
            catch (InstantiationException | IllegalAccessException | InvocationTargetException e)
            {
                throw new RuntimeException(e);
            }
        })));
        return loop.executeLoop();
    }
}
 No newline at end of file
Original line number Original line Diff line number Diff line
package fr.univlille.iut.info.webutils.model.data;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;

class JDBCReflection
{
    private Method sqlMethod;
    private String colName;

    public JDBCReflection(Method sqlMethod, String colName)
    {
        this.sqlMethod = sqlMethod;
        this.colName = colName;
    }

    public Method getSqlMethod()
    {
        return sqlMethod;
    }

    public Object execute(ResultSet rs) throws InvocationTargetException, IllegalAccessException
    {
        return this.sqlMethod.invoke(rs, this.colName);
    }
}
Original line number Original line Diff line number Diff line
package fr.univlille.iut.info.webutils.model.data;

import java.util.List;

public class DAOBasicTest
{
    public static void main(String[] args) throws Exception
    {
        Class.forName("org.postgresql.Driver");
        DBConnector.init(new DBConnectionDescriptor(
                "org.postgresql.Driver",
                "jdbc:postgresql://localhost:5432/but2",
                "nicolasdagneauxetu",
                "moi"));

        EtudiantDAO dao = new EtudiantDAO();

        List<Etudiant> etus = dao.findAll();
        System.out.println(etus);
    }
}
Original line number Original line Diff line number Diff line
package fr.univlille.iut.info.webutils.model.data;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class EtudiantDAO extends DAOBasic<Etudiant>
{
    public EtudiantDAO() throws NoSuchMethodException
    {
        super(Etudiant.class);
    }

    public List<Etudiant> findAll() throws SQLException, IOException, ClassNotFoundException
    {
        return super.findAll();
    }
}

web.utils.iml

0 → 100644
+8 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
  <component name="AdditionalModuleElements">
    <content url="file://$MODULE_DIR$" dumb="true">
      <excludeFolder url="file://$MODULE_DIR$/old" />
    </content>
  </component>
</module>
 No newline at end of file