Converter Array de String para INT - Struts 2

3 respostas
F

Alguém sabe como posso fazer isso?

3 Respostas

H
Creio que isso aqui pode te ajudar:
String[] valoresStr = {"1", "2", "3"};
        Integer[] valoresInt = new Integer[valoresStr.length];

        for (int i = 0; i < valoresStr.length; i++) {
        	valoresInt[i] = Integer.parseInt(valoresStr[i]);
        }
F
package br.com.scb.modelo;

public class Componentes {
	private int id_componentes_projetos;
	private int[] id_alunos;
	private int id_projetos;
	
	public int getId_componentes_projetos() {
		return id_componentes_projetos;
	}
	public void setId_componentes_projetos(int id_componentes_projetos) {
		this.id_componentes_projetos = id_componentes_projetos;
	}
	public int[] getId_alunos() {
		return id_alunos;
	}
	public void setId_alunos(String[] id_alunos) {
		//converte de String[] para Int[]
		//guarda no id alunos
		String[] valoresStr = id_alunos;  
		Integer[] valoresInt = new Integer[valoresStr.length];
	}
	public int getId_projetos() {
		return id_projetos;
	}
	public void setId_projetos(int id_projetos) {
		this.id_projetos = id_projetos;
	}
	
}

Como faço para retornar essa variavel para meu DAO, para inserir os id_alunos na tabela?
Obrigado!

F

Desculpe a ignorância, é pq realmente eu tenho que retornar aquele setId_alunos para depois fazer o for no meu dao para poder gravar os dados no banco...

ComponentesDAO.java

package br.com.scb.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.com.scb.jdbc.ConnectionFactory;
import br.com.scb.modelo.Componentes;

public class ComponentesDAO {
	private final Connection connection;
	
	public ComponentesDAO(){
		connection = new ConnectionFactory().getConnection();
	}
	
	public void adiciona(final Componentes componentes) {
		final String sql = "INSERT INTO componentes_projetos (id_alunos, id_projetos) values (?,?)";
		PreparedStatement stmt;
		
			try {
				stmt = connection.prepareStatement(sql);
				stmt.setInt(1, componentes.getId_alunos());
				stmt.setInt(2, 1);
				stmt.execute();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
	}
}
Criado 4 de abril de 2012
Ultima resposta 4 de abr. de 2012
Respostas 3
Participantes 2