|
java program怎样联到mysql 的databases???
[复制链接]
|
|
发表于 4-10-2006 12:42 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 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才显示?
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class ShedInterface extends JFrame implements ActionListener
- {
- private JButton addCoverBtn, addColdBtn;
- private JMenuItem loadItem,saveItem, saveAsItem, exitItem;
- AddCoverDialog addCoverD;
-
- public ShedInterface()
- {
- FrameWithMenu();
- addCoverBtn = new JButton("Add Covered Storage");
- addColdBtn = new JButton("Add Cold Storage Button");
-
- JPanel p1 = new JPanel(new FlowLayout());
- p1.add(addCoverBtn);
- addCoverBtn.addActionListener(this);
- p1.add(addColdBtn);
- JPanel p2 = new JPanel(new GridLayout(0,1));
- p2.add(p1);
- getContentPane().add(p2,BorderLayout.CENTER);
-
- }
-
- public void FrameWithMenu()
- {
- setTitle("Shed Data Interface");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- JMenuBar mb = new JMenuBar();
- setJMenuBar(mb);
-
- JMenu fileMenu = new JMenu("File");
- mb.add(fileMenu);
- loadItem = new JMenuItem("Load");
- saveItem = new JMenuItem("Save");
- saveAsItem = new JMenuItem("Save As");
- exitItem = new JMenuItem("Exit");
-
- fileMenu.add(loadItem);
- fileMenu.add(saveItem);
- fileMenu.add(saveAsItem);
- fileMenu.add(exitItem);
-
- loadItem.addActionListener(this);
- saveItem.addActionListener(this);
- saveAsItem.addActionListener(this);
- exitItem.addActionListener(this);
- }
-
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource() == addCoverBtn)
- {
- addCoverD.setSize(200,130);
- //addCoverD.setLocationRelativeTo(this);
- addCoverD.setVisible(true);
- }
- else if (ae.getSource() == exitItem)
- {
- dispose();
- System.exit(0);
- }
- }
-
-
- public static void main(String [] args)
- {
- JFrame jf = new ShedInterface();
- jf.setVisible(true);
- //jf.setTitle("Shed Data Interface");
- jf.setSize(400, 400);
- jf.setLocation(100, 100);
- }
- }
复制代码
AddCoverDialog.java
点击左边的BUTTON会出现个DIALOG
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class AddCoverDialog extends JDialog
- {
- private JTextField heightTF;
- private JButton submitBtn,cancelBtn;
-
- public AddCoverDialog(JFrame owner, String title, boolean modal)
- {
- super(owner,title,modal);
- //setSize(300,200);
- setTitle("Add Covered Storage");
- getContentPane().setLayout(new BorderLayout());
-
- JPanel inputPanel = new JPanel(new GridLayout(0,1,0,8));
- heightTF = new JTextField(15);
- inputPanel.add(new JLabel("Shed Height"));
- inputPanel.add(heightTF);
- getContentPane().add(inputPanel,BorderLayout.CENTER);
-
- submitBtn = new JButton("Submit");
- cancelBtn = new JButton("Cancel");
- JPanel buttonPanel = new JPanel(new FlowLayout());
- buttonPanel.add(submitBtn);
- buttonPanel.add(cancelBtn);
-
- JPanel finalPanel = new JPanel(new GridLayout(0,1));
- finalPanel.add(buttonPanel);
- getContentPane().add(finalPanel,BorderLayout.SOUTH);
-
- submitBtn.addActionListener(new ActionListener()
- {
- //Anoynymous class
- public void actionPerformed(ActionEvent ae)
- {
- String shedHeight = heightTF.getText().trim();
- if(shedHeight.length() == 0)
- JOptionPane.showMessageDialog(null,"The field can not empty");
- else
- {
-
- try
- {
- int height = Integer.parseInt(shedHeight);
- if(height != 3 && height != 5 )
- JOptionPane.showMessageDialog(null,"Please enter 3 or 5 only.");
- clearText();
- setVisible(true);
- }
- catch (NumberFormatException nfe)
- {
- JOptionPane.showMessageDialog(null,"Please enter numeric data.");
- clearText();
- }
- }
- }
- });
-
- //Anoynymous class
- cancelBtn.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent ae)
- {
- clearText();
- JOptionPane.showMessageDialog(null,"Addition aborted");
- setVisible(false);
-
- }
- });
- }
-
- public void clearText()
- {
- heightTF.setText("");
- }
-
- /*public static void main(String [] args)
- {
- JFrame jf = new JFrame();
- AddCoverDialog c = new AddCoverDialog(jf,"Testing",true);
- c.setSize(220,130);
- c.setVisible(true);
- }*/
-
- }
复制代码 |
|
|
|
|
|
|
|
发表于 13-10-2006 01:35 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 13-10-2006 01:59 AM
|
显示全部楼层
cari 卡帖。。。 |
|
|
|
|
|
|
|
发表于 13-10-2006 11:39 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 13-10-2006 11:04 PM
|
显示全部楼层
想再请教下,为何这个DIALOG又出现NULLPOINTEREXCEPTION??
AddColdDialog.java
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class AddColdDialog extends JDialog
- {
- private JTextField heightTF,maxTempTF,opTempTF;
- private JButton submitBtn,cancelBtn;
-
- public AddColdDialog(JFrame owner, String title, boolean modal)
- {
- super(owner,title,modal);
- //setSize(300,200);
- setTitle("Add Cold Storage");
- getContentPane().setLayout(new FlowLayout());
-
- JPanel labelPanel = new JPanel(new GridLayout(0,1,0,8));
- labelPanel.add(new JLabel("Shed Height"));
- labelPanel.add(new JLabel("Maximum Teperature"));
- labelPanel.add(new JLabel("Operational Temperature"));
-
- JPanel textFieldPanel = new JPanel(new GridLayout (0,1,0,8));
- textFieldPanel.add(heightTF);
- textFieldPanel.add(maxTempTF);
- textFieldPanel.add(opTempTF);
-
- JPanel inputPanel = new JPanel(new FlowLayout());
- inputPanel.add(labelPanel);
- inputPanel.add(textFieldPanel);
- getContentPane().add(inputPanel,BorderLayout.CENTER);
-
- submitBtn = new JButton("Submit");
- cancelBtn = new JButton("Cancel");
- JPanel buttonPanel = new JPanel(new FlowLayout());
- buttonPanel.add(submitBtn);
- buttonPanel.add(cancelBtn);
-
- JPanel finalPanel = new JPanel(new GridLayout(0,1));
- finalPanel.add(buttonPanel);
- getContentPane().add(finalPanel,BorderLayout.SOUTH);
-
- submitBtn.addActionListener(new ActionListener()
- {
- //Anoynymous class
- public void actionPerformed(ActionEvent ae)
- {
- String shedHeight = heightTF.getText().trim();
- String maxTemp = maxTempTF.getText().trim();
- String opTemp = opTempTF.getText().trim();
-
- if(shedHeight.equals("") || maxTemp.equals("")
- || opTemp.equals(""))
- JOptionPane.showMessageDialog(null,"Please enter all fields");
- else
- {
-
- try
- {
- int height = Integer.parseInt(shedHeight);
- if(height != 3 && height!= 5)
- JOptionPane.showMessageDialog(null,"Please enter 3 or 5 only.");
- clearText();
- setVisible(true);
-
- double maxT = Double.parseDouble(maxTemp);
- double opT = Double.parseDouble(opTemp);
- if(opT > maxT)
- JOptionPane.showMessageDialog(null,"Operational temperature cannot higher than maximum temperature");
- clearText();
- setVisible(true);
- }
- catch(NumberFormatException nfe)
- {
- JOptionPane.showMessageDialog(null,"Please enter numeric data");
- clearText();
- }
- }
- }
- });
-
- //Anoynymous class
- cancelBtn.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent ae)
- {
- clearText();
- JOptionPane.showMessageDialog(null,"Addition aborted");
- setVisible(false);
- }
- });
- }
-
- public void clearText()
- {
- heightTF.setText("");
- maxTempTF.setText("");
- opTempTF.setText("");
- }
-
- /* For testing purposes
- public static void main(String [] args)
- {
- JFrame jf = new JFrame();
- AddColdDialog c = new AddColdDialog(jf,"Testing",true);
- c.setSize(200,130);
- c.setVisible(true);
- }*/
- }
复制代码
main interface里DIALOG的declaration是这样,第二个BUTTON有问题:
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource() == addCoverBtn)
- {
- AddCoverDialog addCoverD = new AddCoverDialog(this, "Tit le", true);
- addCoverD.setSize(230,130);
- addCoverD.setLocationRelativeTo(this);
- addCoverD.setVisible(true);
- }
-
- else if (ae.getSource() == addColdBtn)
- {
- AddColdDialog addColdD = new AddColdDialog(this, "Titl e", true);
- addColdD.setSize(230,130);
- addColdD.setLocationRelativeTo(this);
- addColdD.setVisible(true);
- }
- public static void main(String [] args)
- {
- JFrame jf = new ShedInterface();
- jf.setSize(400, 400);
- jf.setLocation(100, 100);
- jf.setVisible(true);
- }
复制代码
|
|
|
|
|
|
|
|
发表于 15-10-2006 12:57 AM
|
显示全部楼层
你过讲了。我会是因为我以前也常犯同样的问题。
因为你的 heightTF,maxTempTF 和 opTempTF 申报(declare)了, 但没有给于(assign)任何的涵数(value). |
|
|
|
|
|
|
| |
本周最热论坛帖子
|