佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: ericer

java program怎样联到mysql 的databases???

[复制链接]
发表于 4-10-2006 12:42 PM | 显示全部楼层
嗨!黑木头兄, 你好。我想我应该明白你在写什么 +

黑木头兄你果然厉害, 你写编码的方法很先进。在 for loop 那儿就看的出
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 6-10-2006 12:37 AM | 显示全部楼层
deleteAccountButton = new JButton("Delete Account");
deleteAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("DELETE FROM schedule WHERE flightScheduleNo = " + accountNumberList.getSelectedValue());
//errorText.append("Deleted " + i + " rows succesffullly");
//accountNumberList.removeAll();
//loadAccounts();
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
}
);


insertAccountButton = new JButton("Insert Account");
insertAccountButton.addActionListener(
new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("INSERT INTO schedule VALUES (" + accountIDText.getText() + ", " + "'" + basicPriceText.getText() + ", " + "'" + priceHText.getText() + ", " + "'" + priceLText.getText() + ", " + "'" + flightNoText.getText() + ", " + "'" + dateDepartText.getText() + ", " + "'" + placeDepartCodeText.getText() + ", " + "'" +
                           placeArriveCodeText.getText() + ", " + "'" + timeDepartText.getText() + "', " + "'" + timeArriveText.getText() + "', " + "0" + ", " + "now())");

errorText.append("Inserted " + i
+ " rows successfully");
accountNumberList.removeAll();
loadAccounts();
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
}
);


当我在run java gui后,click button 后,为什么会有
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Accounts.displaySQLErrors(Accounts.java:378)
        at Accounts.access$1200(Accounts.java:8)
        at Accounts$2.actionPerformed(Accounts.java:125)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
        at java.awt.Component.processMouseEvent(Component.java:5488)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
        at java.awt.Component.processEvent(Component.java:5253)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Component.dispatchEventImpl(Component.java:3955)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
        at java.awt.Container.dispatchEventImpl(Container.java:2010)
        at java.awt.Window.dispatchEventImpl(Window.java:1766)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
这些问题?
回复

使用道具 举报

发表于 11-10-2006 01:51 AM | 显示全部楼层
我也是遇到類似的問題..
星期五要教功課了..
我完了...

有沒有人可以幫我看看我的code到底除了甚麼問題??
pm我, 好嗎??
回复

使用道具 举报

发表于 12-10-2006 05:57 PM | 显示全部楼层
嗨!ericer 和 明天会更好 兄。我想你们一定是标点到 Null object 或者是你有一个变量 (variable) 但是你还没有分派它。你们的错误可能出现在 SQL 那儿。再贴你们的完整 Code 上来看看。希望我帮到你们 +
回复

使用道具 举报

发表于 12-10-2006 09:51 PM | 显示全部楼层
我写的GUI也出现NULLPOINTEREXCEPTION,
不过我没用到MYSQL..哪位高手帮帮忙..

