package a3;

import java.sql.*;
public class DataBaseInquire
{  String keyword;
   String name;
   public DataBaseInquire()
    {keyword="";
     name="";
       try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          }
       catch(ClassNotFoundException e){}
    }
   public void setKeyword(String s)
    {keyword=s;
      try{byte b[]=keyword.getBytes("ISO-8859-1");
          keyword=new String(b);
         }
      catch(Exception e)
         { }
    }
   
   public void setName(String s)
   {name=s;
     try{byte b[]=name.getBytes("ISO-8859-1");
     name=new String(b);
        }
     catch(Exception e)
        { }
   }
   
  public StringBuffer byKeywordInquire()
    { String number,xingming;
      Connection con=null;
      Statement sql=null;
      ResultSet rs=null;
      StringBuffer buffer=new StringBuffer();  
      int math,english,physics;
      try{ con=DriverManager.getConnection("jdbc:odbc:sun","sa","");
           sql=con.createStatement();
           String condition="SELECT * FROM student WHERE xuehao = "+"'"+keyword+"' or xingming='"+name+"'";
      
           rs=sql.executeQuery(condition);
            buffer.append("<Table Border>");
            buffer.append("<TR>");
            buffer.append("<TH width=100>"+"学号");
            buffer.append("<TH width=100>"+"姓名");
            buffer.append("<TH width=50>"+"数学成绩");
            buffer.append("<TH width=50>"+"英语成绩");
            buffer.append("<TH width=50>"+"物理成绩");
            buffer.append("</TR>");
        while(rs.next())
           { buffer.append("<TR>");
             number=rs.getString(1); 
             buffer.append("<TD >"+number+"</TD>"); 
             xingming=rs.getString(2); 
             buffer.append("<TD >"+xingming+"</TD>");
             math=rs.getInt("sx"); 
             buffer.append("<TD >"+math+"</TD>"); 
             english=rs.getInt("yy"); 
             buffer.append("<TD >"+english+"</TD>");
             physics=rs.getInt("wl"); 
             buffer.append("<TD >"+physics+"</TD>");   
             buffer.append("</TR>") ;          
            }
          buffer.append("</Table>");
          con.close();
          return buffer;
      }
    catch(SQLException e)
          {return new StringBuffer("无法建立查询"+e);
          }
   } 
  
  
}
