我正在尝试将数据库java文件中的值传递给我的frame java文件.BY创建一个类似“frame java。get value(name,phone,date)”的函数,并在frame java文件中接收值。我试图在控制台中打印传递的值它工作正常,但是当我尝试将值设置为文本字段时,它不显示文本字段中的文本...我不是什么错可以任何人帮助我解决这个问题。
这是数据库java函数
public void check_room(String roomno, String date) throws SQLException {
String sql = "select * from customerinfo where roomno='" + roomno
+ "'and cdate='" + date + "' ";
System.out.print("search method called \n ");
System.out.print("\n ");
try {
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "hotel", "hotel");
} catch (SQLException ex) {
// Logger.getLogger(checkout.class.getName()).log(Level.SEVERE,
// null, ex);
}
sate = con.createStatement();
rs = sate.executeQuery(sql);
int tmp = 0;
try {
while (rs.next()) {
System.out.print("search found \n ");
String name = rs.getString("GUEST_NAME");
String phono = rs.getString("GUEST_PHO");
String addr = rs.getString("G_ADDR");
String paid = rs.getString("PAID");
String total = rs.getString("TOTAL");
String balance = rs.getString("BALANCE");
System.out.println(name);
mainmenu menu = new mainmenu();
menu.getvalue(name, phono, addr, paid, total, balance);
tmp++;
}
} catch (SQLException ex) {
// Logger.getLogger(checkout.class.getName()).log(Level.SEVERE,
// null, ex);
}
if (tmp <= 0) {
JOptionPane.showMessageDialog(null, "no details found ");
}
}
=============================================== ===========================
这是我的框架java文件
public void getvalue(String name, String phono, String addr, String paid,String total, String balance) {
nam.setText(name);
pho.setText(phono);
// mainmenu();
我编辑了你说但收到错误。我附上了我的骨架代码 我得到的错误,在
之下已修改的代码。
数据库文件
public class OracelThinconnection
{
private MainMenu menu;
public OracelThinconnection(MainMenu menu)
{
this.menu=menu;
..................
.................
.............
=============================================== ========================= 我遇到错误
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: constructor OracelThinconnection()
location: class bookingapp.OracelThinconnection
at bookingapp.MainMenu.jButton1ActionPerformed(MainMenu.java:1438)
at bookingapp.MainMenu.access$300(MainMenu.java:33)
at bookingapp.MainMenu$4.actionPerformed(MainMenu.java:379)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:1)
我不了解您未展示的代码,但您对问题的猜测是,您正在创建GUI的新实例,一个未可视化的实例,并尝试发送它的信息,而可视化的GUI没有受到干扰和未更新。
关键是您要创建主菜单GUI的 新 实例,该实例与显示的实例无关。将数据传递给此新实例不会对显示的GUI产生影响。
下面:
mainmenu menu= new mainmenu();
menu. getvalue (name, phono, addr, paid, total, balance);
您正在创建一个新的mainmenu对象(该类应命名为MainMenu)。
一个解决方案,给你的Database类一个MainMenu字段,并在这个字段中传入一个对main gui的有效引用,然后在GUI上调用方法,但只在Swing事件线程上调用。例如:
public class MyDataBase {
private MyGui gui;
// constructor gets passed a reference to the valid displayed GUI
public MyDataBase(MyGui gui) {
this.gui = gui;
}
public void someDataBaseMethod() {
// get database information
// now you can call methods on the actual displayed GUI
// but on the Swing event thread only. The SwingUtilities.invokeLater
// with the Runnable is required to be sure that you don't mess up
// Swing's threading model.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// this method below is being called on the
// displayed MyGui instance.
gui.updateGuiWithInformation(....); // some update method of the GUI
}
});
}
密钥 - 不在数据库代码中创建新的GUI实例。
好的,您已经更改了代码,现在有一个看起来像这样的构造函数:
public OracelThinconnection(MainMenu menu) {
// ....
}
现在您需要查看错误指向的行,MainMenu.java:1438
,MainMenu类的第1438行(1438 ????)。在该行上,您可能会调用OracelThinconnection构造函数,但不会将当前的MainMenu实例传递给它。新构造函数将接受MainMenu引用,但是当您调用构造函数时,您现在必须记住 将 中的引用传递给构造函数。
具体来说,从
更改它// not sure what you name the variable below
// but I just gave it thinConnection for now.
OracelThinconnection thinConnection = new OracelThinconnection();
到此:
OracelThinconnection thinConnection = new OracelThinconnection(this);
或者如果这不起作用,那么
OracelThinconnection thinConnection = new OracelThinconnection(MainMenu.this);
作为附录,你的代码显然有超过1400行,告诉你,你有一些巨大的上帝级别,试图做任何事情和一切。我强烈建议你重构这些代码,制作较小的类,每个类都有自己的职责范围,否则你将无法调试或增强这个程序。