import java.awt.*;
import java.sql.*;
import java.io.*;
public class Genera
{
static BufferedWriter myOutput;
public static void main(String[] args) throws IOException{
String titolo = " ";
String commento = " ";
int id=0;
int nd =0;
int nr =0;
int nl = Integer.parseInt(args[0]);
int nt = Integer.parseInt(args[1]);
int rc =0;
try {
// Load the Mysql JDBC driver
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
System.out.println ("Mysql device driver does not exist");
System.exit(1);
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:mysql://localhost/Istogrammi?user=&password=");
// Create a Statement
Statement stmt = conn.createStatement ();
File outputFile = new File("test1.php3");
myOutput = new BufferedWriter(new FileWriter(outputFile));
ResultSet rset = stmt.executeQuery (" select Titolo,Commento,Num_domande,Risposta_corretta,ID FROM test WHERE Num_lez="+nl+" AND Num_test="+nt );
titolo =rset.getString (1);
commento=rset.getString (2);
nd = rset.getInt(3);
rc = rset.getInt(4);
id = rset.getInt(5);
String testo[]= new String[nd];
for(int j= 1; j<(nd+1);j++){
ResultSet rset1 = stmt.executeQuery (" select Testo FROM domande WHERE id_test="+id+" AND num_test="+j );
testo[j-1] =rset1.getString (1);
}
myOutput.write("\n$hostname = \"localhost\";\n $password = \"\"; $user = \"\";\n");
myOutput.write("$num_lez="+nl+";\n $num_test="+nt+";\n $num_computer = 15;\n$num_domande = "+nd+";\n $titolo =\""+titolo+"\";\n $commento = \""+commento+"\";\n");
for(int j= 1; j<(nd+1);j++){
myOutput.write(
"$domanda["+j+"] = \""+testo[j-1]+"\";\n");
}
myOutput.write(
"mysql_pconnect($hosthame,$user,$password) or die(\"Unable to connect to SQL server\");\n mysql_select_db(\"Istogrammi\") or die(\"Unable to select database\");\necho \"
Quiz $num_test della lezione $num_lez : $titolo
$commento\";\n ?>");
myOutput.write(
"
\n \n if ($submit) {\n $result = mysql_query(\"INSERT INTO istogrammi (Num_test,Num_lez,Risposta,Nome_computer) VALUES\n ($num_test,$num_lez,$quiz1,'$REMOTE_ADDR')\");\n }\n if ($submit || $guarda) {\n $total = 0;\n echo \"\\n\";\n echo \"| Domanda | Num. risposte | \\n\";");
myOutput.write(
"for ($i=1;$i<($num_domande+1);$i++) {\n $result = mysql_query(\"Select ID from istogrammi WHERE Risposta=$i AND\n Num_test=$num_test AND Num_lez=$num_lez\");\n $r[i] = mysql_numrows($result);\n echo \"
|---|
| $i | $r[i] | \\n\";\n $total=$total+$r[i];\n }\n echo \" | TOTALE | $total | \\n\";\n echo \" | | \\n\";\n ");
myOutput.write(
"echo \"| Computer | Num. risposte | Computer | Num. risposte | \\n\";for ($i=1;$i<($num_computer+1);$i++) {\n $result = mysql_query(\"Select Nome,Indirizzo from computer WHERE ID=$i\");\n $nomec = mysql_result($result,0,\"Nome\");\n $indc = mysql_result($result,0,\"Indirizzo\");\n $result = mysql_query(\"Select ID from istogrammi WHERE Nome_computer='$indc' AND Num_test=$num_test AND Num_lez=$num_lez\");\n $r[$i] = mysql_numrows($result);\n $nome[$i] = $nomec;\n } echo \"
|---|
| $nome[15] | $r[15] | | | \\n\"; for ($i=1;$i<($num_computer+1-7);$i++) { $i1 = 15 - $i; echo \" | $nome[$i1] | $r[$i1] | $nome[$i] | $r[$i] | \\n\"; } echo \" |
\\n\"; }\n ?>\n
");
myOutput.write(
"
\n ");
myOutput.write(
"
Pagina curata da Giuseppe Zito: zito@ba.infn.it
ultimo aggiornamento: \n");
myOutput.write(
"
\n");
myOutput.close();
stmt.close();
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error accessing DB ");
System.out.println(" Error code is : "+e.getErrorCode());
System.out.println(" Error message is :"+e.getMessage());
}
}
}