Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package industries.griffoul.jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JDBC {
- public static void main(String[] args) {
- enregistrer("Damien");
- lire();
- }
- public static void lire() {
- //String url = "jdbc:mysql://localhost/basetest";
- String url2 = "jdbc:mysql://localhost/basetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- String login = "root";
- String password = "root";
- Connection c = null;
- Statement s = null;
- ResultSet r = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- c = DriverManager.getConnection(url2, login, password);
- s = c.createStatement();
- String sql = "SELECT * FROM tabletest";
- r = s.executeQuery(sql);
- while(r.next()) {
- System.out.println(r.getString("nom"));
- }
- } catch(SQLException e) {
- e.printStackTrace();
- } catch(ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- c.close();
- s.close();
- } catch(SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void enregistrer(String nom) {
- //String url = "jdbc:mysql://localhost/basetest";
- String url2 = "jdbc:mysql://localhost/basetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- String login = "root";
- String password = "root";
- Connection c = null;
- Statement s = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- c = DriverManager.getConnection(url2, login, password);
- s = c.createStatement();
- String sql = "INSERT INTO tabletest(nom) VALUES ('"+nom+"')";
- s.executeUpdate(sql);
- } catch(SQLException e) {
- e.printStackTrace();
- } catch(ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- c.close();
- s.close();
- } catch(SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement