java - 带有 JDBC 连接的基本 Swing GUI,使用来自查询的信息填充标签
问题描述
目前,我创建了一个 Swing GUI 来从基本数据库中提取数据。
最终,我想把它构建成一个销售点系统,这就是开始。
一旦我掌握了“基础知识”,我将能够使用不同的选项卡和更多项目来扩展它。
现在查询能够提取数据并且我可以将代码打印到控制台,但是当我尝试将它实现到每个标签或文本框时,它只会显示在一个文本框上,而不是其他文本框上。
是否需要循环遍历我的每个标签,因为它们被标记为 label1,label2 ...?那么如何确保 SQL 查询加载到每个标签?在循环中,我是否对 productID 进行排序?
我尝试使用多个查询并得到一个结果集已关闭错误,并认为有一种更简单的方法来获取数据。
这是代码:
package application;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.JDesktopPane;
public class Application extends JFrame {
private JTextField prc2;
private JTextField prc1;
//private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Application frame = new Application();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Application() {
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JDesktopPane desktopPane = new JDesktopPane();
getContentPane().add(desktopPane);
prc2 = new JTextField();
prc2.setBounds(70, 111, 86, 20);
desktopPane.add(prc2);
prc2.setColumns(10);
prc1 = new JTextField();
prc1.setBounds(70, 80, 86, 20);
desktopPane.add(prc1);
prc1.setColumns(10);
JLabel item1 = new JLabel("New label");
item1.setBounds(10, 83, 46, 14);
desktopPane.add(item1);
JLabel item2 = new JLabel("New label");
item2.setBounds(10, 114, 46, 14);
desktopPane.add(item2);
JLabel qty1 = new JLabel("New label");
qty1.setBounds(181, 83, 46, 14);
desktopPane.add(qty1);
JLabel qty2 = new JLabel("New label");
qty2.setBounds(181, 114, 46, 14);
desktopPane.add(qty2);
JLabel qty = new JLabel("Quantity");
qty.setBounds(181, 40, 46, 14);
desktopPane.add(qty);
JLabel Price = new JLabel("PRICE");
Price.setBounds(70, 40, 46, 14);
desktopPane.add(Price);
Connection conn = null;
//Statement stmt = null;
//ObservableList<String> data = FXCollections.observableArrayList();
try {
String dbURL = "jdbc:sqlserver://localhost:1433;databaseName = Capstone; username=dbuser; password=password";
conn = DriverManager.getConnection(dbURL);
Statement stmt = conn.createStatement();
String sqlStmt = "SELECT * FROM dbo.products WHERE ProductID = 1";
ResultSet rs = stmt.executeQuery(sqlStmt);
while (rs.next()) {
int id = rs.getInt("ProductID");
String productName = rs.getString("productName");
String quant = rs.getString("productQty");
String price = rs.getString("ProductPrice");
item1.setText(productName);
qty1.setText(quant);
prc1.setText(price);
}
String sqlStmt1 = "SELECT * FROM dbo.products WHERE ProductID = 2";
ResultSet rs1 = stmt.executeQuery(sqlStmt1);
while (rs1.next()) {
int id1 = rs.getInt("ProductID");
String productName1 = rs.getString("productName");
String quant1 = rs.getString("productQty");
String price1 = rs.getString("ProductPrice");
item2.setText(productName1);
qty2.setText(quant1);
prc2.setText(price1);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
如果我错过了类似的问题,请指出正确的方向。
解决方案
推荐阅读
- sql - 后端是否应该在 SQL 查询之前执行一致性检查?
- javascript - 未找到电子菜单栏模块
- html - 即使完成链接href,将HTML链接到CSS也有问题,文件位于同一文件夹中
- spring - 具有 IBM MQ 连接刷新或失效的 Spring JMS
- android - 在 Android 中未收到带有 android plaid sdk 的成功回调
- python - 如何在同一类的第二个函数中引用变量?
- postman - 无法生成集合 提供的架构无效
- webpack - 为什么 react-script build 在 cmd 中失败,但在 npm 脚本中成功(npm run build)?
- java - 在 Tomcat 服务器上运行 Java Servlet 单元测试
- javascript - 猫鼬排序月份