Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package industries.griffoul.jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class JDBC {
  10.    
  11.     public static void main(String[] args) {
  12.         enregistrer("Damien");
  13.         lire();
  14.     }
  15.    
  16.     public static void lire() {
  17.         //String url = "jdbc:mysql://localhost/basetest";
  18.         String url2 = "jdbc:mysql://localhost/basetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  19.         String login = "root";
  20.         String password = "root";
  21.         Connection c = null;
  22.         Statement s = null;
  23.         ResultSet r = null;
  24.        
  25.         try {
  26.             Class.forName("com.mysql.jdbc.Driver");
  27.             c = DriverManager.getConnection(url2, login, password);
  28.             s = c.createStatement();
  29.             String sql = "SELECT * FROM tabletest";
  30.             r = s.executeQuery(sql);
  31.            
  32.             while(r.next()) {
  33.                 System.out.println(r.getString("nom"));
  34.             }
  35.         } catch(SQLException e) {
  36.             e.printStackTrace();
  37.         } catch(ClassNotFoundException e) {
  38.             e.printStackTrace();
  39.         } finally {
  40.             try {
  41.                 c.close();
  42.                 s.close();
  43.             } catch(SQLException e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.     }
  48.    
  49.     public static void enregistrer(String nom) {
  50.         //String url = "jdbc:mysql://localhost/basetest";
  51.         String url2 = "jdbc:mysql://localhost/basetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  52.         String login = "root";
  53.         String password = "root";
  54.         Connection c = null;
  55.         Statement s = null;
  56.        
  57.         try {
  58.             Class.forName("com.mysql.jdbc.Driver");
  59.             c = DriverManager.getConnection(url2, login, password);
  60.             s = c.createStatement();
  61.             String sql = "INSERT INTO tabletest(nom) VALUES ('"+nom+"')";
  62.             s.executeUpdate(sql);
  63.         } catch(SQLException e) {
  64.             e.printStackTrace();
  65.         } catch(ClassNotFoundException e) {
  66.             e.printStackTrace();
  67.         } finally {
  68.             try {
  69.                 c.close();
  70.                 s.close();
  71.             } catch(SQLException e) {
  72.                 e.printStackTrace();
  73.             }
  74.         }
  75.     }
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement