Dúvida com linha de comando

2 respostas
L

Galera é o seguinte, eu preciso de uma aplicação que funcione com linha de comando… para filtrar mais ainda essa minha aplicação é um CRUD!

Eu preciso consultar, alterar, excluir e incluir! O porém é que isso precisa ser em linha de comando, por exemplo:

-A -C Exemplo Exemplo1 -S ServidorExemplo ServidorExemplo1

Onde nessa linha de cima -A seja a opção de alterar, -C é a opção de que eu estou alterando o componente “Exemplo” para “Exemplo1” e -S a opção de que estou alterando o servidor “ServidorExemplo” “ServidorExemplo1”

2 Respostas

E

Ora pois pois. Basta interpretar os argumentos que são passados para o main, dentro do array String[] args.

No seu caso:

args[0] = -A
args[1] = -C , args[2] =Exemplo, args[3] = Exemplo1
args[4] = -S , args[5] = ServidorExemplo , args[6] = ServidorExemplo1

É só trabalhoso, não complicado. Trabalhe!

L

Como eu uso begin transaction nesse cara?

No método deletar, incluir, consultar e alterar?

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class DAO {
	Conexao conexao = new Conexao();
	public String filtroAlterar;
	public String filtroAdd = "";
	Scanner entrada = new Scanner(System.in);
	InfraBridge infraBridge = new InfraBridge();
	LoggingTest log = new LoggingTest();

	private boolean errado;
	private boolean ok;
	private int idComponente;
	private int idDestino;
	private int idOrigem;
	private String componente;
	private String verificaComponente;
	private int ordem1;
	private int verificaOrdem1;
	private int ordem2;
	private int verificaOrdem2;
	private String UPDATE1, UPDATE2, UPDATE3; // UPDATE4, UPDATE5;
	private String DELETAR1, DELETAR2, DELETAR3, DELETAR4, DELETAR5;
	private String INCLUIR_ORDEM1;
	private String INCLUIR_ORDEM2;
	private final static String INCLUIR_TFWKIBDG_CMPT = ("INSERT INTO TFWKIBDG_CMPT (NOME_CMPT,NOME_ALIA_CMPT)VALUES (?,?)");
	private final static String INCLUIR_TFWKIBDG_GRUP_SERD = ("INSERT INTO TFWKIBDG_GRUP_SERD (NOME_GRUP_SERD)VALUES (?)");
	private final static String INCLUIR_TFWKIBDG_SERD = ("INSERT INTO TFWKIBDG_SERD (NOME_SERD)VALUES (?)");
	private final static String CONSULTA = " SELECT SERD.COD_SERD, GRD_SERD.COD_GRUP_SERD, NUM_ORDE_GRUP, NUM_ORDE_SERD, NOME_GRUP_SERD, NOME_SERD, NOME_ALIA_CMPT ,CMPT.COD_CMPT, NOME_CMPT  FROM TFWKIBDG_CMPT_GRUP_SERD COMP WITH(NOLOCK)"
			+ " INNER JOIN TFWKIBDG_GRUP_SERD_SERD GRP_SERD_SERD WITH(NOLOCK)"
			+ " ON COMP.COD_GRUP_SERD = GRP_SERD_SERD.COD_GRUP_SERD"
			+ " INNER JOIN TFWKIBDG_GRUP_SERD GRD_SERD WITH(NOLOCK)"
			+ " ON GRP_SERD_SERD.COD_GRUP_SERD = GRD_SERD.COD_GRUP_SERD"
			+ " INNER JOIN TFWKIBDG_SERD SERD WITH(NOLOCK)"
			+ " ON GRP_SERD_SERD.COD_SERD = SERD.COD_SERD"
			+ " INNER JOIN TFWKIBDG_CMPT CMPT WITH(NOLOCK)"
			+ " ON COMP.COD_CMPT = CMPT.COD_CMPT";
	private String FILTRO;
	private String CONSULTAD;
	private String CONSULTAC;
	private String CONSULTAO1;
	private String CONSULTAO2;

	// aprimorar para string buffer.*******
	/*
	 * public String getConsulta(final Integer codigo) { StringBuffer sql = new
	 * StringBuffer(); sql.append("select * from table ");
	 * sql.append("where table.codigo = "); sql.append(codigo); return
	 * sql.toString(); }
	 */

	public InfraBridge[] consulta() {
		Conexao conexao = new Conexao();
		InfraBridge[] ib = null;
		try {
			ResultSet rs = null;
			componente = infraBridge.getComponente();
			if (componente != null) {
				FILTRO = (" WHERE NOME_CMPT = " + "'" + componente + "'");
				CONSULTAD = CONSULTA + FILTRO;
				rs = conexao.executarConsulta(CONSULTAD);
			}
			if (componente.isEmpty() || componente == null) {
				rs = conexao.executarConsulta(CONSULTA);
			}

			List<InfraBridge> result = new LinkedList<InfraBridge>();

			System.out.println(); // formatação de linha

			if (rs.next()) {
				infraBridge.imprimirColuna();
				do {

					// InfraBridge infraBridge = new InfraBridge();

					infraBridge.setOrdem1(rs.getInt("NUM_ORDE_GRUP"));
					infraBridge.setOrdem2(rs.getInt("NUM_ORDE_SERD"));
					infraBridge.setidDestino(rs.getInt("COD_GRUP_SERD"));
					infraBridge.setidOrigem(rs.getInt("COD_SERD"));
					infraBridge.setidComponente(rs.getInt("COD_CMPT"));
					infraBridge.setServidorDestino(rs
							.getString("NOME_GRUP_SERD"));
					infraBridge.setServidorOrigem(rs.getString("NOME_SERD"));
					infraBridge.setComponente(rs.getString("NOME_CMPT"));
					infraBridge.setAliasComponente(rs
							.getString("NOME_ALIA_CMPT"));

					result.add(infraBridge);
					infraBridge.imprimirRS();

				} while (rs.next());

				conexao.fecharResultSet(rs);

				ib = new InfraBridge[result.size()];
				result.toArray(ib);
				errado = true;
			} else {
				System.out.println("O componente escolhido não existe.");
				errado = false;
			}

		} catch (SQLException ex) {
			// log.logger(ex.toString());
			throw new RuntimeException(ex);
		} finally {
			conexao.desconnection();
		}
		// log.logger.info(ib);
		return ib;
	}

	public void incluir() {
		Conexao conexao = new Conexao();
		consulta();
		if (errado == false) {
			idComponente = conexao.execDML(
					INCLUIR_TFWKIBDG_CMPT,
					new Object[] { infraBridge.getComponente(),
							infraBridge.getAliasComponenteAlterar() });
			idDestino = conexao.execDML(INCLUIR_TFWKIBDG_GRUP_SERD,
					new Object[] { infraBridge.getServidorDestinoAlterar() });
			idOrigem = conexao.execDML(INCLUIR_TFWKIBDG_SERD,
					new Object[] { infraBridge.getServidorOrigemAlterar() });

			INCLUIR_ORDEM1 = ("INSERT INTO TFWKIBDG_CMPT_GRUP_SERD (COD_CMPT, COD_GRUP_SERD, NUM_ORDE_GRUP) VALUES ("
					+ idComponente + "," + idDestino + ",?)");
			INCLUIR_ORDEM2 = ("INSERT INTO TFWKIBDG_GRUP_SERD_SERD (COD_GRUP_SERD, COD_SERD, NUM_ORDE_SERD) VALUES ("
					+ idDestino + "," + idOrigem + ",?)");
			conexao.updateDML(INCLUIR_ORDEM1,
					new Object[] { infraBridge.getOrdem1Alterar() });
			conexao.updateDML(INCLUIR_ORDEM2,
					new Object[] { infraBridge.getOrdem2Alterar() });
			System.out.println("Componente incluido com sucesso");
		} else {
			System.out.println(HELP);
		}
	}

	public void alterar() {

		Conexao conexao = new Conexao();
		consulta();
		if (errado == true) {

			UPDATE1 = "UPDATE TFWKIBDG_CMPT SET NOME_CMPT = '"
					+ infraBridge.getComponenteAlterar()
					+ "' WHERE COD_CMPT = " + infraBridge.getidComponente();
			conexao.executarDML(UPDATE1);
			UPDATE2 = "UPDATE TFWKIBDG_SERD SET NOME_SERD = '"
					+ infraBridge.getServidorOrigemAlterar()
					+ "' WHERE COD_SERD = " + infraBridge.getidOrigem();
			conexao.executarDML(UPDATE2);
			UPDATE3 = "UPDATE TFWKIBDG_GRUP_SERD SET NOME_GRUP_SERD = '"
					+ infraBridge.getServidorDestinoAlterar()
					+ "' WHERE COD_GRUP_SERD = " + infraBridge.getidDestino();
			conexao.executarDML(UPDATE3);
		} else {
			System.out.println(HELP);
		}
	}

	public void deletar() {
		Conexao conexao = new Conexao();
		consulta();
		if (errado == true) {
			DELETAR1 = ("DELETE FROM TFWKIBDG_CMPT_GRUP_SERD WHERE COD_CMPT = '"
					+ infraBridge.getidComponente() + "'");
			conexao.executarDML(DELETAR1);
			DELETAR2 = ("DELETE FROM TFWKIBDG_GRUP_SERD_SERD WHERE COD_SERD = '"
					+ infraBridge.getidOrigem() + "'");
			conexao.executarDML(DELETAR2);
			DELETAR3 = ("DELETE FROM TFWKIBDG_CMPT WHERE COD_CMPT = '"
					+ infraBridge.getidComponente() + "'");
			conexao.executarDML(DELETAR3);
			DELETAR4 = ("DELETE FROM TFWKIBDG_GRUP_SERD WHERE COD_GRUP_SERD = '"
					+ infraBridge.getidDestino() + "'");
			conexao.executarDML(DELETAR4);
			DELETAR5 = ("DELETE FROM TFWKIBDG_SERD WHERE COD_SERD = '"
					+ infraBridge.getidOrigem() + "'");
			conexao.executarDML(DELETAR5);
			System.out.println("O componente foi removido do banco de dados.");
		} else {
			System.out.println(HELP);
		}
	}

	public String consultaComponente() {
		Conexao conexao = new Conexao();
		CONSULTAC = "SELECT NOME_CMPT FROM TFWKIBDG_CMPT WHERE NOME_CMPT = '"
				+ componente + "'";
		verificaComponente = null;
		try {
			ResultSet rs = conexao.executarConsulta(CONSULTAC);

			if (rs.next()) {
				verificaComponente = rs.getString("NOME_CMPT");
			}
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		} finally {
			conexao.desconnection();
		}
		return verificaComponente;
	}

	public int consultaOrdem1() {
		Conexao conexao = new Conexao();
		CONSULTAO1 = "SELECT NUM_ORDE_GRUP FROM TFWKIBDG_CMPT_GRUP_SERD WHERE NUM_ORDE_GRUP = '"
				+ ordem1 + "'";
		verificaOrdem1 = 0;
		try {
			ResultSet rs = conexao.executarConsulta(CONSULTAO1);

			if (rs.next()) {
				verificaOrdem1 = rs.getInt("NUM_ORDE_GRUP");
			}
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		} finally {
			conexao.desconnection();
		}
		return verificaOrdem1;
	}

	public int consultaOrdem2() {
		Conexao conexao = new Conexao();
		CONSULTAO2 = "SELECT NUM_ORDE_SERD FROM TFWKIBDG_GRUP_SERD_SERD WHERE NUM_ORDE_SERD = '"
				+ ordem2 + "'";
		verificaOrdem2 = 0;
		try {
			ResultSet rs = conexao.executarConsulta(CONSULTAO2);

			if (rs.next()) {
				verificaOrdem2 = rs.getInt("NUM_ORDE_SERD");
			}
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		} finally {
			conexao.desconnection();
		}
		return verificaOrdem2;
	}

}
Criado 14 de maio de 2013
Ultima resposta 14 de mai. de 2013
Respostas 2
Participantes 2