📄 jdbc.java~5~
字号:
bt2.setBackground(Color.pink);
bt2.setFont(new Font("宋体",Font.BOLD,12));
lb1.setBounds(80,80,120,30);
tf1.setBounds(200,80,80,30);
bt1.setBounds(80,130,100,30);
bt2.setBounds(180,130,100,30);
con.add(lb1);con.add(tf1);
con.add(bt1);con.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
//输入学生姓名查询
Database DB=new Database();
Database DB1=new Database();
int tem;
String temp[]=new String[6];//存放单个学生各种信息
StringBuffer mess=new StringBuffer();//存放所有查询的结果
if(e.getSource()==bt1)
{
try
{
String sql="select * from chengjibiao WHERE snum= '"+tf1.getText()+"'";
DB.OpenConn();
DB.rs=DB.executeQuery(sql);
DB1.OpenConn();
while(DB.rs.next())//防止重名的情况出现
{
String sql1="select * from fenshu WHERE snum="+DB.rs.getInt(1)+"";
tem=DB.rs.getInt(1);
temp[0]=DB.rs.getString(2);
temp[1]=DB.rs.getString(3);
mess.append("查找到"+tf1.getText()+"学生信息"+"\n"+"学号:"+tem+"\n"+"姓名:"+temp[0]+"\n"+"生日:"+temp[1]+"\n");//基本信息
DB1.rs=DB1.executeQuery(sql1);
while(DB1.rs.next())//显示各科分数
{
mess.append(""+DB1.rs.getString(2)+":"+DB1.rs.getInt(3)+"\n");
}
}
if(mess.length()==0)
JOptionPane.showMessageDialog(this,"未找到信息","学生查询对话框",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(this,mess,"学生查询对话框",JOptionPane.INFORMATION_MESSAGE);
tf1.setText(null);
}
catch(Exception ex)
{
System.out.println(ex);
JOptionPane.showMessageDialog(this,"未找到信息","学生查找对话框",JOptionPane.INFORMATION_MESSAGE);
}
finally
{
DB.closeStmt();
DB.closeConn();
DB1.closeStmt();
DB1.closeConn();
}
}
if(e.getSource()==bt2)
setVisible(false);
}
}
//学生分数录入
class fslr extends JFrame implements ActionListener
{
JLabel lb1,lb2,lb3;
JTextField tf1,tf2,tf3;
JButton bt1,bt2;
fslr()
{
setTitle("学生分数录入");
this.getContentPane().setLayout(null);
lb1=new JLabel("学号");
tf1=new JTextField(10);
lb2=new JLabel("课程");
tf2=new JTextField(10);
lb3=new JLabel("分数");
tf3=new JTextField(10);
bt1=new JButton("插入");
bt2=new JButton("取消");
lb1.setBackground(Color.cyan);
lb1.setFont(new Font("宋体",Font.BOLD,12));
lb2.setBackground(Color.cyan);
lb2.setFont(new Font("宋体",Font.BOLD,12));
lb3.setBackground(Color.cyan);
lb3.setFont(new Font("宋体",Font.BOLD,12));
tf1.setBackground(Color.pink);
tf1.setFont(new Font("宋体",Font.BOLD,12));
tf2.setBackground(Color.pink);
tf2.setFont(new Font("宋体",Font.BOLD,12));
tf3.setBackground(Color.pink);
tf3.setFont(new Font("宋体",Font.BOLD,12));
bt1.setBackground(Color.green);
bt1.setFont(new Font("楷书",Font.BOLD,12));
bt2.setBackground(Color.green);
bt2.setFont(new Font("楷书",Font.BOLD,12));
lb1.setBounds(0,40,100,30);tf1.setBounds(100,40,100,30);
lb2.setBounds(0,70,100,30);tf2.setBounds(100,70,100,30);
lb3.setBounds(0,100,100,30);tf3.setBounds(100,100,100,30);
bt1.setBounds(30,220,80,30);bt2.setBounds(120,220,80,30);
Container con=this.getContentPane();
con.add(lb1);con.add(tf1);
con.add(lb2);con.add(tf2);
con.add(lb3);con.add(tf3);
con.add(bt1);con.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
Database DB=new Database();
if(e.getSource()==bt1)
{
try
{
// DB.OpenConn();
// ResultSet rs1=DB.executeQuery("select snum from chengjibiao WHERE snum="+Integer.parseInt(tf1.getText())+""),
// rs2=DB.executeQuery("select cname from kecheng WHERE cname='"+tf2.getText()+"'"),
// rs3=DB.executeQuery("select snum from fenshu WHERE snum="+Integer.parseInt(tf1.getText())+"and cname='"+tf2.getText()+"'");//是否存在学生和课程
// if(rs3.next())
// {
// JOptionPane.showMessageDialog(this,"分数已经存在!!","学生分数录入对话框",JOptionPane.INFORMATION_MESSAGE);
// tf1.setText(null);tf2.setText(null);tf3.setText(null);&& rs1.next() && rs2.next()
// }
if(tf1.getText().length()!=0 && tf2.getText().length()!=0 && tf3.getText().length()!=0 )
{ String s=new String();
String sql="insert into fenshu values("+Integer.parseInt(tf1.getText())+
",'"+tf2.getText()+"',"+Integer.parseInt(tf3.getText())+")";
DB.OpenConn();
DB.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"学生分数录入成功!!","学生分数录入对话框",JOptionPane.INFORMATION_MESSAGE);
tf1.setText(null);tf2.setText(null);tf3.setText(null);
}
else
JOptionPane.showMessageDialog(this,"输入基本信息错误!!","学生分数录入对话框",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex)
{
System.out.println(e);
JOptionPane.showMessageDialog(this,"未找录入学生的成绩","学生分数录入对话框",JOptionPane.INFORMATION_MESSAGE);
}
finally
{
DB.closeStmt();
DB.closeConn();
}
}
if(e.getSource()==bt2)
setVisible(false);
}
}
//课程添加
class kctj extends JFrame implements ActionListener
{
JLabel lb1,lb2,lb3;
JTextField tf1,tf2,tf3;
JButton bt1,bt2;
kctj()
{
setTitle("课程信息添加");
this.getContentPane().setLayout(null);
lb1=new JLabel("课程名");
tf1=new JTextField(10);
lb2=new JLabel("学分");
tf2=new JTextField(10);
lb3=new JLabel("学时");
tf3=new JTextField(10);
bt1=new JButton("插入");
bt2=new JButton("取消");
lb1.setBackground(Color.cyan);
lb1.setFont(new Font("宋体",Font.BOLD,16));
lb2.setBackground(Color.cyan);
lb2.setFont(new Font("宋体",Font.BOLD,16));
lb3.setBackground(Color.cyan);
lb3.setFont(new Font("宋体",Font.BOLD,16));
tf1.setBackground(Color.pink);
tf1.setFont(new Font("宋体",Font.BOLD,16));
tf2.setBackground(Color.pink);
tf2.setFont(new Font("宋体",Font.BOLD,16));
tf3.setBackground(Color.pink);
tf3.setFont(new Font("宋体",Font.BOLD,16));
bt1.setBackground(Color.green);
bt1.setFont(new Font("楷书",Font.BOLD,16));
bt2.setBackground(Color.green);
bt2.setFont(new Font("楷书",Font.BOLD,16));
lb1.setBounds(0,40,100,30);tf1.setBounds(100,40,100,30);
lb2.setBounds(0,70,100,30);tf2.setBounds(100,70,100,30);
lb3.setBounds(0,100,100,30);tf3.setBounds(100,100,100,30);
bt1.setBounds(0,130,100,30);bt2.setBounds(130,130,100,30);
Container con=this.getContentPane();
con.add(lb1);con.add(tf1);
con.add(lb2);con.add(tf2);
con.add(lb3);con.add(tf3);
con.add(bt1);con.add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
Database DB=new Database();
if(e.getSource()==bt1)
{
try
{
if(tf1.getText().length()!=0 && tf2.getText().length()!=0 && tf3.getText().length()!=0)
{
String sql="insert into kecheng values('"+tf1.getText()+"',"
+Integer.parseInt(tf2.getText())+","+Integer.parseInt(tf3.getText())+")";
DB.OpenConn();
DB.executeUpdate(sql);
String s=new String();
JOptionPane.showMessageDialog(this,"课程信息添加成功!!","课程信息添加对话框",JOptionPane.INFORMATION_MESSAGE);
tf1.setText(null);tf2.setText(null);tf3.setText(null);
}
else
{
JOptionPane.showMessageDialog(this,"输入基本信息错误!!","课程信息添加对话框",JOptionPane.INFORMATION_MESSAGE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -