[Inserer une nouvelle donnée] - [ Consulter la liste des données]

JSP - MYSQL: RECHERCHE AVEC DES REQUETES MYSQL

<%@ page errorPage="error.jsp" import="java.sql.*" %> <%! // --------------- inits for the servlet -------------- // The database connection Connection con; // The statement Statement stmt; // The queryString String queryString = null; // ---- configure this for your site String username = "nobody"; String password = null; // The URL that will connect to TECFA's MySQL server // Syntax: jdbc:TYPE:machine:port/DB_NAME // String url = "jdbc:mysql://localhost:3306/staf2x"; String url = "jdbc:mysql://tecfa2.unige.ch:3306/staf2x"; %> <% // --------------- code for the service method -------------- // Let's see if we got a request queryString = request.getParameter ("QUERYSTRING"); if ((queryString != "") && (queryString != null)) { try { Class.forName("org.gjt.mm.mysql.Driver"); // Establish Connection to the database at URL with usename and password con = DriverManager.getConnection(url, username, password); } catch (Exception e) // (ClassNotFoundException and SQLException) { throw(new UnavailableException(this, "Désolée! La Database n'a pas marché!")); } try { out.println("

Résultats de votre requête

"); out.println(""); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(queryString); ResultSetMetaData rsMeta = rs.getMetaData(); // Get the N of Cols in the ResultSet int noCols = rsMeta.getColumnCount(); out.println(""); for (int c=1; c<=noCols; c++) { String el = rsMeta.getColumnLabel(c); out.println(""); } out.println(""); while (rs.next()) { out.println(""); for (int c=1; c<=noCols; c++) { String el = rs.getString(c); out.println(""); } out.println(""); } out.println("
" + el + "
" + el + "
"); } catch (SQLException ex ) { out.println ( "

" );

while (ex != null) {

out.println("Message: " + ex.getMessage ());

out.println("SQLState: " + ex.getSQLState ());

out.println("ErrorCode: " + ex.getErrorCode ());

ex = ex.getNextException();

out.println("");

}

out.println ( "

" ); } } %>

Votre requête:


Exemples pour faire vos requêtes:

Pour afficher toutes les données, tapez:

select * from scribante1
Pour afficher les données dont le code postal est supérieur à 1200, tapez:
select * from scribante1 where np > 1200
Pour afficher toutes les noms avec leurs numéros de téléphones, tapez:
select nom, tel, portable from scribante1


Vanessa Scribante - avril 2000
Encore une fois Source