1

为什么我有这些错误?这是什么意思?我只想显示我的 SQL DB 上的所有数据。

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javaapplication25.SearchPatient.<init>(SearchPatient.java:26)
    at javaapplication25.SearchPatient$2.run(SearchPatient.java:146)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

这是我使用的代码,不知道如何修复它,因为我在编写 Java 应用程序方面不够好。, 有人能帮我一下吗?:

package javaapplication25;

/**
 *
 * @author cleanfuel
 */
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class SearchPatient extends javax.swing.JFrame {

    /**
     * Creates new form SearchPatient
     */
    public SearchPatient() throws SQLException {
        initComponents();

        Statement stmt = null;

        ResultSet rs = stmt.executeQuery("SELECT * FROM dbo.Patients");
        ResultSetMetaData rsmetadata = rs.getMetaData();

        int columns = rsmetadata.getColumnCount();

        DefaultTableModel dtm = new DefaultTableModel();
        Vector columns_vector = new Vector();
        Vector rows_vector = new Vector();

        for (int i=1; i< columns; i++){
        columns_vector.addElement(rsmetadata.getColumnName(i));
        }

        dtm.setColumnIdentifiers(columns_vector);

        while (rs.next()){

            rows_vector = new Vector();
            for (int j=1; j<columns; j++){

                rows_vector.addElement(rs.getString(j));
            }
            dtm.addRow(rows_vector);
        }
        tblPatients.setModel(dtm);

    }


    @SuppressWarnings("unchecked")


Connection connection;
    private void formWindowOpened(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        connection = Main.ConnectDatabase();
    }
4

1 回答 1

2

你有这两行,一个接一个

Statement stmt = null;

ResultSet rs = stmt.executeQuery("SELECT * FROM dbo.Patients");

你还没有初始化stmt对象,所以它null,没什么,natta

基本上,这就像在插入之前尝试打开电视一样,它不太可能工作

查看JDBC 数据访问以获取更多详细信息,但本质上,您需要建立与数据库的连接,然后使用该连接创建 Statement 对象,然后您可以查询它。

于 2013-04-06T02:12:15.557 回答