0

I am trying to get a table to populate from my database. I followed a tutorial and my code is displayed below, however I am getting this error and cannot figure out why. In my database I have 'first name, last name, address, city, state, zip' not sure if this info is needed to help me with my question

Could someone please help

thank you in advanced for your help.

package medicalrecords;

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

public class TableFromDatabase extends JPanel {
private Connection conexao = null;

public TableFromDatabase() {
Vector columnNames = new Vector();
Vector data = new Vector();

try {
        //  Connect to an Access Database
        conexao = DriverManager.getConnection("jdbc:derby://" + "localhost"
                + ":1527/Medical Records", "root", "password");



        //  Read data from a table
        String sql = "select * from SD2799.PATIENTRECORDS";
        try (Statement stmt = conexao.createStatement(); 
        ResultSet rs = stmt.executeQuery(sql)) {
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }

            //  Get row data
            while (rs.next()) {
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rs.getObject(i));
                }

                data.addElement(row);
            }

        }
        conexao.close();
    } catch (Exception e) {
        System.out.println(e);
    }

    //  Create table with database data
    JTable table = new JTable(data, columnNames) {
        @Override
        public Class getColumnClass(int column) {
            for (int row = 0; row < getRowCount(); row++) {
                Object o = getValueAt(row, column);

                if (o != null) {
                    return o.getClass();
                }
            }

            return Object.class;
        }
    };

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);

    JPanel buttonPanel = new JPanel();
    add(buttonPanel, BorderLayout.SOUTH);
}

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("Patient Records");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableFromDatabase newContentPane = new TableFromDatabase();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    });

    }
}
1
  • Note: C:\Users\student\Desktop\CapstoneTuesday_SD2799\MedicalRecords\src\medicalrecords\TableFromDatabase.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Opps lol forgot that part Commented Aug 18, 2015 at 23:27

1 Answer 1

2

Your message "uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details" is not an error, but it is a warning. The Vector class is generic (Generics were added to Java in 1.5), which means that it takes a type parameter. Here, it means the type of objects it can hold.

If you were to re-compile with the command line option "-Xlint:unchecked" as the warning suggests, the compiler will give you more details about the warning, including the offending lines.

You didn't supply a type parameter for any of your Vectors, so they are raw, meaning no type parameter was supplied. The compiler is warning you that you are using raw types, and that your type safety is at risk.

You can supply the appropriate type parameters to eliminate the warnings. In the TableFromDatabase constructor, towards the top:

Vector<String> columnNames = new Vector<String>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();

and later on in the same constructor:

Vector<Object> row = new Vector<Object>(columns);

However, Vector was superseded by ArrayList (and its List interface) in Java 1.2. In fact Vector was retrofitted then to implement the new-then List interface. It doesn't look like you need Vector's thread safety, so it's recommended to use ArrayList instead.

List<String> columnNames = new ArrayList<String>();
List<Vector<Object>> data = new ArrayList<List<Object>>();

and

List<Object> row = new ArrayList<Object>(columns);

As of Java 1.7, you can use the "diamond operator", and remove the type parameter on the right side of the assignment operator, letting Java infer the proper type, e.g.:

List<String> columnNames = new ArrayList<>();
List<Vector<Object>> data = new ArrayList<>();

and

List<Object> row = new ArrayList<>(columns);
Sign up to request clarification or add additional context in comments.

Comments

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.