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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| public class Formul extends HttpServlet implements Servlet {
Connection conn = null;
private String url;
private String user;
private String mdp="";
private Statement stmt;
private ResultSet rs;
public Formul() throws Exception {
super();
try {
// Load the jdbc driver for MySQL
Class.forName("com.mysql.jdbc.Driver");
//Get a connection to the database source using Thin JDBC driver
String url = "jdbc:mysql://localhost:3306/application";
try {
conn = DriverManager.getConnection(url, "root", "");
System.out.println ("connexion base orcl etablie");
} catch (SQLException e) {
System.out.println ("pas de connexion");
e.printStackTrace();
}
stmt = conn.createStatement();
rs= stmt.executeQuery("SELECT * FROM contact");
while(rs.next()){
System.out.print("connexion");
//imprime les éléments du tuple
int id = rs.getInt("ID");
String lenom = rs.getString("FIRSTNAME");
String lepom = rs.getString("LASTNAME");
String leoom = rs.getString("EMAIL");
System.out.println("nom:" + id + "nom" + lenom + "nom" + lepom + "nom" + leoom);
}
System.out.println();
// Print stdout warning messages if necessary
checkForSQLWarnings(conn.getWarnings());
// Print stdout info messages
printInfo(conn);
}
catch(SQLException e) {
System.err.println("\n*** SQLException caught in LoadDriver()");
printSQLErrors(e);
throw e;
}
try {
conn.close();
rs.close();
stmt.close();
System.out.println("Disconnecting ...");
}
catch(Exception e) {
System.err.println("\n*** Exception caught in close()");
throw e;
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
private void printInfo(Connection c) throws Exception
{
// Get meta-data about the database
DatabaseMetaData info = c.getMetaData();
System.out.println("\nConnected to :\t" + info.getURL());
System.out.println("Driver :\t" + info.getDriverName());
System.out.println("Version :\t" + info.getDriverVersion());
}
/**
* Print stdout all pending SQLWarning warnings
*/
private boolean checkForSQLWarnings(SQLWarning w)
throws SQLException
{
boolean warning = false;
if(w != null) {
warning = true;
System.out.println("\n**** Warning ****\n");
while(w != null) {
System.out.println("SQLState: " + w.getSQLState());
System.out.println("Message: " + w.getMessage());
System.out.println("Vendor: " + w.getErrorCode());
System.out.println("");
w = w.getNextWarning();
}
}
return warning;
}
/**
*Print stderr all pending SQLException exceptions
*/
private void printSQLErrors(SQLException e)
{
while(e != null) {
System.err.println("SQLState: " + e.getSQLState());
System.err.println("Message: " + e.getMessage());
System.err.println("Vendor: " + e.getErrorCode());
System.err.println("");
e = e.getNextException();
}
}
} |
Partager