Advertisement
Guest User

SQL connection sans procédure

a guest
Jun 6th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. public static String lireEnBase(int level) {
  2.         //Information d'accès à la base de données
  3.                 String url = "jdbc:mysql://localhost/lorann?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  4.                 String login = "java";
  5.                 String passwd = "java";
  6.                 Connection cn =null;
  7.                 Statement st =null;
  8.                 ResultSet rs =null;
  9.                 String test = "";
  10.                
  11.                 try {
  12.  
  13.                     // Etape 1 : Chargement du driver
  14.                     Class.forName("com.mysql.cj.jdbc.Driver");
  15.  
  16.                     // Etape 2 : récupération de la connexion
  17.                     cn = DriverManager.getConnection(url, login, passwd);
  18.  
  19.                     // Etape 3 : Création d'un statement
  20.                     st = cn.createStatement();
  21.  
  22.                     String sql = "{call query_level("+level+")}";
  23.                     java.sql.CallableStatement statement = cn.prepareCall(sql);
  24.  
  25.                     // Etape 4 : exécution requête
  26.                     rs = st.executeQuery(sql);
  27.  
  28.                     // Si récup données alors étapes 5 (parcours Resultset)
  29.  
  30.                     while (rs.next()) {
  31.                         test = rs.getString("Level_Code");
  32.                         System.out.println(test);
  33.                     }
  34.                 } catch (SQLException e) {
  35.                     e.printStackTrace();
  36.                 } catch (ClassNotFoundException e) {
  37.                     e.printStackTrace();
  38.                 } finally {
  39.                     try {
  40.                     // Etape 6 : libérer ressources de la mémoire.
  41.                         cn.close();
  42.                         st.close();
  43.                     } catch (SQLException e) {
  44.                         e.printStackTrace();
  45.                     }
  46.                 }
  47.                 return test;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement