Inserir um array list no banco de dados

4 respostas
M

Pessoal, bom dia,
gostaria de saber como eu poderia adicionar um arraylist no banco de dados, da forma que os arquivos sejam separados da seguinte forma: mes, mes, mes segue o que estou tentando fazer no jsp:

<td><s:checkboxlist 
		list="#{'JANEIRO':'JANEIRO','FEVEREIRO':'FEVEREIRO','MARÇO':'MARÇO','ABRIL':'ABRIL','MAIO':'MAIO'}" name="meses" theme="simple"/><br></br>
		<s:checkboxlist 
		list="#{'JUNHO':'JUNHO','JULHO':'JULHO','AGOSTO':'AGOSTO','SETEMBRO':'SETEMBRO','OUTUBRO':'OUTUBRO'}" name="meses" theme="simple"/><br></br>
		
		<s:checkboxlist 
		list="#{'NOVEMBRO':'NOVEMBRO','DEZEMBRO':'DEZEMBRO'}" name="meses" theme="simple"/>
		
		</td>

segue o Action:

public class ControleDocumentosAction {
	private Arquivo arquivo;
	private List<Arquivo> arquivos;
	private String mensagemErro;
	private String nomeFrente;
	private List<String> meses;
	
	//PREENCHE NOME DAS FRENTES NA TELA DE CADASTRO DE DOCUMENTOS
	@Action(value="preencheTela", results= {
			@Result(name="ok", location="/paginas/cadastroDocumentos.jsp")
	})
	public String execute() throws Exception {
		
		arquivos = new ControleDocumentosDAO().preencheTela(arquivo);
		
		return "ok";
		
	}
		
	//CADASTRO DOS DOCUMENTOS
	@Action(value="cadastraDocumentos", results= {
			@Result(name="ok", type="redirectAction", params= {"actionName", "preencheTela"})
	})
	
	public String execute1() throws Exception {
		
		new ControleDocumentosDAO().cadastraDocumento(arquivo, meses);
		mensagemErro = "DOCUMENTO CADASTRADO COM SUCESSO!!";
		return "ok";
		
	}

e agora o DAO não sei como fazer para adiononar os meses selecionados no form e add a um campo da tabela da seguinte forma: JANEIRO, FEVEREIRO, MARÇO etc

//CADASTRO DOCUMENTO
	public void cadastraDocumento(Arquivo arquivo, List<String> meses) throws SQLException {
		PreparedStatement stmt;
		
							
			stmt = connection.prepareStatement("insert into servico values(null,?,?,?)");
			stmt.setString(1, arquivo.getServico().getDescricaoServico());
			stmt.setString(2, arquivo.getServico().getPalavraChave());
			stmt.setString(3, arquivo.getServico().getPalavraChave1());
			stmt.execute();
			
				
			PreparedStatement stmt2;
			stmt2 = connection.prepareStatement("select idservico from servico where descricaoServico = ? and palavraChave = ? and palavraChave1 = ?");
			stmt2.setString(1, arquivo.getServico().getDescricaoServico());
			stmt2.setString(2, arquivo.getServico().getPalavraChave());
			stmt2.setString(3, arquivo.getServico().getPalavraChave1());
			stmt.clearParameters();
			
			PreparedStatement stmt3;
			stmt3 = connection.prepareStatement("select idfrente from frente where nomeFrente =?");
			stmt3.setString(1, arquivo.getFrente().getNomeFrente());
			stmt.clearParameters();
			
				ResultSet rs = stmt2.executeQuery();
			
					if (rs.next()) {
				
						arquivo.getServico().setId(rs.getInt("idservico"));
												
					}
					
					ResultSet rs2 = stmt3.executeQuery();
						if (rs2.next()) {
							arquivo.getFrente().setId(rs2.getInt("idfrente"));
						}
					
			
			PreparedStatement stmt4;
			stmt4 = connection.prepareStatement("insert into arquivo values(null,?,?,?,?,?,?,?,?,?,?,?)");
			stmt4.setInt(1, arquivo.getFrente().getId());
			stmt4.setInt(2, arquivo.getServico().getId());
			stmt4.setString(3, arquivo.getNumerocontrato());
			stmt4.setString(4, arquivo.getMeses()); queria adicionar a lista neste campo
			stmt4.setString(5, arquivo.getPeriodo2());
			stmt4.setInt(6, arquivo.getAno1());
			stmt4.setInt(7, arquivo.getAno2());
			stmt4.setString(8, arquivo.getContrato());
			stmt4.setString(9, arquivo.getLocalArmazenamento());
			stmt4.setString(10, arquivo.getNumeroArmazenamento());
			stmt4.setString(11, arquivo.getLink());
			stmt4.execute();
			stmt4.clearParameters();
	}

obrigado a todos pela ajuda

4 Respostas

M

Dá uma ajuda ae galera

R

qual o tipo do campo no BD?

M

E string

M

pq??

Criado 25 de fevereiro de 2013
Ultima resposta 26 de fev. de 2013
Respostas 4
Participantes 2