gpt4 book ai didi

Java:如何从 JcomboBox 返回 JDBC 查询

转载 作者:行者123 更新时间:2023-11-30 07:43:30 24 4
gpt4 key购买 nike

package userProfile;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Database extends JFrame implements ActionListener {

JFrame frame;
JButton search, mainMenu;
JComboBox fromLoc, toLoc, fromDate, fromTime;
JLabel fromLabel, toLabel, fromDateLabel, fromTimeLabel;
PreparedStatement ps = null;
Connection link;
ResultSet rs = null;

public Database() {

frame = new JFrame("Make a Reservation");
frame.getContentPane().setLayout(null);

//Arrival date/time comboBoxes and labels
fromDateLabel = new JLabel("Departure Date");
fromDateLabel.setBounds(50,230,100,30);
fromDate = new JComboBox(new String[]{"12/15/2015",
"12/21/2015", "12/21/2015", "12/24/2015"});
fromDate.addActionListener(this);
fromDate.setBounds(50,200,100,30);
/*
fromTimeLabel = new JLabel("Departure Time");
fromTimeLabel.setBounds(160,230,100,30);
fromTime = new JComboBox(new String[]{"13:00","15:00", "15:30", "08:00"});
fromTime.addActionListener(this);
fromTime.setBounds(160,200,100,30);
*/
//Departure label and comboBox
fromLabel = new JLabel("Departure");
fromLabel.setBounds(50,300,100,30);
fromLoc = new JComboBox(new String[]{"Atlanta", "Charleston", "New York", "Los Angeles", "Orlando", "San Francisco"});
fromLoc.addActionListener(this);
fromLoc.setBounds(50,270,100,30);

toLabel = new JLabel("Arrival");
toLabel.setBounds(160,300,100,30);
toLoc = new JComboBox(new String[]{"Atlanta", "Charleston", "New York", "Los Angeles", "Orlando", "San Francisco"});
toLoc.addActionListener(this);
toLoc.setBounds(160,270,100,30);

search = new JButton("Ok");
search.addActionListener(this);
search.setBounds(270,270,100,30);

//adding the buttons in frame
frame.getContentPane().add(fromDateLabel);
frame.getContentPane().add(fromDate);
frame.getContentPane().add(fromTimeLabel);
frame.getContentPane().add(fromTime);
frame.getContentPane().add(fromLabel);
frame.getContentPane().add(fromLoc);
frame.getContentPane().add(toLabel);
frame.getContentPane().add(toLoc);
frame.getContentPane().add(search);

frame.setSize(400,400);
frame.setVisible(true);

try {
// Driver for mysql
Class.forName("com.mysql.jdbc.Driver");
// connection link obj
link = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");
// query statement obj
} catch (SQLException sqle) {
System.out.println("An error occurred.Maybe user/password is invalid");
sqle.printStackTrace();
} catch (ClassNotFoundException cfne) {
cfne.printStackTrace();
}
}

@Override
public void actionPerformed(ActionEvent e) {
JComboBox fromLoc = (JComboBox)e.getSource();
String fromL = fromLoc.getSelectedItem().toString();
JComboBox fromDate = (JComboBox)e.getSource();
String fromD = fromDate.getSelectedItem().toString();
JComboBox toLoc = (JComboBox)e.getSource();
String toL = toLoc.getSelectedItem().toString();

ArrayList<Flights> results = new ArrayList<>();

try {
ps = link.prepareStatement("select * from flights where from_date = ? and from_loc = ? and to_loc = ?");
ps.setString(1, fromD);
ps.setString(2, fromL);
ps.setString(3, toL);
rs = ps.executeQuery();

while(rs.next()) {

Flights flight = new Flights();
flight.setFromD(rs.getString("from_date"));
flight.setFromL(rs.getString("from_loc"));
flight.setToL(rs.getString("to_loc"));

results.add(flight);
}

} catch (SQLException sqle) {
System.out.println("An error occurred.Maybe user/password is invalid");
sqle.printStackTrace();
}


}

}

运行此程序时,我收到 NullPointerException。该程序应使用 3 个组合框中的数组中的硬编码字符串来查询数据库,并将返回的字符串作为对象保存到 ArrayList 中。请再次帮忙谢谢

最佳答案

我将创建一个 Flight 模型类并将其用作 DTO。所以,在你的 while(rs.next)我将使用结果来实例化 Flight 对象并将它们添加到 ArrayList<Flight>它可以返回到您需要使用它的任何地方。你的第三个问题我不太确定,因为我没有太多地使用 Swing。希望我的回答对您有帮助。如果我误解了你的问题,请告诉我。

编辑:

DTO 是用于包含和传输数据的数据传输对象。因此 Flight 类可能如下所示:

public class Flight{  
public String fromD;
public String fromL;
public String toL;...
//And so on
//You also need setters and getters
}

然后在您的数据库类中:

ArrayList<Flight> results = new ArrayList<>();
while(rs.next){
Flight flight = new Flight();
flight.setFromD(rs.getString("columnname"));
//Same method for the other variables you need to set.
results.add(flight);
}

关于Java:如何从 JcomboBox 返回 JDBC 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282887/

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