0

so I am working to create a functioning GUI that can retrieve info from a database (in this case I'm using Oracle 11g XE) using JDBC to do things such as populate a JTable (built using WindowBuilder).

My Oracle DB Server is running and I can successfully connect to it, however when I try and execute a query to be passed to the DB something goes wrong.

Here is my Connection class, just used to connect to the DB:

import java.sql.*;
import javax.swing.*;

public class OracleConnection {
  static Connection con = null;

  public static Connection dbConnector() {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      con = DriverManager.getConnection(
          "jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
      JOptionPane.showMessageDialog(null, "Connection Successful!");
    } catch (Exception x) {
      JOptionPane.showMessageDialog(null, "Connection Unsuccessful.");
    }
    return null;
  }

  public static void main(String[] args) {
    dbConnector();
  }
}

This works (at least to my knowledge it is because I am prompted with a "Connection Successful!" message)

Now here my GUI class (only other class at the moment):

import java.awt.EventQueue;
import java.sql.*;
import javax.swing.*;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import net.proteanit.sql.DbUtils;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Management {

  private JFrame frame;
  private JTable tableDownloads;
  private JPanel panelMenu;
  private JPanel panelDown;
  private JPanel panelUp;
  private JPanel panelUtility;
  private JTable tableUploads;
  private JTable tableUtilities;
  ResultSet rs = null;
  PreparedStatement pat = null;
  Connection conn = null;


  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Management window = new Management();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */

  public Management() {
    conn = OracleConnection.dbConnector();
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {

    //CREATES ALL FRAMES / PANELS / BUTTONS / SCROLLPANES / LABELS
    //I CUT A LOT OF UN-NEEDED STUFF OUT TO SAVE ROOM

    //CREATING FRAMES
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new CardLayout(0, 0));


    //CREATING PANELS
    final JPanel panelMenu = new JPanel();
    frame.getContentPane().add(panelMenu, "name_13312291634045");
    panelMenu.setLayout(null);

    final JPanel panelDown = new JPanel();
    frame.getContentPane().add(panelDown, "name_13314999633769");
    panelDown.setLayout(null);
    panelDown.setVisible(false);


    //CREATING TABLES
    tableDownloads = new JTable();
    scrollPaneDownloads.setViewportView(tableDownloads);


    //CREATING BUTTONS
    JButton btnDownloads = new JButton("Downloads");
    btnDownloads.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        panelMenu.setVisible(false);
        panelDown.setVisible(true);
        generateDownloads();
      }
    });
    btnDownloads.setFont(new Font("Cambria", Font.BOLD, 14));
    btnDownloads.setBounds(32, 104, 122, 39);
    panelMenu.add(btnDownloads);

    JButton btnBackDown = new JButton("Back to Menu");
    btnBackDown.setFont(new Font("Cambria", Font.PLAIN, 13));
    btnBackDown.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelDown.setVisible(false);
        panelMenu.setVisible(true);
      }
    });
    btnBackDown.setBounds(323, 239, 109, 23);
    panelDown.add(btnBackDown);


    //CREATING LABELS
    JLabel lblDownloaduploadAndUtility = new JLabel("Download/Upload and Utility Manager");
    lblDownloaduploadAndUtility.setFont(new Font("Cambria", Font.BOLD, 20));
    lblDownloaduploadAndUtility.setBounds(48, 11, 370, 39);
    panelMenu.add(lblDownloaduploadAndUtility);

    JLabel lblDownloadsTable = new JLabel("Downloads Table");
    lblDownloadsTable.setFont(new Font("Cambria", Font.BOLD, 24));
    lblDownloadsTable.setBounds(122, 0, 196, 29);
    panelDown.add(lblDownloadsTable);
  }


  //METHOD TO GENERATE DOWNLOADS TABLE ******NOT WORKING******

  private void generateDownloads() {
    try {
      String query = "SELECT * FROM DOWNLOADS";
      pat = conn.prepareStatement(query);     //******** FAILS HERE ********
      rs = pat.executeQuery();
      tableDownloads.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}  

It seems to break on pat = conn.prepareStatement(query);

The error I am getting is...

at Management.generateDownloads(Management.java:207) at Management.access$1(Management.java:204) at Management$2.actionPerformed(Management.java:115) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)...

and continues on for many more lines of similar errors..

I can't seem to figure out any reason why it is not properly communicating with the DB. Any help would be GREATLY appreciated guys!

1 Answer 1

1

But your dbConnector() method always returns null.

Perhaps you want it to do this instead:

public static Connection dbConnector() {
    if (con == null) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
            JOptionPane.showMessageDialog(null, "Connection Successful!");
        } catch (Exception x) {
            JOptionPane.showMessageDialog(null, "Connection Unsuccessful.");
        }
    }
    return con;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I cannot believe it haha.. Spent hours before posting here looking at all sorts of things, can't believe it was this simple of a fix! Thanks a ton gknicker!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.