1.用JTable作数据库数据显示;用文本框实现往数据库表中添加记录;按钮--添加事件、删除事件、修改事件....。
2.简单的:你就作个两列表,数据库当然选用你会的。
3.逻辑判断:空值怎么处理?字段非法怎么处理?...........
4.你可以参照其它数据库操作工具来实现一些功能,以此来体现你的能力。
2.简单的:你就作个两列表,数据库当然选用你会的。
3.逻辑判断:空值怎么处理?字段非法怎么处理?...........
4.你可以参照其它数据库操作工具来实现一些功能,以此来体现你的能力。
//帮忙改正
//虽然你会很痛苦,但我会送上我所有的分哦!!!!
--------------------------------------------------------------
这个程序是连接数据库,读StuTable.db文件的数据,
StuTable只包含了学号和姓名两列,并要添加,删除。
-----------------------------
import java.net.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class MyFrist
{
public static void main(String[] args)
{
JFrame frame=new MyFristProgram();
frame.show();
}
}class MyFristProgram extends JFrame
{
public MyFristProgram()
{
setTitle("myfrist");
etSize(400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane=getContentPane();
JPanel northpanel=new JPanel();
northpanel.add(new JLabel("学号"));
JTextField stu_id=new JTextField("any",8);
northpanel.add(stu_id);
northpanel.add(new JLabel("姓名"));
JTextField name_id=new JTextField("any",8);
northpanel.add(name_id);
JButton findbutton=new JButton("查找");
northpanel.add(findbutton);
findbutton.addActionListener(new FindAction());
contentPane.add(northpanel ,BorderLayout.NORTH);
JTextArea centerTextArea=new JTextArea("the result",5,30);
JScrollPane scollpanel=new JScrollPane(centerpanel);
contentPane.add(scollpanel,BorderLayout.CENTER);
JPanel southpanel=new JPanel();
JButton adding=new JButton("插入");
JButton delete=new JButton("删除");
JButton cancel=new JButton("关闭");
southpanel.add(adding);
southpanel.add(delete);
southpanel.add(cancel);
adding.addActionListener(new AddAction());
delete.addActionListener(new DeleteAction());//
//这个DeleteAction没写,应为不会写,望高手可以补上。
cancel.addActionListener(new
ActionListener()
{
public void actionPerformed(AtionEvent event)
{
System.exit(0);
}
});
contentPane.add(southpanel,BorderLayout.SOUTH);
}
private class FindAction implements ActionListener
{
public getValue()
{
try {
Integer.parseInt(stu_id.getText());//
ruturn stu_id.getText();//这里报错,找不到stu_id;
}catch(NumberFormatException e)
{
return "any";
}
}
public void actionPerformed(ActionEvent event)
{
String stuid=getValue();
String nameid=name_id.getText();
/** create a connect to DB
* but i don't know how could i do
* I suppose the DB's name is BED
* and username is myfrist
* password is flowblood
*/
try{
String url="jdbc:odbc//163.com:8080/BED";
String DBUsername="myfrist";
String DBPassword="flowblood";
Connection con
=DriverManager.getConnection(url,DBUsername,DBPassword);
Statement stmt=con.createStatement();
}catch(Exception e)
{ System.out.plintln("error"+e);}
/**
*execute SQL query
*
*/
ResultSet rs=null;
try
{
if(!stuid.equals("any")&&!nameid.equals("any"))
{
if(AllQuerystmt==null {
String AllQuery=
"SELECT *"+
"FROM StuTable"+
"WHILE StuTable.StuNumber=stuid AND"+
"StuTable.StuName=nameid ";
AllQuerystmt
=con.prepareStatement(AllQuery);
}
rs=AllQuerystmt.exexuteQuery();
}
else if(stuid.equals("any")&&!nameid.equals("any"))
{
if(NameIdQuerystmt==null)
{
String NameIdQuery=
"SELECT *"+
"FROM StuTable"+
"WHILE StuTable.StuName=nameid"+
"StuTable.StuNumber=? " ;
NameIdQuerystmt=
con.preareStatement(NameIdQuery);
}
NameIdQuerystmt.setString(1,stuid);//
// here is error or correct???
rs=NameIdQuerystmt.executeQuery();
else
{ // do nothing
}
centerTextArea.setText("the result");
while(rs.next)
{
centerTextArea.append(rs.getString(1)
+" | "+rs.getSting(2)+"\n");
}
}
}catch(Exception e)
{
centerTextArea.setText("error"+e);
}
}
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement AllQuerystmt;
private PreparedStatement NameIdQuerystmt;
}
private class AddAction implements ActionListener
{ //我想让弹出面板在父面板的上面,,,但我不会,请指教。
public void actionPerFormed(ActionEvent event)
{
if (dialogpanel==null)
dialogpanel=new DialogPanel();
else if(dialogpanel.isVisible())
{ dialogpanel.setVisible(false);
dialogpanel.show();
}
else if(!dialogpanel.isVisible())
{ dialogpanel.setVisible(true);
dialogpanel.show();
}
}
DialogPanel dialogpanel;
}
//MyFristPrograme variale are here
private JPanel northpanel;
private JPanel southpanel;
private JTextField stu_id;
private JTextField name_id;
private JTextArea centerpanel;
private JScrollPane scollpanel;
private JButton findbutton;
private JButton adding;
private JButton delete;
private JButton cancel;
}
class DialogPanel extends JFrame
{
public Dialog()
{
setSize(200,120);
setTitle("插入数据");
getContentPane().setLayout(new BorderLayout());
JPanel panel= new JPanel();
panel.setLayout(new GridLayout(2,2));
panel.add(new JLabel("学号"));
JTextField userNumber= new JTextField("Number",10);
panel.add(userNumber);
panel.add(new JLabel("学生名"));
JTextField userName=new JTextField("Name",10);
panel.add(userName);
getContentPane().add(panel,BorderLayout.CENTER);
JPanel buttonPanel=new JPanel();
JButton okButton=new JButton("OK");
JButton cancelButton=new JButton("cancel");
buttonPanel.add(okButton);//
//加一个监听,按OK就把数据加入DB,我不会呀
buttonPanel.add(cancelButton);
//按取消,就关闭弹出面板,可以和AddAction一样设置不显示吗?
getContentPane().add(buttonPanel,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
JFrame celltest=new DialogPanel();
celltest.show();
}
private JTextField userNumber;
private JTextField userName;
private JButton okButton;
private JButton cancelButton;
}
这里是你写错了,因该是 return 吧> //加一个监听,按OK就把数据加入DB,我不会呀
对于事件的处理一般用 匿名内部类来做 这样就不用写那么多 class implements ActionListener 了 也省得老是要传递自己的引用
就像你的 WindowListener 一样
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
这里面的一个 new WindowAdapter() { } 就是一个内部类,为了应付考试的话 你也不用理解得太深 回用就行了
对于 Button 来说
JButton.addActionListener(new ActionListener(){
public void actionPerformed(AtionEvent event){
//Handle the event
}
});
> //我想让弹出面板在父面板的上面,,,但我不会,请指教。
对于 JDialog 来说有一个构造函数 JDialog(Frame owner, boolean modal) 那个 boolean 值就是用来设置 JDialog 是否为 modal 的,设置为 true 就行了 一般用 new JDialog(null, true); 让它置于屏幕的中央> //按取消,就关闭弹出面板,可以和AddAction一样设置不显示吗?
要关闭弹出面板 让他 dispose() 就可以了
先回答你几个问题 看你的代码确实有点累 不过话说回来 还算比较规范 以前写过 VC ?
建议你多看查查 Java 的 API 很有帮助