Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4.  
  5. import com.mysql.jdbc.Connection;
  6. import com.mysql.jdbc.Statement;
  7.  
  8. public class ConnexionBDD {
  9.  
  10. static String url = "jdbc:mysql://localhost:8888/Peoples?autoReconnect=true&useSSL=false";
  11. static String login = "root";
  12. static String password = "";
  13.  
  14. static Connection connection = null;
  15. static Statement statement = null;
  16. static ResultSet result = null;
  17. static String request = "";
  18.  
  19. public static void main (String[] args) {
  20.  
  21. System.out.println("will load driver");
  22. loadingDrive();
  23. System.out.println("will connect");
  24. connection();
  25.  
  26. }
  27.  
  28. public static void loadingDrive() {
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. } catch ( ClassNotFoundException e ) {
  32. e.getMessage();
  33. }
  34. }
  35.  
  36. public static void connection() {
  37. try
  38. {
  39. connection = (Connection) DriverManager.getConnection(url, login, password);
  40. System.out.println("Connected");
  41. statement = (Statement) connection.createStatement();
  42.  
  43. result = statement.executeQuery(request);
  44.  
  45. }
  46. catch ( SQLException e )
  47. {
  48. System.out.println(e.getMessage());
  49. } finally {
  50. if ( result != null ) {
  51. try {
  52. result.close();
  53. } catch ( SQLException ignore ) {
  54. }
  55. }
  56. if ( statement != null ) {
  57. try {
  58. statement.close();
  59. } catch ( SQLException ignore ) {
  60. }
  61. }
  62. if ( connection != null ) {
  63. try {
  64. connection.close();
  65. } catch ( SQLException ignore ) {
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72. will load driver
  73. will connect
  74. Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  75.  
  76. result = statement.executeQuery(request);
  77.  
  78. System.out.println(result.next());
  79.  
  80. <dependency>
  81. <groupId>mysql</groupId>
  82. <artifactId>mysql-connector-java</artifactId>
  83. <version>5.1.6</version>
  84. </dependency>
  85.  
  86. will connect
  87. Connected
  88. true
  89.  
  90. Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  91.  
  92. static String url = "jdbc:mysql://localhost:3306/Peoples?autoReconnect=true&useSSL=false";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement