public class Select_studentnoPanel extends JPanel {
public Select_studentnoPanel ()
{
try{
jbInit();
}catch(Exception exception)
{
exception.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setLayout(null);
this.setBackground(new Color(168,233,216));
button1.setBounds(new Rectangle(314,36,81,23));
button1.setText("退出");
button1.addActionListener(new Exit(this));
label1.setText("学生姓名");
label1.setBounds(new Rectangle(25,38,71,15));
button2.setBounds(new Rectangle(246,38,64,23));
button2.setText("查询");
button2.addActionListener(new Select_no(this));
tsno.setBounds(new Rectangle(108,38,126,20));
list.setBounds(new Rectangle(20,84,442,70));
this.add(button1);
this.add(label1);
this.add(button2);
this.add(tsno);
this.add(list);
this.setVisible(true);
this.setSize(600,600);
}
JButton button1=new JButton();
JButton button2=new JButton();
JLabel label1=new JLabel();
JTextField tsno=new JTextField();
JList list=new JList();
public void button1_actionPerformed(ActionEvent e)
{
this.setVisible(false);
}public void button2_actionPerformed(ActionEvent e)
{
String sno=tsno.getText().trim();
GradeManager sm=new GradeManager();
Vector v=sm.findStudent(sno);
list.setListData(v);
}
}
class Select_no implements ActionListener
{
private Select_studentnoPanel adaptee;
Select_no(Select_studentnoPanel adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.button2_actionPerformed(e);
}
}
class Exit implements ActionListener
{
private Select_studentnoPanel adaptee;
Exit(Select_studentnoPanel adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.button1_actionPerformed(e);
}
}
public Select_studentnoPanel ()
{
try{
jbInit();
}catch(Exception exception)
{
exception.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setLayout(null);
this.setBackground(new Color(168,233,216));
button1.setBounds(new Rectangle(314,36,81,23));
button1.setText("退出");
button1.addActionListener(new Exit(this));
label1.setText("学生姓名");
label1.setBounds(new Rectangle(25,38,71,15));
button2.setBounds(new Rectangle(246,38,64,23));
button2.setText("查询");
button2.addActionListener(new Select_no(this));
tsno.setBounds(new Rectangle(108,38,126,20));
list.setBounds(new Rectangle(20,84,442,70));
this.add(button1);
this.add(label1);
this.add(button2);
this.add(tsno);
this.add(list);
this.setVisible(true);
this.setSize(600,600);
}
JButton button1=new JButton();
JButton button2=new JButton();
JLabel label1=new JLabel();
JTextField tsno=new JTextField();
JList list=new JList();
public void button1_actionPerformed(ActionEvent e)
{
this.setVisible(false);
}public void button2_actionPerformed(ActionEvent e)
{
String sno=tsno.getText().trim();
GradeManager sm=new GradeManager();
Vector v=sm.findStudent(sno);
list.setListData(v);
}
}
class Select_no implements ActionListener
{
private Select_studentnoPanel adaptee;
Select_no(Select_studentnoPanel adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.button2_actionPerformed(e);
}
}
class Exit implements ActionListener
{
private Select_studentnoPanel adaptee;
Exit(Select_studentnoPanel adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.button1_actionPerformed(e);
}
}
public int addStudent(Student s)
{
int i=0;
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException ce)
{System.out.println("SQLException:"+ce.getMessage());}
try
{
String url = "jdbc:odbc:class8_10";
String user = "";
String password = "";
Connection con=DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sqlstr="insert into student values('"+s.getSno()+"','"+s.getSname()+"','"+s.getSclass()+"','"+s.getSgrade()+"')";
i=stmt.executeUpdate(sqlstr);
stmt.close();
con.close();
}
catch(SQLException e)
{System.out.println("SQLException:"+e.getMessage());}
return i;
}
public int deleteStudent(Student sno)
{
int i=0;
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException ce)
{System.out.println("SQLException:"+ce.getMessage());}
try
{
String url = "jdbc:odbc:class8_10";
String user = "";
String password = "";
Connection con=DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sqlstr="delete from student where Sno='"+sno+"'";
i=stmt.executeUpdate(sqlstr);
stmt.close();
con.close();
}
catch(SQLException e)
{System.out.println("SQLException:"+e.getMessage());}
return i;
}
public int updateStudent(String sno,String sname,String sclass,String sgrade)
{
int i=0;
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException ce)
{System.out.println("SQLException:"+ce.getMessage());}
try
{
String url = "jdbc:odbc:class8_10";
String user = "";
String password = "";
Connection con=DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
String sqlstr="update student set sname='"+sname+"',sclass='"+sclass+"','"+sgrade+"'";
i=stmt.executeUpdate(sqlstr);
stmt.close();
con.close();
}
catch(SQLException e)
{System.out.println("SQLException:"+e.getMessage());}
return i;
}
public Vector findStudent(String sno){
Vector s=null;
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException ce)
{System.out.println("SQLException:"+ce.getMessage());}
try
{
String url = "jdbc:odbc:class8_10";
String user = "";
String password = "";
Connection con=DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student where sno='"+sno+"'");
while(rs.next())
{
System.out.println(
"学号"+rs.getString(1)+"\t"+
"姓名"+rs.getString(2)+"\t"+
"班级 "+rs.getString(3)+"\t"+
"成绩"+rs.getFloat(4)+"\t");
}
stmt.close();
con.close();
}
catch(SQLException e)
{System.out.println("SQLException:"+e.getMessage());}
return s;
}
}