Hola abdel rahman!!!
Ese código de ejemplo deberia funcionar tanto en .java com en jsp, como en servlets. De todos modos ahi te paso un ejemplo completo (InsertExample.java) para que te situes un poquito mas.
Código PHP:
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public final class InsertExample {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
/* Get a connection. */
String driverClassName = "com.mysql.jdbc.Driver";
String driverUrl = "jdbc:mysql://localhost/test";
String user = "";
String password = "";
Class.forName(driverClassName);
connection = DriverManager.getConnection(driverUrl, user,
password);
/* Create data for some accounts. */
String[] accountIdentifiers = new String[]{"pruebas-1",
"pruebas-2", "" + "pruebas-3"};
double[] balances = new double[]{100.0, 200.0, 300.0};
/* Create "statement". */
statement = connection.createStatement();
/* Insert the accounts in database. */
for (int i = 0; i < accountIdentifiers.length; i++) {
/* Execute query. */
String queryString = "INSERT INTO TutAccount " +
"(accId, balance) VALUES ('" +
accountIdentifiers[i] + "', " + balances[i] + ")";
int insertedRows = statement.executeUpdate(queryString);
if (insertedRows != 1) {
throw new SQLException(accountIdentifiers[i] +
": problems when inserting !!!!");
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} // try
} // main
} // class
Un saludo,
kripton