Como jogar o valor de um select para uma variável no java?

5 respostas
F

Pessoal, irei da um select com a chave primária… uma variável no java deve receber esse valor… como posso fazer isso…
Estou usando Stored procedure.

Digito o código e o java exibe o nome do cliente que tem esse código.

5 Respostas

fredericoengels

Você vai executar a query e o objeto que vai receber o resultado dessa consulta ao banco é ResultSet. Da uma pesquisada sobre JDBC, na internet você vai encontrar vários exemplos

JoaoBluSCBR
CallableStatement cs; 
try { 
// Call a procedure with no parameters 
cs = connection.prepareCall("{call myproc}"); 
cs.execute(); 

// Call a procedure with one IN parameter 
cs = connection.prepareCall("{call myprocin(?)}"); 

// Set the value for the IN parameter 
cs.setString(1, "a string"); 

// Execute the stored procedure 
cs.execute(); 

// Call a procedure with one OUT parameter 
cs = connection.prepareCall("{call myprocout(?)}"); 

// Register the type of the OUT parameter 
cs.registerOutParameter(1, Types.VARCHAR); 

// Execute the stored procedure and retrieve the OUT value 
cs.execute(); 
String outParam = cs.getString(1); // OUT parameter 

// Call a procedure with one IN/OUT parameter 
cs = connection.prepareCall("{call myprocinout(?)}"); 

// Register the type of the IN/OUT parameter 
cs.registerOutParameter(1, Types.VARCHAR); 

// Set the value for the IN/OUT parameter 
cs.setString(1, "a string"); 

// Execute the stored procedure and retrieve the IN/OUT value 
cs.execute(); 
outParam = cs.getString(1); // OUT parameter 
} catch (SQLException e) { 
} 
CallableStatement cs; 
try { 
// Call a procedure with no parameters 
cs = connection.prepareCall("{call myproc}"); 
cs.execute(); 

// Call a procedure with one IN parameter 
cs = connection.prepareCall("{call myprocin(?)}"); 

// Set the value for the IN parameter 
cs.setString(1, "a string"); 

// Execute the stored procedure 
cs.execute(); 

// Call a procedure with one OUT parameter 
cs = connection.prepareCall("{call myprocout(?)}"); 

// Register the type of the OUT parameter 
cs.registerOutParameter(1, Types.VARCHAR); 

// Execute the stored procedure and retrieve the OUT value 
cs.execute(); 
String outParam = cs.getString(1); // OUT parameter 

// Call a procedure with one IN/OUT parameter 
cs = connection.prepareCall("{call myprocinout(?)}"); 

// Register the type of the IN/OUT parameter 
cs.registerOutParameter(1, Types.VARCHAR); 

// Set the value for the IN/OUT parameter 
cs.setString(1, "a string"); 

// Execute the stored procedure and retrieve the IN/OUT value 
cs.execute(); 
outParam = cs.getString(1); // OUT parameter 
} catch (SQLException e) { 
}
F
//Seria assim?

cs = cn.prepareCall("Consultar_Cliente_Excluir ?");
			cs.setInt(1, cod_Garantia);


                   cs.execute();
                   rst = cs.executeQuery();
                   System.out.println(rst);
ViniciusLM

Acho que seria assim

cs = cn.prepareCall("Consultar_Cliente_Excluir ?");   
            cs.setInt(1, cod_Garantia);   
  
  
                   rst = cs.executeQuery();
                   System.out.println(rst.getString(1));

Mas o que exatamente ele retorna?

Ajuda ae Pessoal :lol:
http://vwdarkside.com/en/jedi/vinicius-michelan-251769
Para eu me tornar um Jedi melhor rsrsrs :roll:

F

eu tava fazendo certo… é que eu modifiquei um negocio em outra classe e não lembrava que tinha mudado hihi
valeu!

Criado 28 de julho de 2011
Ultima resposta 28 de jul. de 2011
Respostas 5
Participantes 4