当前位置: 技术问答>java相关
【谁能解决谁是老大】【该程序有500行,你能解决吗?】
来源: 互联网 发布时间:2015-02-18
本文导语: 小弟碰到一java数据库问题,急求搞定,可惜没人能行:: 数据库的添加是通过menuitem触发的,请仔细研究,谢谢! // // import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class notebook ex...
小弟碰到一java数据库问题,急求搞定,可惜没人能行::
数据库的添加是通过menuitem触发的,请仔细研究,谢谢!
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class notebook extends JFrame
{
//================================变量申明========================================//
Container ctp = getContentPane();
Color coolblue = new Color(204,204,255);
Font myfont= new Font("宋体",Font.PLAIN,12);
Connection theConnection;
Statement theStatement;
ResultSet theResult;
ResultSetMetaData theMetaData;
String theDataSource,
theUser,
thePassword;
JPanel paBtn= new JPanel(),
paTbl= new JPanel(),
paScrll = new JPanel(),
paStatu=new JPanel();
JPanel paMenu = new JPanel();
JMenuBar theMenuBar = new JMenuBar();
JMenu menuFile = new JMenu ("文件");
JMenuItem menuItemNew = new JMenuItem("添加");
JMenuItem menuItemExit = new JMenuItem("退出");
JMenu menuEdit = new JMenu("编辑");
JMenuItem menuItemDel=new JMenuItem("删除");
ImageIcon iconAdd1 = new ImageIcon("add.gif"),
iconAdd2 = new ImageIcon("add_up.gif"),
iconAdd3 = new ImageIcon("add_down.gif"),
iconDel1 = new ImageIcon("del.gif"),
iconDel2 = new ImageIcon("del_up.gif"),
iconDel3 = new ImageIcon("del_down.gif");
JLabel lbStatu = new JLabel(" 开始");
JScrollPane scrll = new JScrollPane();
JTable tbl;
String[] title = {"姓名","年龄","OICQ","Email","电话","详细地址"};
String[][] data = {{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""}};
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
//==================================构造函数===================================//
public notebook()
{
ctp.setLayout(new BorderLayout());
ctp.add("North",theMenuBar);
ctp.add("Center",paTbl);
ctp.add("South",paStatu);
theMenuBar.add(menuFile);
theMenuBar.add(menuEdit);
menuFile.add(menuItemExit);
menuEdit.add(menuItemNew);
menuEdit.add(menuItemDel);
menuFile.setFont(myfont);
menuEdit.setFont(myfont);
menuItemNew.setFont(myfont);
menuItemExit.setFont(myfont);
menuItemDel.setFont(myfont);
setJMenuBar(theMenuBar);
menuItemNew.addActionListener(new myActionListener());
menuItemExit.addActionListener(new myActionListener());
menuItemDel.addActionListener(new myActionListener());
paTbl.setLayout(new BorderLayout());
JTable tbl = new JTable(data,title);
paTbl.add("Center",paScrll);
paTbl.add("South",tbl);
tbl.setBackground(coolblue);
JScrollPane scrll = new JScrollPane(tbl,v,h);
paScrll.setLayout(new BorderLayout());
paScrll.add("Center",scrll);
paStatu.setLayout(new BorderLayout());
paStatu.add(lbStatu);
lbStatu.setFont(myfont);
lbStatu.setForeground(Color.red);
setSize(400,240);
setTitle("notebook");
setVisible(true);
addWindowListener(new MywindowListener());
}
//===================================自定义类===================================//
//frmAdd::添加好友窗口
class frmAdd extends JFrame
{
Container ctpAdd = getContentPane();
JPanel paAddNew = new JPanel(),
paAddMain = new JPanel(),
paAddStatu = new JPanel(),
paBtn = new JPanel();
JLabel lbAddNew = new JLabel("添加新用户"),
lbName = new JLabel("姓名"),
lbAge = new JLabel("年龄"),
lbOicq = new JLabel("OICQ"),
lbEmail = new JLabel("Email"),
lbTel = new JLabel("电话"),
lbAddr = new JLabel("详细地址"),
lbAddStatu = new JLabel(" 添加 "),
lbNull = new JLabel(" "),
lbNull2 =new JLabel(" ");
//JButton btnName = new JButton("hello");
JTextField tfName = new JTextField(5),
tfAge = new JTextField(5),
tfOicq = new JTextField(10),
tfEmail = new JTextField(10),
tfTel = new JTextField(10),
tfAddr = new JTextField(20);
JButton btnAddOk = new JButton("确定"),
btnAddCancel = new JButton("取消");
String addName ,
addAge,
addOicq,
addEmail,
addTel,
addAddr;
public frmAdd()
{
ctpAdd.setFont(myfont);
ctpAdd.setLayout(new BorderLayout());
ctpAdd.add("North",lbAddNew);
ctpAdd.add("Center",paAddMain);
ctpAdd.add("South",paAddStatu);
paAddMain.setLayout(new FlowLayout());
paAddMain.add(lbName);
paAddMain.add(tfName);
paAddMain.add(lbAge);
paAddMain.add(tfAge);
paAddMain.add(lbOicq);
paAddMain.add(tfOicq);
paAddMain.add(lbEmail);
paAddMain.add(tfEmail);
paAddMain.add(lbTel);
paAddMain.add(tfTel);
paAddMain.add(lbNull2);
paAddMain.add(lbAddr);
paAddMain.add(tfAddr);
paAddMain.add(lbNull);
paAddMain.add(paBtn);
paBtn.setLayout(new FlowLayout());
paBtn.add(btnAddOk);
btnAddOk.setFont(myfont);
btnAddOk.setForeground(Color.blue);
paAddMain.add(btnAddCancel);
btnAddCancel.setFont(myfont);
btnAddCancel.setForeground(Color.blue);
paAddStatu.setLayout(new BorderLayout());
paAddStatu.add("Center",lbAddStatu);
lbName.setFont(myfont);
lbAge.setFont(myfont);
lbOicq.setFont(myfont);
lbEmail.setFont(myfont);
lbTel.setFont(myfont);
lbAddr.setFont(myfont);
lbAddNew.setFont(myfont);
lbName.setForeground(Color.blue);
lbAge.setForeground(Color.blue);
lbOicq.setForeground(Color.blue);
lbEmail.setForeground(Color.blue);
lbTel.setForeground(Color.blue);
lbAddr.setForeground(Color.blue);
lbAddNew.setForeground(Color.blue);
lbAddStatu.setFont(myfont);
lbAddStatu.setForeground(Color.red);
btnAddOk.setEnabled(true);
btnAddCancel.setEnabled(true);
btnAddOk.addActionListener(new MyActionListener());
btnAddCancel.addActionListener(new MyActionListener());
setSize(350,200);
setTitle("添加新用户");
setVisible(true);
setResizable(false);
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if (source==btnAddCancel)
{
hide();
}
if (source==btnAddOk)
{
addName=tfName.getText();
addAge=tfName.getText();
addOicq=tfOicq.getText();
addEmail=tfEmail.getText();
addTel=tfTel.getText();
addAddr=tfAddr.getText();
lbAddStatu.setText("正在添加数据库...");
try
{
String insertSQL = "insert into test (name,age,qq,mail,tele,addr) values (addName,addAge,addOicq,addEmail,addTel,addAddr)";
openConnection();
theStatement.executeUpdate(insertSQL);
execSQLCommand("Select * from test");
theConnection.close();
}
catch (Exception g)
{
handleException(g);
}
}
}
};
class MywindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
};
};
//frmDel::删除好友窗口
class frmDel extends JFrame
{
Container ctpDel = getContentPane();
JPanel paDelTitle = new JPanel();
JPanel paDelMain = new JPanel(),
paDelContent = new JPanel(),
paDelEnter = new JPanel();
JPanel paDelStatu = new JPanel();
JLabel lbDelTitle = new JLabel("删除好友资料"),
lbDelStatu = new JLabel(" 删除"),
lbSelect = new JLabel("请输入要删除的名单");
JButton btnDelOk = new JButton("确定"),
btnDelCancel = new JButton("取消");
JTextField tfDelName = new JTextField(10);
public frmDel()
{
ctpDel.setLayout(new BorderLayout());
ctpDel.add("North",paDelTitle);
ctpDel.add("Center",paDelMain);
ctpDel.add("South",paDelStatu);
paDelTitle.setLayout(new FlowLayout());
paDelTitle.add(lbDelTitle);
lbDelTitle.setFont(myfont);
lbDelTitle.setForeground(Color.red);
paDelMain.setLayout(new GridLayout(2,0));
paDelMain.add(paDelContent);
paDelMain.add(paDelEnter);
paDelContent.setLayout(new FlowLayout());
paDelEnter.setLayout(new FlowLayout());
paDelContent.add(lbSelect);
paDelContent.add(tfDelName);
lbSelect.setFont(myfont);
lbSelect.setForeground(Color.blue);
paDelEnter.add(btnDelOk);
paDelEnter.add(btnDelCancel);
btnDelOk.setFont(myfont);
btnDelOk.setForeground(Color.blue);
btnDelCancel.setForeground(Color.blue);
btnDelCancel.setFont(myfont);
paDelStatu.setLayout(new BorderLayout());
paDelStatu.add(lbDelStatu);
lbDelStatu.setForeground(Color.red);
lbDelStatu.setFont(myfont);
btnDelCancel.addActionListener(new DelActionListener());
btnDelOk.addActionListener(new DelActionListener());
setSize(350,140);
setTitle("删除好友 ");
setVisible(true);
setResizable(false);
}
//frmDel::事件处理
class DelActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object s = e.getSource();
if (s==btnDelCancel)
{
hide();
}
}
};
};
//===================================public class 事件处理=====================================//
public void handleException (Exception e)
{
lbStatu.setText("有错误发生:"+e.getMessage());
}
class MywindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if (source==menuItemNew)
{
frmAdd addfrm=new frmAdd();
setVisible(true);
}
if (source==menuItemDel)
{
frmDel delfrm=new frmDel();
setVisible(true);
}
if (source==menuItemExit)
{
dispose();
System.exit(0);
}
}
}
//建立数据库连接
public void openConnection()
{
theDataSource="jdbc:odbc:Mytest";
theUser="";
thePassword="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);
theConnection.setReadOnly(true);
lbStatu.setText("一切准备就绪");
}
catch(Exception e)
{
handleException(e);
}
}
//执行查询语句,保存结果
public void execSQLCommand(String command)
{
;
try
{
theStatement=theConnection.createStatement();
theResult=theStatement.executeQuery(command);
theMetaData=theResult.getMetaData();
int columnCount=theMetaData.getColumnCount();
int i,j=0;
while(theResult.next())
{
for(i=1;i
数据库的添加是通过menuitem触发的,请仔细研究,谢谢!
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class notebook extends JFrame
{
//================================变量申明========================================//
Container ctp = getContentPane();
Color coolblue = new Color(204,204,255);
Font myfont= new Font("宋体",Font.PLAIN,12);
Connection theConnection;
Statement theStatement;
ResultSet theResult;
ResultSetMetaData theMetaData;
String theDataSource,
theUser,
thePassword;
JPanel paBtn= new JPanel(),
paTbl= new JPanel(),
paScrll = new JPanel(),
paStatu=new JPanel();
JPanel paMenu = new JPanel();
JMenuBar theMenuBar = new JMenuBar();
JMenu menuFile = new JMenu ("文件");
JMenuItem menuItemNew = new JMenuItem("添加");
JMenuItem menuItemExit = new JMenuItem("退出");
JMenu menuEdit = new JMenu("编辑");
JMenuItem menuItemDel=new JMenuItem("删除");
ImageIcon iconAdd1 = new ImageIcon("add.gif"),
iconAdd2 = new ImageIcon("add_up.gif"),
iconAdd3 = new ImageIcon("add_down.gif"),
iconDel1 = new ImageIcon("del.gif"),
iconDel2 = new ImageIcon("del_up.gif"),
iconDel3 = new ImageIcon("del_down.gif");
JLabel lbStatu = new JLabel(" 开始");
JScrollPane scrll = new JScrollPane();
JTable tbl;
String[] title = {"姓名","年龄","OICQ","Email","电话","详细地址"};
String[][] data = {{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""},
{"","","","","",""}};
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
//==================================构造函数===================================//
public notebook()
{
ctp.setLayout(new BorderLayout());
ctp.add("North",theMenuBar);
ctp.add("Center",paTbl);
ctp.add("South",paStatu);
theMenuBar.add(menuFile);
theMenuBar.add(menuEdit);
menuFile.add(menuItemExit);
menuEdit.add(menuItemNew);
menuEdit.add(menuItemDel);
menuFile.setFont(myfont);
menuEdit.setFont(myfont);
menuItemNew.setFont(myfont);
menuItemExit.setFont(myfont);
menuItemDel.setFont(myfont);
setJMenuBar(theMenuBar);
menuItemNew.addActionListener(new myActionListener());
menuItemExit.addActionListener(new myActionListener());
menuItemDel.addActionListener(new myActionListener());
paTbl.setLayout(new BorderLayout());
JTable tbl = new JTable(data,title);
paTbl.add("Center",paScrll);
paTbl.add("South",tbl);
tbl.setBackground(coolblue);
JScrollPane scrll = new JScrollPane(tbl,v,h);
paScrll.setLayout(new BorderLayout());
paScrll.add("Center",scrll);
paStatu.setLayout(new BorderLayout());
paStatu.add(lbStatu);
lbStatu.setFont(myfont);
lbStatu.setForeground(Color.red);
setSize(400,240);
setTitle("notebook");
setVisible(true);
addWindowListener(new MywindowListener());
}
//===================================自定义类===================================//
//frmAdd::添加好友窗口
class frmAdd extends JFrame
{
Container ctpAdd = getContentPane();
JPanel paAddNew = new JPanel(),
paAddMain = new JPanel(),
paAddStatu = new JPanel(),
paBtn = new JPanel();
JLabel lbAddNew = new JLabel("添加新用户"),
lbName = new JLabel("姓名"),
lbAge = new JLabel("年龄"),
lbOicq = new JLabel("OICQ"),
lbEmail = new JLabel("Email"),
lbTel = new JLabel("电话"),
lbAddr = new JLabel("详细地址"),
lbAddStatu = new JLabel(" 添加 "),
lbNull = new JLabel(" "),
lbNull2 =new JLabel(" ");
//JButton btnName = new JButton("hello");
JTextField tfName = new JTextField(5),
tfAge = new JTextField(5),
tfOicq = new JTextField(10),
tfEmail = new JTextField(10),
tfTel = new JTextField(10),
tfAddr = new JTextField(20);
JButton btnAddOk = new JButton("确定"),
btnAddCancel = new JButton("取消");
String addName ,
addAge,
addOicq,
addEmail,
addTel,
addAddr;
public frmAdd()
{
ctpAdd.setFont(myfont);
ctpAdd.setLayout(new BorderLayout());
ctpAdd.add("North",lbAddNew);
ctpAdd.add("Center",paAddMain);
ctpAdd.add("South",paAddStatu);
paAddMain.setLayout(new FlowLayout());
paAddMain.add(lbName);
paAddMain.add(tfName);
paAddMain.add(lbAge);
paAddMain.add(tfAge);
paAddMain.add(lbOicq);
paAddMain.add(tfOicq);
paAddMain.add(lbEmail);
paAddMain.add(tfEmail);
paAddMain.add(lbTel);
paAddMain.add(tfTel);
paAddMain.add(lbNull2);
paAddMain.add(lbAddr);
paAddMain.add(tfAddr);
paAddMain.add(lbNull);
paAddMain.add(paBtn);
paBtn.setLayout(new FlowLayout());
paBtn.add(btnAddOk);
btnAddOk.setFont(myfont);
btnAddOk.setForeground(Color.blue);
paAddMain.add(btnAddCancel);
btnAddCancel.setFont(myfont);
btnAddCancel.setForeground(Color.blue);
paAddStatu.setLayout(new BorderLayout());
paAddStatu.add("Center",lbAddStatu);
lbName.setFont(myfont);
lbAge.setFont(myfont);
lbOicq.setFont(myfont);
lbEmail.setFont(myfont);
lbTel.setFont(myfont);
lbAddr.setFont(myfont);
lbAddNew.setFont(myfont);
lbName.setForeground(Color.blue);
lbAge.setForeground(Color.blue);
lbOicq.setForeground(Color.blue);
lbEmail.setForeground(Color.blue);
lbTel.setForeground(Color.blue);
lbAddr.setForeground(Color.blue);
lbAddNew.setForeground(Color.blue);
lbAddStatu.setFont(myfont);
lbAddStatu.setForeground(Color.red);
btnAddOk.setEnabled(true);
btnAddCancel.setEnabled(true);
btnAddOk.addActionListener(new MyActionListener());
btnAddCancel.addActionListener(new MyActionListener());
setSize(350,200);
setTitle("添加新用户");
setVisible(true);
setResizable(false);
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if (source==btnAddCancel)
{
hide();
}
if (source==btnAddOk)
{
addName=tfName.getText();
addAge=tfName.getText();
addOicq=tfOicq.getText();
addEmail=tfEmail.getText();
addTel=tfTel.getText();
addAddr=tfAddr.getText();
lbAddStatu.setText("正在添加数据库...");
try
{
String insertSQL = "insert into test (name,age,qq,mail,tele,addr) values (addName,addAge,addOicq,addEmail,addTel,addAddr)";
openConnection();
theStatement.executeUpdate(insertSQL);
execSQLCommand("Select * from test");
theConnection.close();
}
catch (Exception g)
{
handleException(g);
}
}
}
};
class MywindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
};
};
//frmDel::删除好友窗口
class frmDel extends JFrame
{
Container ctpDel = getContentPane();
JPanel paDelTitle = new JPanel();
JPanel paDelMain = new JPanel(),
paDelContent = new JPanel(),
paDelEnter = new JPanel();
JPanel paDelStatu = new JPanel();
JLabel lbDelTitle = new JLabel("删除好友资料"),
lbDelStatu = new JLabel(" 删除"),
lbSelect = new JLabel("请输入要删除的名单");
JButton btnDelOk = new JButton("确定"),
btnDelCancel = new JButton("取消");
JTextField tfDelName = new JTextField(10);
public frmDel()
{
ctpDel.setLayout(new BorderLayout());
ctpDel.add("North",paDelTitle);
ctpDel.add("Center",paDelMain);
ctpDel.add("South",paDelStatu);
paDelTitle.setLayout(new FlowLayout());
paDelTitle.add(lbDelTitle);
lbDelTitle.setFont(myfont);
lbDelTitle.setForeground(Color.red);
paDelMain.setLayout(new GridLayout(2,0));
paDelMain.add(paDelContent);
paDelMain.add(paDelEnter);
paDelContent.setLayout(new FlowLayout());
paDelEnter.setLayout(new FlowLayout());
paDelContent.add(lbSelect);
paDelContent.add(tfDelName);
lbSelect.setFont(myfont);
lbSelect.setForeground(Color.blue);
paDelEnter.add(btnDelOk);
paDelEnter.add(btnDelCancel);
btnDelOk.setFont(myfont);
btnDelOk.setForeground(Color.blue);
btnDelCancel.setForeground(Color.blue);
btnDelCancel.setFont(myfont);
paDelStatu.setLayout(new BorderLayout());
paDelStatu.add(lbDelStatu);
lbDelStatu.setForeground(Color.red);
lbDelStatu.setFont(myfont);
btnDelCancel.addActionListener(new DelActionListener());
btnDelOk.addActionListener(new DelActionListener());
setSize(350,140);
setTitle("删除好友 ");
setVisible(true);
setResizable(false);
}
//frmDel::事件处理
class DelActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object s = e.getSource();
if (s==btnDelCancel)
{
hide();
}
}
};
};
//===================================public class 事件处理=====================================//
public void handleException (Exception e)
{
lbStatu.setText("有错误发生:"+e.getMessage());
}
class MywindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if (source==menuItemNew)
{
frmAdd addfrm=new frmAdd();
setVisible(true);
}
if (source==menuItemDel)
{
frmDel delfrm=new frmDel();
setVisible(true);
}
if (source==menuItemExit)
{
dispose();
System.exit(0);
}
}
}
//建立数据库连接
public void openConnection()
{
theDataSource="jdbc:odbc:Mytest";
theUser="";
thePassword="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);
theConnection.setReadOnly(true);
lbStatu.setText("一切准备就绪");
}
catch(Exception e)
{
handleException(e);
}
}
//执行查询语句,保存结果
public void execSQLCommand(String command)
{
;
try
{
theStatement=theConnection.createStatement();
theResult=theStatement.executeQuery(command);
theMetaData=theResult.getMetaData();
int columnCount=theMetaData.getColumnCount();
int i,j=0;
while(theResult.next())
{
for(i=1;i