📄 courseframe.java
字号:
package com.studentFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.sql.*;
/*课程信息管理窗口*/
public class CourseFrame extends JFrame implements ItemListener,ActionListener
{
String s = " 课程名 性质 教师 学时 学分";
Label prompt;
Choice term; //学期的下拉列表框
Panel p;
TextArea ta;
JButton inquire;
String tempterm = "2005-1"; //默认学期为2005-1
String id;
CourseFrame(String ID)
{
id = ID;
prompt = new Label("请选择查询学期");
term = new Choice();
term.add("2005-1");
term.add("2005-2");
term.add("2006-1");
term.add("2006-2");
inquire = new JButton("查询");
p = new Panel();
p.add(prompt);
p.add(term);
p.add(inquire);
ta = new TextArea();
ta.setEditable(false);
ta.setFont(new Font("TimesRoman",Font.BOLD,12));
this.getContentPane().add(p,BorderLayout.NORTH);
this.getContentPane().add(ta,BorderLayout.CENTER);
term.addItemListener(this);
inquire.addActionListener(this);
setSize(500,300);
setLocation(300,300);
setVisible(true);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String url="jdbc:odbc:javadbc";
String username="";
String password="";
Connection con =null;
Statement stmt = null;
ResultSet rs = null;
String sql,result;
boolean hasrecord = false; //标志是否有记录
ta.append("课程名 课程性质 学时 学分 教师 地点 \n");
try{
/*连接数据库*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
sql = "select course.cname,property,studytime,credit,teacher,place from "
+"course,cschedule where course.cname=cschedule.cname and course.term='"
+tempterm+"' and cschedule.grade in"+"(select grade from student where id ='"
+id+"')";
rs = stmt.executeQuery(sql);
while(rs.next())
{
/*显示查询到的课程信息*/
result = rs.getString(1)+" "+rs.getString(2)+" "+rs.getInt(3)+
" "+rs.getFloat(4)+" "+rs.getString(5)+" "+rs.getString(6);
/*将查询的课程信息添加到文本域*/
ta.append(result+"\n");
hasrecord = true;
}
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
catch(Exception e1) {}
/*关闭数据库连接*/
finally{
try
{
if(stmt!=null)
stmt.close();
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
if(!hasrecord)
JOptionPane.showMessageDialog(null,"暂无课程");
inquire.setEnabled(false);
}
/*判断查询的学期*/
public void itemStateChanged(ItemEvent e1)
{
Choice temp;
if(e1.getItemSelectable() instanceof Choice)
{
temp = (Choice)(e1.getItemSelectable());
tempterm = temp.getSelectedItem();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -