Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package Database;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class Database {
  11.  
  12.  
  13. public Database() {
  14.  
  15. /*try {
  16. Class.forName("org.postgresql.Driver");
  17. System.out.println("Driver O.K.");
  18.  
  19. String url = "jdbc:postgresql://localhost:5432/gestionentreprise";
  20. String user = "chris";
  21. String passwd = "199710";
  22.  
  23. Connection conn = DriverManager.getConnection(url, user, passwd);
  24. System.out.println("Connexion effective !");
  25.  
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. } */
  29. String url = "jdbc:mysql://sql7.freemysqlhosting.net:3306/sql7292995";
  30. String user = "sql7292995";
  31. String passwd = "nn4Y4shvHf";
  32.  
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. System.out.println("Driver O.K.");
  36.  
  37.  
  38. Connection conn = DriverManager.getConnection(url, user, passwd);
  39. System.out.println("Connexion effective !");
  40.  
  41. //RECUPER DATA
  42. /*
  43. Statement stmt=conn.createStatement();
  44. ResultSet rs=stmt.executeQuery("select * from test");
  45. while(rs.next())
  46. System.out.println(rs.getString(1)+" "+rs.getString(2)); */
  47.  
  48. // create a sql date object so we can use it in our INSERT statement
  49.  
  50.  
  51. // the mysql insert statement
  52. String query = " insert into test (id, nom)"
  53. + " values (?, ?)";
  54.  
  55. // create the mysql insert preparedstatement
  56. PreparedStatement preparedStmt = conn.prepareStatement(query);
  57. preparedStmt.setString (1, "Tamere ?");
  58. preparedStmt.setString (2, "ET ouai");
  59.  
  60.  
  61. // execute the preparedstatement
  62. preparedStmt.execute();
  63.  
  64. System.out.println("done");
  65.  
  66. conn.close();
  67.  
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement