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
|
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class Mysql {
Mysql(){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://hote:3306/login";
String login = "login";
String password = "pass";
java.sql.Connection connection = null;
try{
Class.forName(driver);
connection = DriverManager.getConnection(url,login,password);
//travail avec les données
}
catch(ClassNotFoundException cnfe){
System.out.println("Driver introuvable : ");
cnfe.printStackTrace();
}
catch(SQLException sqle){
System.out.println("Erreur SQL : " +sqle);
//Cf. Comment gérer les erreurs ?
}
catch(Exception e){
System.out.println("Autre erreur : " +e);
e.printStackTrace();
}
finally
{
if(connection!=null){try{connection.close();}catch(Exception e){e.printStackTrace();}}
//etc.
}
}
public static void main(String[] args) {
Mysql connec = new Mysql();
}
} |