📄 pj_record.java
字号:
package prapare_manager;
import java.sql.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import user.quanxian;
import java.sql.DriverManager;
public class pj_record extends JFrame implements ActionListener,ItemListener
{
private static final long serialVersionUID = -7700451926035976789L;
JPanel cp;
int i1=0,i2=0;//复选框选择记录
JScrollPane jsp = new JScrollPane();
ImageIcon icon=new ImageIcon("picture/12.jpg");
JLabel tp=new JLabel(icon,JLabel.CENTER);
Object[][] rowData=new Object[50][5];
String[] columnNames = {
"项目","成绩","创造者","创造者学院", "创造地点"};
JTable jT1 = new JTable(rowData, columnNames) ;
JLabel j1=new JLabel("学 院:");
JLabel j2=new JLabel("参赛项目: ");
JLabel j3=new JLabel("创造者:");
JLabel j4=new JLabel("成绩:");
JLabel j5=new JLabel("创造地点:");
JLabel j6=new JLabel("选择对象:");
JLabel j7=new JLabel("默认方式下,查询按创造者(姓名和学院)进行操作!");
JComboBox cbx1=new JComboBox();
JComboBox cbx2=new JComboBox();
JButton b1=new JButton("添 加");
JButton b2=new JButton("删 除");
JButton b3=new JButton("查 询");
JButton b4=new JButton("修改");
JButton b5=new JButton("刷 新");
JButton b6=new JButton("返回");
JCheckBox cb1=new JCheckBox("学 院",false);
JCheckBox cb2=new JCheckBox("项 目",false);
JTextField jt1=new JTextField(16);
JTextField jt2=new JTextField(16);
JTextField jt3=new JTextField(16);
public pj_record()
{try{Init();if(new quanxian().get_quanxian()==2){b2.setEnabled(false);b1.setEnabled(false);b4.setEnabled(false);}
}catch (Exception err){err.printStackTrace();}}
public void Init() throws Exception{
cp=(JPanel)this.getContentPane();
cp.setLayout(null);
this.setSize(760,540);
this.setTitle("项目纪录");
b1.setBounds(600,20,85,30);
b1.setFont(new java.awt.Font("Dialog",0,15));
b1.addActionListener(this);
cp.add(b1);
b2.setBounds(600,70,85,30);
b2.setFont(new java.awt.Font("Dialog",0,15));
b2.addActionListener(this);
cp.add(b2);
b3.setBounds(600,120,85,30);
b3.setFont(new java.awt.Font("Dialog",0,15));
b3.addActionListener(this);
cp.add(b3);
b4.setBounds(600,170,85,30);
b4.setFont(new java.awt.Font("Dialog",0,15));
b4.addActionListener(this);
cp.add(b4);
b5.setBounds(600,220,85,30);
b5.setFont(new java.awt.Font("Dialog",0,15));
b5.addActionListener(this);
cp.add(b5);
b6.setBounds(600,270,85,30);
b6.setFont(new java.awt.Font("Dialog",0,15));
b6.addActionListener(this);
cp.add(b6);
j6.setBounds(500,340,160,30);
j6.setForeground(new Color(255,0,0));
j6.setBackground(new Color(255,255,255));
j6.setFont(new java.awt.Font("Dialog",0,16));
cp.add(j6,null);
cb1.setBounds(570,340,60,30);
cb1.setBackground(new Color(255,255,255));
cb1.addItemListener(this);
cp.add(cb1,null);
cb2.setBounds(630,340,60,30);
cb2.setBackground(new Color(255,255,255));
cb2.addItemListener(this);
cp.add(cb2,null);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs1=null;
rs1=ps.executeQuery("select * from 学院名称表");
while(rs1.next()){cbx1.addItem(rs1.getString(1));}
ResultSet rs2=null;
rs2=ps.executeQuery("select * from 项目表");
while(rs2.next()){cbx2.addItem(rs2.getString(1));}
}
catch(Exception err){err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接出错!");}
j1.setBounds(30,340,60,30);
j1.setForeground(new Color(255,0,0));
j1.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j1,null);
cbx1.setBounds(90,340,160,30);
cbx1.setFont(new java.awt.Font("Dialog",0,15));
cp.add(cbx1,null);
j2.setBounds(290,340,80,30);
j2.setFont(new java.awt.Font("Dialog",0,16));
j2.setForeground(new Color(255,0,0));
cp.add(j2,null);
cbx2.setBounds(360,340,100,30);
cbx2.setFont(new java.awt.Font("Dialog",0,14));
cp.add(cbx2,null);
j3.setBounds(30,400,60,30);
j3.setForeground(new Color(255,0,0));
j3.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j3,null);
jt1.setBounds(90,400,80,30);
jt1.setFont(new java.awt.Font("Dialog",0,15));
cp.add(jt1,null);
j4.setBounds(320,400,60,30);
j4.setForeground(new Color(255,0,0));
j4.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j4,null);
jt2.setBounds(360,400,80,30);
jt2.setFont(new java.awt.Font("Dialog",0,15));
cp.add(jt2,null);
j5.setBounds(500,400,80,30);
j5.setForeground(new Color(255,0,0));
j5.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j5,null);
jt3.setBounds(570,400,100,30);
jt3.setFont(new java.awt.Font("Dialog",0,15));
cp.add(jt3,null);
j7.setBounds(140,440,500,30);
j7.setForeground(new Color(55,220,150));
j7.setFont(new java.awt.Font("华文仿宋",Font.BOLD,20));
cp.add(j7,null);
jsp.setBounds(6,0,540,305);
jsp.getViewport().add(jT1,null);
cp.add(jsp);
tp.setSize(760,500);
tp.setText("");
cp.add(tp,null);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs = null;
rs=ps.executeQuery("select * from 最高记录");
jT1.setModel(new javax.swing.table.DefaultTableModel());
Object[][] rowData=new Object[50][5];
String[] columnNames = {
"项目","成绩","创造者","创造者学院", "创造地点"};
JTable jT1 = new JTable(rowData, columnNames);
jsp.getViewport().add(jT1,null); //加载JTable对象
int i=0;
while(rs.next())
{
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2]=rs.getString(3);
rowData[i][3]=rs.getString(4);
rowData[i][4]=rs.getString(5);
i++;
}
cbx1.setSelectedItem(rowData[0][3]);
cbx2.setSelectedItem(rowData[0][0]);
jt1.setText((String)rowData[0][2]);
jt2.setText((String)rowData[0][1]);
jt3.setText((String)rowData[0][4]);
}catch (Exception err){err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接出错!");}
}
public static void main(String args[])
{new pj_record();}
public void itemStateChanged(ItemEvent e)
{JCheckBox cbx=(JCheckBox) e.getItem();
if(cbx.getText()=="学 院"){if(e.getStateChange()==ItemEvent.SELECTED) i1=1;else i1=0;}
if(cbx.getText()=="项 目"){if(e.getStateChange()==ItemEvent.SELECTED) i2=1;else i2=0;}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b6){this.dispose();}
else
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs=null;
if(e.getSource()==b1)
{
rs=ps.executeQuery("select * from 最高记录 where 项目='"+cbx2.getSelectedItem()+"'");
if(rs.next()){JOptionPane.showMessageDialog(null,"此项目记录已存在!");}
else
{
int v=JOptionPane.showConfirmDialog(this,"确认添加吗?","添加确认",JOptionPane.OK_CANCEL_OPTION);
if(jt1.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入创造者姓名!");jt1.requestFocus();}
else if(jt2.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入创造者姓名!");jt2.requestFocus();}
else if(v==JOptionPane.OK_OPTION)
{
ps.executeUpdate("Insert Into 最高记录 Values('"+cbx2.getSelectedItem().toString()+"','"+jt2.getText().trim()+"'" +
" ,'"+jt1.getText().trim()+"','"+cbx1.getSelectedItem().toString()+"','"+jt3.getText().trim()+"')");
JOptionPane.showMessageDialog(null,"添加成功!");
}
}
rs=ps.executeQuery("select * from 最高记录");
}
else if(e.getSource()==b2)
{
JOptionPane.showMessageDialog(null,"删除以项目为参考!");
int v=JOptionPane.showConfirmDialog(this,"确定删除吗?","删除确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION)
{ rs=ps.executeQuery("select * from 最高记录 where 项目='"+cbx2.getSelectedItem()+"'");
if(rs.next()){ps.executeUpdate("delete from 最高记录 where 项目='"+cbx2.getSelectedItem()+"'");
JOptionPane.showMessageDialog(null,"删除成功!");}
else{JOptionPane.showMessageDialog(null,"没有此记录!" );}
}
rs=ps.executeQuery("select * from 最高记录");
}
else if(e.getSource()==b3)
{
if(i1==1&&i2==0)
{
rs=ps.executeQuery("select * from 最高记录 where 创造者单位='"+
cbx1.getSelectedItem()+"'");
if(rs.next()){rs=ps.executeQuery("select * from 最高记录 where 创造者单位='"+
cbx1.getSelectedItem()+"'"); }
else{JOptionPane.showMessageDialog(null,"没有此记录!" );rs=ps.executeQuery("select * from 最高记录");}
}
else if(i1==0&&i2==1)
{
rs=ps.executeQuery("select * from 最高记录 where 项目='"+
cbx2.getSelectedItem()+"'");
if(rs.next())
{
rs=ps.executeQuery("select * from 最高记录 where 项目='"+
cbx2.getSelectedItem()+"'");
}
else{JOptionPane.showMessageDialog(null,"没有此记录!" );rs=ps.executeQuery("select * from 最高记录");}
}
else if(i1==1&&i2==1)
{
rs=ps.executeQuery("select * from 最高记录 where 创造者单位='"+
cbx1.getSelectedItem()+"'and 项目='"+cbx2.getSelectedItem()+"'");
if(rs.next())
{
rs=ps.executeQuery("select * from 最高记录 where 创造者单位='"+
cbx1.getSelectedItem()+"'and 项目='"+cbx2.getSelectedItem()+"'");
}
else{JOptionPane.showMessageDialog(null,"没有此记录!" );rs=ps.executeQuery("select * from 最高记录");}
}
else
{
if(jt1.getText().equals("")){JOptionPane.showMessageDialog(null, "请输入创造者姓名!");jt1.requestFocus();
rs=ps.executeQuery("select * from 最高记录");}
else{rs=ps.executeQuery("select * from 最高记录 where 创造者='"+jt1.getText().trim()+"' and 创造者单位='"+cbx1.getSelectedItem()+"'");
if(rs.next()){rs=ps.executeQuery("select * from 最高记录 where 创造者='"+jt1.getText().trim()+"'and 创造者单位='"+cbx1.getSelectedItem()+"'");}
else{JOptionPane.showMessageDialog(null,"没有此记录!" );rs=ps.executeQuery("select * from 最高记录");}
}
}
}
else if(e.getSource()==b4)
{
int v=JOptionPane.showConfirmDialog(this,"确定修改吗?","修改确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION)
{ ps.executeUpdate("Update 最高记录 set 创造者='" + jt1.getText().trim() +
"' ,成绩='" + jt2.getText().trim() +
"',创造者单位='"+cbx1.getSelectedItem().toString()+"',创造地点='"+jt3.getText().trim()+"'where 项目='"+cbx2.getSelectedItem().toString()+"'");
JOptionPane.showMessageDialog(null,"修改成功!" ); }
rs=ps.executeQuery("select * from 最高记录");
}
else {rs=ps.executeQuery("select * from 最高记录");}
jT1.setModel(new javax.swing.table.DefaultTableModel());
Object[][] rowData=new Object[50][5];
String[] columnNames = {
"项目","成绩","创造者","创造者学院", "创造地点"};
JTable jT1=new JTable(rowData, columnNames); //创建新的JTable对象
jsp.getViewport().add(jT1,null); //加载JTable对象
int i=0;
while(rs.next())
{
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2]=rs.getString(3);
rowData[i][3]=rs.getString(4);
rowData[i][4]=rs.getString(5);
i++;
}
cbx1.setSelectedItem(rowData[0][3]);
cbx2.setSelectedItem(rowData[0][0]);
jt1.setText((String)rowData[0][2]);
jt2.setText((String)rowData[0][1]);
jt3.setText((String)rowData[0][4]);
}catch(Exception err){err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接错误!");}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -