【问题标题】:display database table in java在java中显示数据库表
【发布时间】:2015-08-18 23:25:01
【问题描述】:

我正在尝试从我的数据库中获取一个表格。我遵循了一个教程,我的代码显示在下面,但是我收到了这个错误并且不知道为什么。在我的数据库中,我有“名字、姓氏、地址、城市、州、邮编”不确定是否需要这些信息来帮助我解决我的问题

有人可以帮忙

在此先感谢您的帮助。

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);
    });

    }
}

【问题讨论】:

  • 注意:C:\Users\student\Desktop\CapstoneTuesday_SD2799\MedicalRecords\src\medicalrecords\TableFromDatabase.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息。哎呀哈哈忘了那部分

标签: java database resultset


【解决方案1】:

您的消息“使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息”不是错误,而是警告。 Vector 类是泛型的(Java 1.5 中添加了泛型),这意味着它需要一个类型参数。在这里,它表示它可以容纳的对象的类型。

如果您按照警告提示使用命令行选项“-Xlint:unchecked”重新编译,编译器将为您提供有关警告的更多详细信息,包括违规行。

您没有为任何Vectors 提供类型参数,因此它们是原始,这意味着没有提供类型参数。编译器警告您正在使用原始类型,并且您的类型安全存在风险。

您可以提供适当的类型参数来消除警告。在TableFromDatabase 构造函数中,朝向顶部:

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

之后在同一个构造函数中:

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

但是,Vector 在 Java 1.2 中被 ArrayList(及其 List 接口)取代。事实上,Vector 被改装后实现了新的List 接口。看来你不需要Vector的线程安全,所以建议改用ArrayList

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

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

从 Java 1.7 开始,您可以使用“菱形运算符”,并删除赋值运算符右侧的类型参数,让 Java 推断出正确的类型,例如:

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

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

【讨论】:

    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2023-03-04
    • 2019-04-08
    相关资源
    最近更新 更多