小博博博博博1 小博博博博博1
关注数: 23 粉丝数: 705 发帖数: 6,180 关注贴吧数: 9
求助大神!!!! 我想做一个关于中超的系统,现在在只完成了两部分,一个是系统的主界面,另一个是添加球队信息的界面,我想通过点击主界面上的菜单选项,执行添加球队信息的界面。两部分单独运行可以实现,但是在主界面点击添加球队之后,却无法将球队信息添加到数据库中。程序贴在下面。 主界面程序: package 中超联赛联赛积分管理系统; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.swing.*; import javax.swing.event.MenuEvent; import javax.swing.event.MenuListener; public class MainFrame extends JFrame implements ActionListener { static Connection con; static PreparedStatement sql; public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/db_team", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return con; } private JPanel p; private JLabel lbl; private JMenuBar menubar; private JMenu menuteam, menuplayer, menuintegral; private JMenuItem miTquire, miTadd, miTedit, miTdelete, miPquire, miPadd, miPedit, miPdelete, miIquire, miIadd, miIedit, miIdelete; public MainFrame() { super("中超联赛联赛积分管理系统"); p = new JPanel(); Icon icon = new ImageIcon("C:/Users/lenovo/Desktop/毕业设计/中超标志.jpg"); lbl = new JLabel(icon); lbl.setBounds(0, 0, this.getWidth(), this.getHeight()); p.add(lbl); menubar = new JMenuBar(); this.setJMenuBar(menubar); menuteam = new JMenu("球队管理"); menuplayer = new JMenu("球员管理"); menuintegral = new JMenu("积分管理"); menubar.add(menuteam); menubar.add(menuplayer); menubar.add(menuintegral); miPquire = new JMenuItem("查询球员"); miPadd = new JMenuItem("添加球员"); miPedit = new JMenuItem("修改球员"); miPdelete = new JMenuItem("删除球员"); miTquire = new JMenuItem("查询球队"); miTadd = new JMenuItem("添加球队"); miTedit = new JMenuItem("修改球队"); miTdelete = new JMenuItem("删除球队"); miIquire = new JMenuItem("查询积分"); miIadd = new JMenuItem("录入积分"); miIedit = new JMenuItem("修改积分"); miIdelete = new JMenuItem("删除积分"); menuteam.add(miTquire); menuteam.add(miTadd); menuteam.add(miTedit); menuteam.add(miTdelete); menuplayer.add(miPquire); menuplayer.add(miPadd); menuplayer.add(miPedit); menuplayer.add(miPdelete); menuintegral.add(miIquire); menuintegral.add(miIadd); menuintegral.add(miIedit); menuintegral.add(miIdelete); miTquire.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 Tquire tqu = new Tquire(); } }); miTadd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 Tadd tad = new Tadd(); tad.getConnection(); } }); this.add(p); this.setSize(810, 560); this.setLocation(100, 50); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); } public static void main(String[] args) { new MainFrame(); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 Object source = e.getSource(); if (source == miTquire) { new Tquire(); } if (source == miTadd) { Tadd tad = new Tadd(); tad.getConnection(); } } } 添加球队信息界面代码: package 中超联赛联赛积分管理系统; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.swing.*; public class Tadd extends JFrame implements ActionListener { static Connection con; static PreparedStatement sql; public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/db_team", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return con; } private JPanel p; private JLabel lblTname, lblTcoach, lblTlocation; private JTextField txtTname, txtTcoach, txtTlocation; private JButton btnok, btncancl; public Tadd() { super("球队信息添加"); p = new JPanel(); lblTname = new JLabel("球队名称"); lblTcoach = new JLabel("主教练"); lblTlocation = new JLabel("主场位置"); txtTname = new JTextField(20); txtTcoach = new JTextField(20); txtTlocation = new JTextField(20); btnok = new JButton("添加"); btncancl = new JButton("取消"); p.setLayout(null); lblTname.setBounds(90, 20, 100, 25); lblTcoach.setBounds(90, 70, 100, 25); lblTlocation.setBounds(90, 120, 100, 25); txtTname.setBounds(200, 20, 100, 25); txtTcoach.setBounds(200, 70, 100, 25); txtTlocation.setBounds(200, 120, 100, 25); btnok.setBounds(90, 200, 60, 25); btncancl.setBounds(200, 200, 60, 25); p.add(lblTname); p.add(txtTname); p.add(lblTcoach); p.add(txtTcoach); p.add(lblTlocation); p.add(txtTlocation); p.add(btnok); p.add(btncancl); btnok.addActionListener(this); btncancl.addActionListener(this); this.add(p); this.setSize(400, 300); this.setLocation(100, 100); this.setResizable(false); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { Tadd ta = new Tadd(); ta.getConnection(); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if (e.getSource() == btnok) { String strTname = txtTname.getText(); String strTcoach = txtTcoach.getText(); String strTlocation = txtTlocation.getText(); if (strTname.equals("")) { JOptionPane.showMessageDialog(btnok, "球队名不能为空!"); return; } if (strTcoach.equals("")) { JOptionPane.showMessageDialog(btnok, "教练姓名不能为空!"); return; } if (strTlocation.equals("")) { JOptionPane.showMessageDialog(btnok, "主场位置不能为空!"); return; } try { sql = con.prepareStatement("INSERT INTO tb_team(Tname,Tcoach,Tlocation) " + "values(?,?,?)"); sql.setString(1, strTname); sql.setString(2, strTcoach); sql.setString(3, strTlocation); sql.executeUpdate(); } catch (Exception ex) { ex.printStackTrace(); } this.setVisible(false); } if (e.getSource() == btncancl) { txtTname.setText(""); txtTcoach.setText(""); txtTlocation.setText(""); this.setVisible(false); } } } 我该怎么改 是应该使用多线程吗?
求教 程序如下 : package 中超联赛联赛积分管理系统; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.swing.*; public class Tadd extends JFrame implements ActionListener { static Connection con; static PreparedStatement sql; private JPanel p; private JLabel lblTname, lblTcoach, lblTlocation; private JTextField txtTname, txtTcoach, txtTlocation; private JButton btnok, btncancl; public Tadd() { super("球队信息添加"); p = new JPanel(); lblTname = new JLabel("球队名称"); lblTcoach = new JLabel("主教练"); lblTlocation = new JLabel("主场位置"); txtTname = new JTextField(20); txtTcoach = new JTextField(20); txtTlocation = new JTextField(20); btnok = new JButton("添加"); btncancl = new JButton("取消"); p.setLayout(null); lblTname.setBounds(90, 20, 100, 25); lblTcoach.setBounds(90, 70, 100, 25); lblTlocation.setBounds(90, 120, 100, 25); txtTname.setBounds(200, 20, 100, 25); txtTcoach.setBounds(200, 70, 100, 25); txtTlocation.setBounds(200, 120, 100, 25); btnok.setBounds(90, 200, 60, 25); btncancl.setBounds(200, 200, 60, 25); p.add(lblTname); p.add(txtTname); p.add(lblTcoach); p.add(txtTcoach); p.add(lblTlocation); p.add(txtTlocation); p.add(btnok); p.add(btncancl); btnok.addActionListener(this); btncancl.addActionListener(this); this.add(p); this.setSize(400, 300); this.setLocation(100, 100); this.setResizable(false); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { Tadd ta = new Tadd(); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if (e.getSource() == btnok) { String strTname = txtTname.getText(); String strTcoach = txtTcoach.getText(); String strTlocation = txtTlocation.getText(); if (strTname.equals("")) { JOptionPane.showMessageDialog(btnok, "球队名不能为空!"); return; } if (strTcoach.equals("")) { JOptionPane.showMessageDialog(btnok, "教练姓名不能为空!"); return; } if (strTlocation.equals("")) { JOptionPane.showMessageDialog(btnok, "主场位置不能为空!"); return; } try { sql = con.prepareStatement("INSERT INTO tb_team(Tname,Tcoach,Tlocation) "+"values(?,?,?)"); sql.setString(1, strTname); sql.setString(2, strTcoach); sql.setString(3,strTlocation); sql.executeUpdate(); } catch (Exception ex) { ex.printStackTrace(); } this.setVisible(false); } if (e.getSource() == btncancl) { txtTname.setText(""); txtTcoach.setText(""); txtTlocation.setText(""); this.setVisible(false); } } } 结果再添加数据时 出现 at 中超联赛联赛积分管理系统.Tadd.actionPerformed(Tadd.java:89) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 程序哪里出错了?我该怎么改?
1 下一页