gpt4 book ai didi

Java JDBC Eclipse 使用 Oracle DB 填充 JTable

转载 作者:行者123 更新时间:2023-12-01 11:54:28 27 4
gpt4 key购买 nike

所以我正在努力创建一个功能正常的 GUI,它可以使用 JDBC 从数据库检索信息(在本例中我使用 Oracle 11g XE)执行诸如填充 JTable(使用 WindowBuilder 构建)之类的操作。

我的 Oracle 数据库服务器正在运行,我可以成功连接到它,但是当我尝试执行要传递到数据库的查询时,出现了问题。

这是我的 Connection 类,仅用于连接到数据库:

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

这有效(至少据我所知,这是因为系统提示我“连接成功!”消息)

现在这是我的 GUI 类(目前只有其他类):

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

它似乎在 pat = conn.prepareStatement(query);

上中断

我收到的错误是...

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)...

并继续出现更多类似错误的行..

我似乎无法找出它无法与数据库正确通信的任何原因。任何帮助将不胜感激伙计们!

最佳答案

但是您的 dbConnector() 方法始终返回 null。

也许您希望它这样做:

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

关于Java JDBC Eclipse 使用 Oracle DB 填充 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com