📄 shujuku.java
字号:
JButton b1= new JButton("确定");
dialogPane.add(b1);
JButton b2= new JButton("取消");
dialogPane.add(b2);
b1.addActionListener(this);//为两个按钮添加事件监听器
b2.addActionListener(this);
dialog.setBounds(200,100,400,180);
dialog.setBackground(Color.blue);//背景颜色
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();//获得事件对象的相关名称
if (cmd.equals("确定"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
Connection con=DriverManager.getConnection("jdbc:odbc:PIMS");//连接数据库
Statement stmt=con.createStatement();//创建语句
String xuehao=tF1.getText();
tF1.setForeground(Color.blue);
String xingming=tF2.getText();
String xingbie=tF3.getText();
String nianling=tF4.getText();
String zhuanye=tF5.getText();
if (xuehao.length()==0||xingming.length()==0||xingbie.length()==0||nianling.length()==0||zhuanye.length()==0)
{
JOptionPane.showMessageDialog(dialog, "输入信息不能为空");
}
else
{
stmt.executeUpdate("INSERT INTO xuesheng VALUES ('"+xuehao+"','"+xingming+"','"+xingbie+"','"+nianling+"','"+zhuanye+"')");//执行添加
JOptionPane.showMessageDialog(dialog, "添加成功 ");
}
stmt.close();
con.close();//关闭连接
dialog.dispose();//直接返回主页面
}catch(Exception ex)
{
}
}
else if (cmd.equals("取消"))
{
dialog.dispose();//直接返回主页面
}
}
}
class xuehaochazhao implements ActionListener
{
JDialog dialog;
JTextField tF1=new JTextField();//定义输入框用于输入信息
JTextField tF2=new JTextField();//定义输入框用于输入信息
JTextField tF3=new JTextField();//定义输入框用于输入信息
JTextField tF4=new JTextField();//定义输入框用于输入信息
JTextField tF5=new JTextField();//定义输入框用于输入信息
JTextField tF6=new JTextField();//定义输入框用于输入信息
xuehaochazhao(JFrame f)
{
dialog=new JDialog(f,"按学号查找",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(7,2));//设计界面图形
dialogPane.add(new JLabel("在此输入要查找的学号:",SwingConstants.CENTER));
dialogPane.add(tF6);
dialogPane.add(new JLabel("学生学号:",SwingConstants.CENTER));
dialogPane.add(tF1);
dialogPane.add(new JLabel("学生姓名:",SwingConstants.CENTER));
dialogPane.add(tF2);
dialogPane.add(new JLabel("学生性别:",SwingConstants.CENTER));
dialogPane.add(tF3);
dialogPane.add(new JLabel("学生年龄:",SwingConstants.CENTER));
dialogPane.add(tF4);
dialogPane.add(new JLabel("学生专业:",SwingConstants.CENTER));
dialogPane.add(tF5);
JButton b1=new JButton("确定");
dialogPane.add(b1);
JButton b2= new JButton("取消");
dialogPane.add(b2);
b1.addActionListener(this);//为两个按钮添加事件监听器
b2.addActionListener(this);
dialog.setBounds(200,200,400,200);
dialog.setBackground(Color.red);//背景颜色
//dialog.show();
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();//获得事件对象的相关名称
if (cmd.equals("确定"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
Connection con=DriverManager.getConnection("jdbc:odbc:PIMS");//连接数据库
PreparedStatement pstmt=con.prepareStatement("select 学号,姓名,性别,年龄,专业 from xuesheng where 学号=?");
pstmt.setString(1,tF6.getText());//执行查找
ResultSet rs=pstmt.executeQuery();
if(rs.next()==false)
{
JOptionPane.showMessageDialog(dialog, "没有此人,请确认后再查");
}
else
{
tF1.setText(rs.getString("学号"));
tF1.setForeground(Color.blue);
tF2.setText(rs.getString("姓名"));
tF2.setForeground(Color.blue);
tF3.setText(rs.getString("性别"));
tF3.setForeground(Color.blue);
tF4.setText(rs.getString("年龄"));
tF4.setForeground(Color.blue);
tF5.setText(rs.getString("专业"));
tF5.setForeground(new Color(121,44,32));
}
pstmt.close();
con.close();
}catch (Exception ex){}
}
else if (cmd.equals("取消"))
{
dialog.dispose();//直接返回主页面
}
}
}
class xingmingchazhao implements ActionListener
{
JDialog dialog;
JTextField tF1=new JTextField();//定义输入框用于输入信息
JTextField tF2=new JTextField();//定义输入框用于输入信息
JTextField tF3=new JTextField();//定义输入框用于输入信息
JTextField tF4=new JTextField();//定义输入框用于输入信息
JTextField tF5=new JTextField();//定义输入框用于输入信息
JTextField tF6=new JTextField();//定义输入框用于输入信息
xingmingchazhao(JFrame f)
{
dialog=new JDialog(f,"按姓名查找",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(7,2));//设计界面图形
dialogPane.add(new JLabel("在此输入要查找的姓名:",SwingConstants.CENTER));
dialogPane.add(tF6);
dialogPane.add(new JLabel("学生学号:",SwingConstants.CENTER));
dialogPane.add(tF1);
dialogPane.add(new JLabel("学生姓名:",SwingConstants.CENTER));
dialogPane.add(tF2);
dialogPane.add(new JLabel("学生性别:",SwingConstants.CENTER));
dialogPane.add(tF3);
dialogPane.add(new JLabel("学生年龄:",SwingConstants.CENTER));
dialogPane.add(tF4);
dialogPane.add(new JLabel("学生专业:",SwingConstants.CENTER));
dialogPane.add(tF5);
JButton b1=new JButton("确定");
dialogPane.add(b1);
JButton b2= new JButton("取消");
dialogPane.add(b2);
b1.addActionListener(this);//为两个按钮添加事件监听器
b2.addActionListener(this);
dialog.setBounds(200,200,400,200);
dialog.setBackground(Color.blue);//背景颜色
//dialog.show();
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();//获得事件对象的相关名称
if (cmd.equals("确定"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
Connection con=DriverManager.getConnection("jdbc:odbc:PIMS");//连接数据库
PreparedStatement pstmt=con.prepareStatement("select 姓名,学号,性别,年龄,专业 from xuesheng where 姓名=?");
pstmt.setString(1,tF6.getText());//执行查找
ResultSet rs=pstmt.executeQuery();
if(rs.next()==false)
{
JOptionPane.showMessageDialog(dialog, "没有此人,请确认后再查");
}
else
{
tF1.setText(rs.getString("学号"));
tF1.setForeground(Color.blue);
tF2.setText(rs.getString("姓名"));
tF2.setForeground(Color.blue);
tF3.setText(rs.getString("性别"));
tF3.setForeground(Color.blue);
tF4.setText(rs.getString("年龄"));
tF4.setForeground(Color.blue);
tF5.setText(rs.getString("专业"));
tF5.setForeground(new Color(121,44,32));
}
pstmt.close();
con.close();
}catch (Exception ex){}
}
else if (cmd.equals("取消"))
{
dialog.dispose();//直接返回主页面
}
}
}
class xuehaoshanchu implements ActionListener
{
JDialog dialog;
JTextField tF1=new JTextField();//定义输入框用于输入信息
xuehaoshanchu(JFrame f)
{
dialog=new JDialog(f,"按学号删除",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(2,2));//设计界面图形
dialogPane.add(new JLabel("在此输入要删除的学号:",SwingConstants.CENTER));
dialogPane.add(tF1);
JButton b1=new JButton("确定");
dialogPane.add(b1);
JButton b2= new JButton("取消");
dialogPane.add(b2);
b1.addActionListener(this);//为两个按钮添加事件监听器
b2.addActionListener(this);
dialog.setBounds(100,90,300,90);
dialog.setBackground(Color.black);//背景颜色
//dialog.show();
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();//获得事件对象的相关名称
if (cmd.equals("确定"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
Connection con=DriverManager.getConnection("jdbc:odbc:PIMS");//连接数据库
PreparedStatement pstmt=con.prepareStatement("delete from xuesheng where 学号=?");
pstmt.setString(1,tF1.getText());
if (pstmt.executeUpdate()!=1)
{
JOptionPane.showMessageDialog(dialog, "没有此人无法删除");
}
else
{
JOptionPane.showMessageDialog(dialog, "删除成功");
}
pstmt.close();
con.close ();
}catch(Exception ex){}
}
else if(cmd.equals("取消"))
{
dialog.dispose();
}
}
}
class xingmingshanchu implements ActionListener
{
JDialog dialog;
JTextField tF1=new JTextField();//定义输入框用于输入信息
xingmingshanchu(JFrame f)
{
dialog=new JDialog(f,"按姓名删除",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(2,2));//设计界面图形
dialogPane.add(new JLabel("在此输入要删除的姓名:",SwingConstants.CENTER));
dialogPane.add(tF1);
JButton b1=new JButton("确定");
dialogPane.add(b1);
JButton b2= new JButton("取消");
dialogPane.add(b2);
b1.addActionListener(this);//为两个按钮添加事件监听器
b2.addActionListener(this);
dialog.setBounds(100,90,300,90);
dialog.setBackground(Color.black);//背景颜色
//dialog.show();
dialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();//获得事件对象的相关名称
if (cmd.equals("确定"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
Connection con=DriverManager.getConnection("jdbc:odbc:PIMS");//连接数据库
PreparedStatement pstmt=con.prepareStatement("delete from xuesheng where 姓名=?");
pstmt.setString(1,tF1.getText());
if (pstmt.executeUpdate()!=1)
{
JOptionPane.showMessageDialog(dialog, "没有此人无法删除");
}
else
{
JOptionPane.showMessageDialog(dialog, "删除成功");
}
pstmt.close();
con.close ();
}catch(Exception ex){}
}
else if(cmd.equals("取消"))
{
dialog.dispose();
}
}
}
class bangzhu implements ActionListener
{
JDialog dialog;
JLabel label=new JLabel("E:\\java练习\\第9章\\图形的数据库\\1.jpg");
bangzhu(JFrame f)
{
dialog=new JDialog(f,"帮助",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(2,1));//设计界面图形
dialogPane.add(label);//添加控件
JButton b1=new JButton("确定");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -