package OAD;
import OAD.*;
import java.sql.*;
public class lugeja{
	
	public String populateOptions(String tabel, String user, String pass){
		Connection cn;
		Statement st;
		ResultSet rs;
		String tagasi="";
		try{
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
    	cn=DriverManager.getConnection("jdbc:odbc:plaadikogu", user, pass);
    	st=cn.createStatement();
   	}catch(Exception e){return "Tekkis viga: "+e;}
   	try{
   		st=cn.createStatement();
   		String lause="SELECT * FROM "+tabel;
   		rs=st.executeQuery(lause);
   		while(rs.next()!=false){
   			tagasi=tagasi.concat("<option value='"+rs.getString(1)+"'>"+rs.getString(2)+"</option>");
   		}
   	}catch(Exception e){return "Tekkis viga: "+e;}
   	try{cn.close();}catch(Exception e){return "Tekkis viga: "+e;}
   	return tagasi;
	}
	
	public String populateChangeForm(String tabel, String esit, String user, String pass){
		Connection cn;
		Statement st;
		ResultSet rs;
		ResultSetMetaData rsmd;
		String tagasi="";
		try{
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
    	cn=DriverManager.getConnection("jdbc:odbc:plaadikogu", user, pass);
    	st=cn.createStatement();
   	}catch(Exception e){return "Tekkis viga: "+e;}
   	try{
   		st=cn.createStatement();
   		String lause="SELECT * FROM "+tabel+" WHERE ID="+esit;
   		rs=st.executeQuery(lause);
   		rs.next();
   		rsmd=rs.getMetaData();
   		tagasi="<form><table>";
   		for (int i=1; i<=rsmd.getColumnCount(); i++){
   			tagasi=tagasi.concat("<tr><td>"+rsmd.getColumnName(i)+"</td><td<input type='text' name='"+rsmd.getColumnName(i)+"' value='"+rs.getString(rsmd.getColumnName(i))+"'></td></tr>");	
   		}
   		tagasi=tagasi.concat("<tr><td>Kustuta:</td><td><input type=checkbox name='del'></td></tr><tr><td><input type=submit value='MUUDA'></td></tr>");
   		tagasi=tagasi.concat("</table></form>");
   	}catch(Exception e){return "Tekkis viga: "+e;}
   	try{cn.close();}catch(Exception e){return "Tekkis viga: "+e;}
   	return tagasi;
	}	
}