OVH Cloud OVH Cloud

Mysql

3 réponses
Avatar
Craps
Je cherche un exemple simple de code java ayant acces a une base de donnee
mysql

J'ai telechargé les librairies mysql mais je ne sais pas comment l'utiliser
si quelqu'un a des exemples de code je suis preneur ou un site web clair

Merci

Craps

3 réponses

Avatar
euler
Craps wrote:

Je cherche un exemple simple de code java ayant acces a une base de donnee
mysql

J'ai telechargé les librairies mysql mais je ne sais pas comment
l'utiliser si quelqu'un a des exemples de code je suis preneur ou un site
web clair

Merci

Craps


Voici un petit exemple très simple (pas optimisé pour 2 ronds cela dit),
mais qui devrait t'aider à utiliser une base de donnée mysql. Il faudra
juste penser à faire les adaption nécessaires, càd remplacer les ????? par
les username et password correspondant à ton utilisateur et aussi remplacer
la requête SQL pour avoir qqchose correspondant à ta propre base et tes
propres tables.

Bien entendu, cet exemple ne fonctionne que si tu as un démon mysqld qui
tourne, sinon pas de connexion possible

//-----------------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.sql.Statement;
import java.sql.ResultSet;

public class TestMySQLAccess {

public static void main(String[] args)
{
Statement stmt = null;
ResultSet rs = null;

try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println ("jdbc driver sucessfully created....");
} catch (Exception ex) {
// handle the error
}

try {
Connection conn DriverManager.getConnection("jdbc:mysql://localhost/anonymity?user=?????&password=f?????");

stmt = conn.createStatement();
if (stmt.execute("SELECT * FROM Person")) {
rs = stmt.getResultSet();
}
System.out.println ("ResultSet created...");

while (rs.next()) {
String name = rs.getString("name");
String firstname = rs.getString("firstname");
String city = rs.getString("city");
java.sql.Date date = rs.getDate("birthdate");
String gender = rs.getString("gender");
String status = rs.getString("status");

System.out.println (name + ";" + firstname + ";" + city + "::" +
gender + status);
}

} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed

if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { // ignore }
rs = null;
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { // ignore }
stmt = null;
}
}
}
} // finally
}// main
}

Avatar
Craps
?????????????????4??????????????????????????????????????????????4???????????
??4???????????????????????????4?????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????
???????????????????????4????????????????????????????????????????????????????
?????????4?????????????????????????????4????????????????????????????????????
????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????}
Avatar
Craps
Merci j'ai simplifié le code en m'inspirant de ton exemple et celui d'un
bouquin.
Je le poste au cas où quelqu'un chercherait un exemple simple.

merci encore
-------------------------
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.sql.Statement;
import java.sql.ResultSet;

public class Bdd
{
Statement stmt = null;
ResultSet rs = null;

public static void main(String[] args)
throws SQLException, ClassNotFoundException , java.io.IOException
{
// charger le pilote jdbc de mysql
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Le driver a été correctement chargé");
Connection uneConnexion DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement unEnonceSQL = uneConnexion.createStatement();
ResultSet resultatSelect = unEnonceSQL.executeQuery("SELECT * FROM
PERSONNEL");

while(resultatSelect.next())
{
String name = resultatSelect.getString("nom");
String prenom = resultatSelect.getString("prenom");
System.out.println(name+"-"+prenom);
}
unEnonceSQL.close();
uneConnexion.close();
}
}