Structure FTP connection using MVC model in Java

1

My intention is to use the MVC model that I use for the connection in my database and apply it to the connection to my FTP server, first I explain how I connect to my BD.

This is the connection class belonging to the Model layer

public class Conector {
public static Connection connect(){
String url="D:\ANGELO\Base de Datos SQLite\SISGO\sisgo.db";
Connection connect=null;
try {
    Class.forName("org.sqlite.JDBC");
    connect= DriverManager.getConnection("jdbc:sqlite:"+url);
    if (connect!=null) {
        System.out.println("Conectado");
    }
} catch (SQLException ex) {
    System.err.println("No se ha podido conectar a la base de datos\n"+ex.getMessage());
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return connect;
}

After that, this is the connection to do all my CRUD in the DTO_bo. For example, the following line of code adds a user

public class Usuario_DTO_bo implements Usuario_DAO {
Connection con=null; //Creando la variable conexion para que sea usada por todos los metodos
@Override
//Metodo para agregar usuario
public void agregarUsuario(Usuario_DTO_bean usuario) {
    try {
        String salto=PasswordUtils.getSalt(30);
        String pass=PasswordUtils.generateSecurePassword(usuario.getClave(), salto);
        con=Conector.connect();
        PreparedStatement st= con.prepareStatement("insert into usuario (usuario,clave,permisos,estado,apellido_p,apellido_m,nombre,salto) VALUES (?,?,?,?,?,?,?,?);");
                st.setString(1, usuario.getUsuario());
                st.setString(2, pass);
                st.setString(3, usuario.getPermisos());
                st.setInt(4, usuario.getEstado());
                st.setString(5, usuario.getApellido_p());
                st.setString(6, usuario.getApellido_m());
                st.setString(7, usuario.getNombre());
                st.setString(8, salto);
                st.execute();
        con.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

The code to add a user does not matter, the question is that using the line

con=Conector.connect();

I can easily call the connection, and this is useful to call it in any class, so I hope you understand how to structure my call to the connection.

THE IMPORTANT

Now, I found a code to connect to my FTP server and it works perfectly, it sends me the results by console successfully.

package Database;

//aqui van muchas bibliotecas que no puse para no llenar espacio :)

public class prueba {

public static void main(String[] args) throws ClassNotFoundException, 
IOException  {

String server = "10.175.51.25";
    int port = 21;
    String user = "anonymous";
    String pass = "anonymous";
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        showServerReply(ftpClient);
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("Operation failed. Server reply code: " + replyCode);
            return;
        }
        boolean success = ftpClient.login(user, pass);
        showServerReply(ftpClient);
        if (!success) {
            System.out.println("Could not login to the server");
            return;
        } else {
            System.out.println("LOGGED IN SERVER");
        }

           ftpClient.logout();
           ftpClient.disconnect();


    } 

    catch (IOException ex) {
        System.out.println("Oops! Something wrong happened");
        ex.printStackTrace();
    }

}
private static void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }


}
}

The point is that this works perfectly for me in a main, the question is to be able to structure it to use it in MVC on the web, and just as I did with the connection to the database, I could invoke the connection to my FTP in only one line something like

ftp=Conector_FTP.connect();
    
asked by angelo1793 21.08.2018 в 18:36
source

0 answers