ShedInterface.java
这是GUI界面里面有两个button,menu的部分还没写完,只是奇怪为何每次都要拉一拉frame才显示?


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;

  6. public class ShedInterface extends JFrame implements ActionListener
  7. {
  8.   private JButton addCoverBtn, addColdBtn;
  9.   private JMenuItem loadItem,saveItem, saveAsItem, exitItem;
  10.     AddCoverDialog addCoverD;
  11.        
  12.         public ShedInterface()
  13.         {
  14.            FrameWithMenu();       
  15.            addCoverBtn = new JButton("Add Covered Storage");
  16.            addColdBtn = new JButton("Add Cold Storage Button");
  17.           
  18.            JPanel p1 = new JPanel(new FlowLayout());
  19.            p1.add(addCoverBtn);
  20.            addCoverBtn.addActionListener(this);
  21.            p1.add(addColdBtn);
  22.            JPanel p2 = new JPanel(new GridLayout(0,1));
  23.            p2.add(p1);
  24.            getContentPane().add(p2,BorderLayout.CENTER);
  25.           
  26.         }   
  27.        
  28.         public void FrameWithMenu()
  29.         {
  30.                 setTitle("Shed Data Interface");
  31.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.                
  33.                 JMenuBar mb = new JMenuBar();
  34.                 setJMenuBar(mb);
  35.                
  36.                 JMenu fileMenu = new JMenu("File");
  37.                 mb.add(fileMenu);
  38.                 loadItem = new JMenuItem("Load");
  39.                 saveItem = new JMenuItem("Save");
  40.                 saveAsItem = new JMenuItem("Save As");
  41.                 exitItem = new JMenuItem("Exit");
  42.                
  43.                 fileMenu.add(loadItem);
  44.                 fileMenu.add(saveItem);
  45.                 fileMenu.add(saveAsItem);
  46.                 fileMenu.add(exitItem);
  47.                
  48.                 loadItem.addActionListener(this);
  49.                 saveItem.addActionListener(this);
  50.                 saveAsItem.addActionListener(this);
  51.                 exitItem.addActionListener(this);
  52.         }
  53.        
  54.         public void actionPerformed(ActionEvent ae)
  55.         {
  56.                   if(ae.getSource() == addCoverBtn)
  57.                   {
  58.                         addCoverD.setSize(200,130);
  59.                         //addCoverD.setLocationRelativeTo(this);
  60.                         addCoverD.setVisible(true);
  61.                   }
  62.              else if (ae.getSource() == exitItem)
  63.               {
  64.                       dispose();
  65.                      System.exit(0);
  66.               }
  67.         }
  68.           
  69.        
  70.         public static void main(String [] args)
  71.         {
  72.                 JFrame jf = new ShedInterface();
  73.                 jf.setVisible(true);
  74.                 //jf.setTitle("Shed Data Interface");
  75.             jf.setSize(400, 400);
  76.             jf.setLocation(100, 100);
  77.         }
  78. }
复制代码


AddCoverDialog.java
点击左边的BUTTON会出现个DIALOG


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;

  4. public class AddCoverDialog extends JDialog
  5. {
  6.         private JTextField heightTF;
  7.         private JButton submitBtn,cancelBtn;
  8.        
  9.         public AddCoverDialog(JFrame owner, String title, boolean modal)
  10.         {
  11.                 super(owner,title,modal);
  12.                 //setSize(300,200);
  13.                 setTitle("Add Covered Storage");
  14.                 getContentPane().setLayout(new BorderLayout());
  15.                
  16.                 JPanel inputPanel = new JPanel(new GridLayout(0,1,0,8));
  17.                 heightTF = new JTextField(15);
  18.                 inputPanel.add(new JLabel("Shed Height"));
  19.                 inputPanel.add(heightTF);
  20.                 getContentPane().add(inputPanel,BorderLayout.CENTER);
  21.                
  22.                 submitBtn = new JButton("Submit");
  23.                 cancelBtn = new JButton("Cancel");
  24.                 JPanel buttonPanel = new JPanel(new FlowLayout());
  25.                 buttonPanel.add(submitBtn);
  26.                 buttonPanel.add(cancelBtn);
  27.                
  28.                 JPanel finalPanel = new JPanel(new GridLayout(0,1));
  29.                 finalPanel.add(buttonPanel);
  30.                 getContentPane().add(finalPanel,BorderLayout.SOUTH);
  31.                
  32.                 submitBtn.addActionListener(new ActionListener()
  33.                 {
  34.                   //Anoynymous class
  35.                   public void actionPerformed(ActionEvent ae)
  36.                  {
  37.                         String shedHeight = heightTF.getText().trim();
  38.                         if(shedHeight.length() == 0)
  39.                     JOptionPane.showMessageDialog(null,"The field can                           not empty");
  40.                          else
  41.                            {
  42.                                     
  43.                             try
  44.                              {
  45.                               int height = Integer.parseInt(shedHeight);
  46.                               if(height != 3 && height != 5 )
  47.                               JOptionPane.showMessageDialog(null,"Please                           enter 3 or 5 only.");
  48.                                       clearText();
  49.                                       setVisible(true);
  50.                               }
  51.                               catch (NumberFormatException nfe)
  52.                               {
  53.                               JOptionPane.showMessageDialog(null,"Please                  enter numeric data.");
  54.                               clearText();
  55.                               }
  56.                           }
  57.                     }
  58.                 });
  59.                
  60.                 //Anoynymous class
  61.                 cancelBtn.addActionListener(new ActionListener()
  62.                 {
  63.                   public void actionPerformed(ActionEvent ae)
  64.                   {
  65.                    clearText();
  66.                   JOptionPane.showMessageDialog(null,"Addition aborted");
  67.                    setVisible(false);
  68.                                
  69.                    }
  70.                 });
  71.         }
  72.                
  73.         public void clearText()
  74.         {
  75.           heightTF.setText("");
  76.         }
  77.                
  78.         /*public static void main(String [] args)
  79.         {
  80.                 JFrame jf = new JFrame();
  81.                 AddCoverDialog c = new AddCoverDialog(jf,"Testing",true);
  82.                 c.setSize(220,130);
  83.                 c.setVisible(true);
  84.         }*/
  85.   
  86. }       
