[code] public ArrayList listarIntens(int numOrdem) throws SQLException {
String sql = “SELECT * FROM ordem_servico WHERE os_num_ordem = ?”;
PreparedStatement comandoSQL = (PreparedStatement) banco.getConexao().prepareStatement(sql);
comandoSQL.setInt(1, numOrdem);
ResultSet rs = comandoSQL.executeQuery();
ArrayList<ItensServico> listaItens = new ArrayList<ItensServico>();
ItensServico tmp;
while (rs.next()) {
tmp = new ItensServico();
tmp.setCod_servico(rs.getInt("is_cod_servico"));
tmp.setValorItem(rs.getFloat("is_valor_item"));
tmp.setNum_ordem(rs.getInt("is_num_ordem"));
listaItens.add(tmp);
}
return listaItens;
}[/code]
como modifico o comando acima para pegar um valor(que é o nome do serviço) ,cuja a FK,na tabela ordem_servico, é is_cod_servico ,em vez de colocar o comando “tmp.setCod_servico(rs.getInt(“is_cod_servico”));”
Como você projetou a tuas classes? Você criou algum tipo de relacionamento entre as tuas
classes? Ou seja Um Serviço não “precisa”’ conhecer a(s) OrdemServico a ele associada.
class Servico{
private String nome;
//outras propriedades
//outras ações;
}
class OrdemServico{
private List<Servico> servicos;
//outras propriedades
//outras ações;
}
Ai você pode usar a referência e atribuir o valor respectivo do ID, nome e etc do servico:
while(rs.next()){
//cria um objeto OrdemServico em cada iteração [ação implícita]
ordemServico.servicos.setNome(rs.getString("NOME_SERVICO"));
// e atribui posteriomente a lista.
}
Obs: Amigo vai uma dica, quando postares aqui e em outros foruns, seja específico em teu problema. E não apresente
como se fosse uma obrigação por parte dos participantes do grupo responderem…
Espero ter ajudado, se tiveres um outro ponto de vista, ou o meu entendimento estiver errado, vai postando para que outros
também possam apresentar as dicas.
existe sim relacionamento entre as classes ordem_servico, itens_servico e tipo_servico…
funciona assim: a tabela ‘itens_servico’ tem duas FK : cod_servico e num_ordem. Onde ‘cod_servico’ é a PK da tabela ‘tipo_servico’ e ‘num_ordem’ é a PK da tabela ‘ordem_servico’.
[quote=eiras][code] public ArrayList listarIntens(int numOrdem) throws SQLException {
String sql = “SELECT * FROM ordem_servico WHERE os_num_ordem = ?”;
PreparedStatement comandoSQL = (PreparedStatement) banco.getConexao().prepareStatement(sql);
comandoSQL.setInt(1, numOrdem);
ResultSet rs = comandoSQL.executeQuery();
ArrayList<ItensServico> listaItens = new ArrayList<ItensServico>();
ItensServico tmp;
while (rs.next()) {
tmp = new ItensServico();
tmp.setCod_servico(rs.getInt("is_cod_servico"));
tmp.setValorItem(rs.getFloat("is_valor_item"));
tmp.setNum_ordem(rs.getInt("is_num_ordem"));
listaItens.add(tmp);
}
return listaItens;
}[/code]
como modifico o comando acima para pegar um valor(que é o nome do serviço) ,cuja a FK,na tabela ordem_servico, é is_cod_servico ,em vez de colocar o comando “tmp.setCod_servico(rs.getInt(“is_cod_servico”));”
desde ja agradeço![/quote]
Então cara se precisa fazer um inner join… e não fazer dessa forma … veja :
select is_nome_servico, is_valor_item, is_num_ordem
from ordem_servico o inner join servico s using(is_cod_servico) where os_num_ordem=?;
dai então voce tem o nome do servico que esta na ordem_do servico… valeu se não der certa posta que nois resorve…hehe
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
at gui.frameTelaPrincipal.escondeColuna(frameTelaPrincipal.java:117)
at gui.frameTelaPrincipal.jlServeAbertaValueChanged(frameTelaPrincipal.java:1072)
at gui.frameTelaPrincipal.access$700(frameTelaPrincipal.java:46)
at gui.frameTelaPrincipal$11.valueChanged(frameTelaPrincipal.java:308)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:442)
at javax.swing.JList.setSelectionInterval(JList.java:2035)
at javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(BasicListUI.java:2731)
at javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(BasicListUI.java:2687)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.Component.processMouseEvent(Component.java:6260)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
at gui.frameTelaPrincipal.escondeColuna(frameTelaPrincipal.java:117)
at gui.frameTelaPrincipal.jlServeAbertaValueChanged(frameTelaPrincipal.java:1072)
at gui.frameTelaPrincipal.access$700(frameTelaPrincipal.java:46)
at gui.frameTelaPrincipal$11.valueChanged(frameTelaPrincipal.java:308)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:137)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:668)
at javax.swing.JList.setValueIsAdjusting(JList.java:2110)
at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2788)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
pelo que eu entendi vc esta passando como parâmetro o número da ordem, O que pode estar ocorrendo é que não esta sendo encontrado nenhum registro ligado a esse número. Talvez vc deve-se circundar isso ai com try/catch para que nenhuma outra classe utilize este método e acabe gerando este erro.
ficaria algo assim:
[code]# public ArrayList listarIntens(int numOrdem) throws SQLException {
}catch(SQLException e){
e.printStackTrace();
//Aqui vc coloca alguma mesagem em um JOptioPane dizendo que ouve um erro de sql
}catch(Exception e){
e.printStackTrace();
//Aqui vc coloca alguma mesagem em um JOptioPane dizendo que ouve um erro e a busca não foi possível
}
return listaItens;
} [/code]
E tb não sei se vc ja fez, mas seria bom fazer um método para efetuar a conexão com o banco e outra para desconectar, e chamá-las em cada método de listar
, criar, alterar , excluir etc…E sempre desconectar no final de cada um, pq assim se houver algum erro fica mais específico aonde esta ocorrendo.