Home » Developer & Programmer » JDeveloper, Java & XML » Strange Problem connecting Oracle (Windows XP, JRK/JDK 1.6.0_11, Netbeans 6,5)
Strange Problem connecting Oracle [message #383678] Thu, 29 January 2009 11:42 Go to next message
MarcosMalfatti
Messages: 2
Registered: January 2009
Location: Brasil
Junior Member
Hello & help!

A have a strange problem trying connect with Oracle inside my Java Program.
I create a small look like "Hello World" program to connect with Oracle and display SYSDATE. The code is below.

package testedb2;

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

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("Program Started!");
        try {
            ODS = new OracleDataSource();
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        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) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            System.out.println("Conectado com o Oracle!");
        }
        try {
            stmt = conexao.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Executing Query!");
        try {
            rset = stmt.executeQuery(Query);
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex));
        }
        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!");
    }
}


Then I make another program with Swing and another graphical objects and copy & paste tho code inside Button Action Performed Event. I use Netbeans to make this programs.
The code of new program is:
/*
 * 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    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"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jScrollPane1.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"); // NOI18N

        javax.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);
    }// </editor-fold>                       

    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, "Program Started!", "Program Started!", JOptionPane.INFORMATION_MESSAGE);
        try {
            ODS = new OracleDataSource();
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        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) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            JOptionPane.showMessageDialog(null, "Oracle Connected!", "Oracle Connected!", JOptionPane.INFORMATION_MESSAGE);
        }
        try {
            stmt = conexao.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Executing Query!", "Executing Query!", JOptionPane.INFORMATION_MESSAGE);
        try {
            rset = stmt.executeQuery(Query);
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Query Executed!", "Query Executed!", JOptionPane.INFORMATION_MESSAGE);
        try {
            while (rset.next()) {
                JOptionPane.showMessageDialog(null, rset.getString(1), "Results!", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conexao.close();
            ODS.close();
        } catch (SQLException ex) {
            Logger.getLogger(TesteDB3View.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "End Program!", "End Program!", 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                  
}


What's happen? First program working fine, but second program not, it frozen in getConnection.
The program stay frozen by 2 or 3 hours and show message "the network adapter could not establish the connection", but during this time I working first program, and it working fine, I access the Database with SQL Plus, and I use iReport to refresh reports accessing same database.
I use OJDBC14.jar, but a testing with OJDBC6.jar and same problem occur.

Thanks for any help!

Marcos Malfatti
Re: Strange Problem connecting Oracle (Solved!!) [message #384605 is a reply to message #383678] Wed, 04 February 2009 07:12 Go to previous messageGo to next message
MarcosMalfatti
Messages: 2
Registered: January 2009
Location: Brasil
Junior Member
I reinstall 5 times Windows to make several tests and I find a problem and resolution:
- I have a proxy server into my network and Java try connect with my database using yhis proxy server.
To solve this problem, I go in Control Panel -> Internet Options -> Connections -> LAN Settings -> Advanced and put fully qualified name of my database server into "Do not use proxy server for address beginning with".
And my program is working fine.

Thanks for all.

Marcos Malfatti.
Re: Strange Problem connecting Oracle (Solved!!) [message #384992 is a reply to message #384605] Fri, 06 February 2009 00:30 Go to previous message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Thanks for taking the time to explain what caused this!
Previous Topic: export xml from table data (merged)
Next Topic: Strange issue connecting to host web site
Goto Forum:
  


Current Time: Tue Dec 17 23:10:37 CST 2024