复制代码
回复

使用道具 举报

发表于 13-10-2006 01:35 AM | 显示全部楼层
因为你在显示window之后又改window的设定。把 jf.setVisible(true); 写在所有setting 后面就行了。
public static void main(String [] args)
        {
                JFrame jf = new ShedInterface();
                jf.setSize(400, 400);
                jf.setLocation(100, 100);
                jf.setVisible(true);

                //jf.setTitle("Shed Data Interface";
               
        }

你地问题是应为你的addCoverD declaration 没做好。
public void actionPerformed(ActionEvent ae)
        {
                  if(ae.getSource() == addCoverBtn)
                  {
                          AddCoverDialog addCoverD = new AddCoverDialog(this, "Title", true);
                          addCoverD.setSize(200,130);
                        //addCoverD.setLocationRelativeTo(this);
                        addCoverD.setVisible(true);
                  }
             else if (ae.getSource() == exitItem)
              {
                      dispose();
                     System.exit(0);
              }
        }
回复

使用道具 举报

Follow Us
发表于 13-10-2006 01:59 AM | 显示全部楼层
cari 卡帖。。。
回复

使用道具 举报

发表于 13-10-2006 11:39 AM | 显示全部楼层
原帖由 BC2207 于 13-10-2006 01:35 AM 发表
因为你在显示window之后又改window的设定。把 jf.setVisible(true); 写在所有setting 后面就行了。
public static void main(String [] args)
        {
                JFrame jf = new ShedInterface();
  ...



BC2207,你太棒啦....
回复

使用道具 举报


ADVERTISEMENT

发表于 13-10-2006 11:04 PM | 显示全部楼层
想再请教下,为何这个DIALOG又出现NULLPOINTEREXCEPTION??

AddColdDialog.java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;

  4. public class AddColdDialog extends JDialog
  5. {
  6.         private JTextField heightTF,maxTempTF,opTempTF;
  7.         private JButton submitBtn,cancelBtn;
  8.        
  9.         public AddColdDialog(JFrame owner, String title, boolean modal)
  10.         {
  11.                 super(owner,title,modal);
  12.                 //setSize(300,200);
  13.                 setTitle("Add Cold Storage");
  14.                 getContentPane().setLayout(new FlowLayout());
  15.                
  16.                 JPanel labelPanel = new JPanel(new GridLayout(0,1,0,8));
  17.                 labelPanel.add(new JLabel("Shed Height"));
  18.                 labelPanel.add(new JLabel("Maximum Teperature"));
  19.                 labelPanel.add(new JLabel("Operational Temperature"));
  20.                
  21.                 JPanel textFieldPanel = new JPanel(new GridLayout       (0,1,0,8));
  22.                 textFieldPanel.add(heightTF);
  23.                 textFieldPanel.add(maxTempTF);
  24.                 textFieldPanel.add(opTempTF);
  25.                
  26.                 JPanel inputPanel = new JPanel(new FlowLayout());
  27.                 inputPanel.add(labelPanel);
  28.                 inputPanel.add(textFieldPanel);
  29.                 getContentPane().add(inputPanel,BorderLayout.CENTER);
  30.                
  31.                 submitBtn = new JButton("Submit");
  32.                 cancelBtn = new JButton("Cancel");
  33.                 JPanel buttonPanel = new JPanel(new FlowLayout());
  34.                 buttonPanel.add(submitBtn);
  35.                 buttonPanel.add(cancelBtn);
  36.                
  37.                 JPanel finalPanel = new JPanel(new GridLayout(0,1));
  38.                 finalPanel.add(buttonPanel);
  39.                 getContentPane().add(finalPanel,BorderLayout.SOUTH);
  40.                
  41.                 submitBtn.addActionListener(new ActionListener()
  42.                 {
  43.                     //Anoynymous class
  44.                     public void actionPerformed(ActionEvent ae)
  45.                    {
  46.                         String shedHeight = heightTF.getText().trim();
  47.                         String maxTemp = maxTempTF.getText().trim();
  48.                         String opTemp = opTempTF.getText().trim();
  49.                                
  50.                         if(shedHeight.equals("") || maxTemp.equals("")
  51.                                     || opTemp.equals(""))
  52.                         JOptionPane.showMessageDialog(null,"Please enter all fields");
  53.                     else
  54.                         {
  55.                                           
  56.                          try
  57.                             {
  58.                               int height = Integer.parseInt(shedHeight);
  59.                               if(height != 3 && height!= 5)
  60.                         JOptionPane.showMessageDialog(null,"Please enter 3 or 5 only.");
  61.                         clearText();
  62.                         setVisible(true);
  63.                                                     
  64.                         double maxT = Double.parseDouble(maxTemp);
  65.                         double opT = Double.parseDouble(opTemp);
  66.                         if(opT > maxT)
  67.                     JOptionPane.showMessageDialog(null,"Operational temperature cannot higher than maximum temperature");
  68.                         clearText();
  69.                         setVisible(true);                             
  70.                   }
  71.                   catch(NumberFormatException nfe)
  72.                  {
  73.                 JOptionPane.showMessageDialog(null,"Please enter numeric data");
  74.                 clearText();
  75.                 }
  76.              }
  77.           }
  78.       });
  79.                  
  80.      //Anoynymous class
  81.     cancelBtn.addActionListener(new ActionListener()
  82.     {
  83.         public void actionPerformed(ActionEvent ae)
  84.         {
  85.           clearText();
  86.          JOptionPane.showMessageDialog(null,"Addition aborted");
  87.         setVisible(false);
  88.         }
  89.   });
  90.         }
  91.                
  92.         public void clearText()
  93.         {
  94.            heightTF.setText("");
  95.            maxTempTF.setText("");
  96.            opTempTF.setText("");
  97.         }
  98.        
  99.         /* For testing purposes
  100.          public static void main(String [] args)
  101.         {
  102.                 JFrame jf = new JFrame();
  103.                 AddColdDialog c = new AddColdDialog(jf,"Testing",true);
  104.                 c.setSize(200,130);
  105.                 c.setVisible(true);
  106.         }*/
  107.   }
复制代码


main interface里DIALOG的declaration是这样,第二个BUTTON有问题:


  1. public void actionPerformed(ActionEvent ae)
  2.         {
  3.        if(ae.getSource() == addCoverBtn)
  4.            {
  5.                 AddCoverDialog addCoverD = new AddCoverDialog(this, "Tit                            le", true);
  6.                addCoverD.setSize(230,130);
  7.                  addCoverD.setLocationRelativeTo(this);
  8.                  addCoverD.setVisible(true);
  9.                   }
  10.                   
  11.             else if (ae.getSource() == addColdBtn)
  12.               {
  13.                       AddColdDialog addColdD = new AddColdDialog(this, "Titl                             e", true);
  14.             addColdD.setSize(230,130);
  15.           addColdD.setLocationRelativeTo(this);
  16.           addColdD.setVisible(true);
  17.     }

  18.     public static void main(String [] args)
  19.         {
  20.                 JFrame jf = new ShedInterface();
  21.                 jf.setSize(400, 400);
  22.             jf.setLocation(100, 100);
  23.             jf.setVisible(true);
  24.         }
复制代码

回复

使用道具 举报

发表于 15-10-2006 12:57 AM | 显示全部楼层
你过讲了。我会是因为我以前也常犯同样的问题。
因为你的 heightTF,maxTempTF 和 opTempTF 申报(declare)了, 但没有给于(assign)任何的涵数(value).
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-9-2024 11:34 PM , Processed in 0.101164 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表