With this function we can save a serialized class into a MySQL's table.
/**
* Actualitza l'objecte serialitzat (scorm_structure) de
* l'usuari amb l'identificador indicat.
*
* Nota! Fa un update, per tant ha d'existir l'usuari!
*
* @param courseStudentName : El nom de la taula en que ho guardem.
* @param studentId : L'identificador de l'usuari.(int Type)
* @param object : Retornarà l'UserObjective, caldrà fer un cast.
* @return int Type: Retornarà l'identificador del resultset.
* @throws Exception : Llança una excepció si hi ha un error.
*/
private int writeJavaObject(
final String courseStudentName,
final int studentId,
String sqlUpdateScormStructure =
"UPDATE " + courseStudentName
+ " set scorm_structure = ? WHERE student_id = ?";
/** We already have
* private Connection conn = DriverManager.getConnection(url, userName, password);
*/
conn.prepareStatement(sqlUpdateScormStructure);
oout.writeObject(object); //serializing
// set input parameters
pstmt.setBytes(1, baos.toByteArray());
pstmt.setInt(2, studentId);
pstmt.executeUpdate();
// get the generated key for the id
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
rs.close();
pstmt.close();
return id;
}
I'm not loading a .jar library, I'm only loading a .class, which implements my library functions.
To test this code you must have in the directory D:\LoadLibrary a TestHelloWorld.class and the interface TestString WITH THE SAME package that you have in the main code. It means that if your TestString in your main code is com.haduart.TestString you must put your TestString.class in the D:\LoadLibrary\com\haduart\TestString.class.
/**
* In the D:/LoadLibrary we must have .class and the same interface (TestString) with the same pacakge as the
* main program.
*/
URL urlToJar =
new URL("file://D:/LoadLibrary/");
System.
out.
println("loading...");
Class c2 = cl.loadClass("TestHelloWorld");
System.
out.
println("loaded!");
Object nouObject = c2.
newInstance();
TestString ts = (TestString) cons.newInstance(args);
/**
* Now we can use the "ts" object as a normal TestString interface. No matter which is the implementation that is begin it.
*/
System.
out.
println("S1: " + ts.
printS1());
System.
out.
println("S2: " + ts.
printS2());
Vim peut indenter automatiquement du code source (c, java, ...)
Sélectionner la partie du code source à traiter en mode visuel (v)
Appuyer sur la touche =