O pepino é o seguinte:
Criei um programa para acessar uma base Oracle, tipo um “Olá Mundo” só para testar a conexão, o código esta abaixo:
[list]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package testedb2;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
/**
*
-
@author malfatti
*/
public class Main {/**
-
@param args the command line arguments */ public static void main(String[] args) { String Query = “Select sysdate from dual”; OracleDataSource ODS = null; Connection conexao = null; Statement stmt = null; ResultSet rset = null;
System.out.println(“Programa Iniciado!”); try { ODS = new OracleDataSource(); } catch (SQLException ex) { System.out.println(“Problema Carregando Driver Oracle!”); } try { ODS.setDatabaseName(“orcl”); ODS.setDriverType(“thin”); ODS.setServiceName(“orcl”); ODS.setServerName(“server1”); ODS.setPortNumber(1521); ODS.setUser(“scott”); ODS.setPassword(“tiger”); conexao = ODS.getConnection(); } catch (SQLException ex) { System.out.println("Problema Conectando com o Oracle: " + ex.getMessage()); System.exit(0); } finally { System.out.println(“Conectado com o Oracle!”); } try { stmt = conexao.createStatement(); } catch (SQLException ex) { System.out.println(“Problema Criando Statement Oracle!”); System.exit(0); } System.out.println(“Executando Query!”); try { rset = stmt.executeQuery(Query); } catch (SQLException ex) { System.out.println(“Problema Executando Query!”); System.exit(0); } System.out.println(“Query Executada!”); try { while (rset.next()) { System.out.println(rset.getString(1)); QtdLinhas++; } } catch (SQLException ex) { System.out.println("Problema Fetching valores da Query: " + ex.getMessage()); System.exit(0); } try { conexao.close(); ODS.close(); } catch (SQLException ex) { System.out.println(“Problema Fechando Conexão!”); System.exit(0); } System.out.println(“Programa Concluido!”); } } [/list]
-
Então eu criei outro programa, usando o Swing e objetos gráficos. O código está abaixo:
[list]
/*
- TesteDB3View.java
*/
package testedb3;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
/**
-
The application’s main frame.
*/
public class TesteDB3View extends FrameView {public TesteDB3View(SingleFrameApplication app) {
super(app);initComponents();
}
@Action
/** This method is called from within the constructor to
-
initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is
-
always regenerated by the Form Editor. */ @SuppressWarnings(“unchecked”) // private void initComponents() {
mainPanel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton();
mainPanel.setName(“mainPanel”); // NOI18NjScrollPane1.setName(“jScrollPane1”); // NOI18NjScrollPane1.setViewportView(Texto);org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testedb3.TesteDB3App.class).getContext().getResourceMap(TesteDB3View.class); jButton1.setText(resourceMap.getString(“jButton1.text”)); // NOI18N jButton1.setName(“jButton1”); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });javax.swing.ActionMap actionMap =
org.jdesktop.application.Application.getInstance(testedb3.TesteDB3App.class).getContext().getActionMap(TesteDB3View.class, this);
jButton2.setAction(actionMap.get(“quit”)); // NOI18N
jButton2.setText(resourceMap.getString(“jButton2.text”)); // NOI18N
jButton2.setToolTipText(resourceMap.getString(“jButton2.toolTipText”)); // NOI18N
jButton2.setName(“jButton2”); // NOI18Njavax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout.setHorizontalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(mainPanelLayout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 254, Short.MAX_VALUE) .addComponent(jButton2))) .addContainerGap()) ); mainPanelLayout.setVerticalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE) .addContainerGap()) );
setComponent(mainPanel);
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String Query = “Select sysdate from dual”; OracleDataSource ODS = null; Connection conexao = null; Statement stmt = null; ResultSet rset = null;JOptionPane.showMessageDialog(null, "Programa Iniciado!", "Programa Iniciado!", JOptionPane.INFORMATION_MESSAGE); try { ODS = new OracleDataSource(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Carregando Driver Oracle!", "Erro!", JOptionPane.ERROR_MESSAGE); } try { ODS.setDatabaseName("orcl"); ODS.setDriverType("thin"); ODS.setServiceName("orcl"); ODS.setServerName("server1"); ODS.setPortNumber(1521); ODS.setUser("scott"); ODS.setPassword("tiger"); conexao = ODS.getConnection(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Conectando com o Oracle: " + ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE); System.exit(0); } finally { JOptionPane.showMessageDialog(null, "Conectado com o Oracle!", "Conectado com o Oracle!", JOptionPane.INFORMATION_MESSAGE); } try { stmt = conexao.createStatement(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Criando Statement Oracle!", "Erro!", JOptionPane.ERROR_MESSAGE); System.exit(0); } JOptionPane.showMessageDialog(null, "Executando Query!", "Executando Query!", JOptionPane.INFORMATION_MESSAGE); try { rset = stmt.executeQuery(Query); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Executando Query!", "Erro!", JOptionPane.ERROR_MESSAGE); System.exit(0); } JOptionPane.showMessageDialog(null, "Query Executada!", "Query Executada!", JOptionPane.INFORMATION_MESSAGE); try { while (rset.next()) { JOptionPane.showMessageDialog(null, rset.getString(1), "Resultados!", JOptionPane.INFORMATION_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Obtendo valores da Query: " + ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE); System.exit(0); } try { conexao.close(); ODS.close(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Problema Fechando Conexão!", "Erro!", JOptionPane.ERROR_MESSAGE); System.exit(0); } JOptionPane.showMessageDialog(null, "Programa Concluido!", "Programa Concluido!", JOptionPane.INFORMATION_MESSAGE);
}
// Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JPanel mainPanel; // End of variables declaration } [/list]
-
Na verdade somente fic um copiar e colar e troquei os System.out.println por JOptionPane.showMessageDialog para ficar mais “Windows”.
E aqui esta o problema: O primeiro programa funciona legal, inclusive testei com uma query mais parruda e tudo funcionou legal, os dados foram mostrados como deveriam. Mas o segundo não, ele cria o OracleDatSource e depois, quando vai executar o getConnection, ele fica uma semana pensando e depois da o erro “the network adapter could not establish the connection”.
Se alguém souber como resolver esta encrenca, desde já agradeço.
Marcos Malfatti.