1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
package com.servlet.test;
// You can use this sample servlet as a starting point to connect to
// your database included with useractive account.
//
// Portions copied from Sun's HelloWorldServlet.java
// You are free to use this code in any way you chose
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public
class HelloWorldServlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
try {
// The newInstance() call is a work around for some
// broken Java implementations
// out.println("asdf");
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E) {
out.println("Unable to load driver.");
E.printStackTrace();
}
out.println("<br><hr>");
try {
Connection Conn = DriverManager.getConnection("jdbc:mysql://192.168.0.2:3306/exemple", "root" , "");
// Do something with the Connection
Statement Stmt = Conn.createStatement();
ResultSet RS = Stmt.executeQuery("SELECT * from etudiant");
while (RS.next()) {
out.println(RS.getString("id_e"));
out.println("<br>");
out.println(RS.getString("nom_e"));
out.println("<br>");
out.println(RS.getString("prenom_e"));
out.println("<br>");
out.println("----------------------<br>");
}
// Clean up after ourselves
RS.close();
Stmt.close();
Conn.close();
}
catch (SQLException E) {
out.println("SQLException: " + E.getMessage());
out.println("SQLState: " + E.getSQLState());
out.println("VendorError: " + E.getErrorCode());
}
out.println("<h1>Hello World. Sun... 1.4</h1>");
out.println("</body></html>");
}
} |
Partager