Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package sample;
  2.  
  3. import com.mysql.cj.jdbc.Driver;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class Main {
  11.  
  12. private static final String URL = "jdbc:mysql://localhost:3306/pbz?useUnicode=true&useSSL=false&useJDBCCompliantTimezoneShift=true" +
  13. "&useLegacyDatetimeCode=false&serverTimezone=UTC";
  14. private static final String USERNAME = "root";
  15. private static final String PASSWORD = "123qwe";
  16.  
  17. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  18.  
  19. Connection connection = null;
  20. Driver driver;
  21.  
  22. try {
  23. driver = new com.mysql.cj.jdbc.Driver();
  24. DriverManager.registerDriver(driver);
  25. }
  26. catch (SQLException e1) {
  27. System.out.println("Драйвер не зарегистрировался");
  28. return;
  29. }
  30.  
  31. try {
  32. connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  33. if (!connection.isClosed())
  34. System.out.println("Соединение установлено");
  35.  
  36.  
  37. Statement statement = connection.createStatement();
  38. statement.execute("SELECT * FROM calls");
  39.  
  40. }catch (SQLException ex){
  41. System.err.println("Соединение не установлено");
  42. ex.printStackTrace(); // Понадобился, чтобы отловить исключения, скрытые выводом на экран предупреждения
  43. return;
  44. } finally {
  45. if (connection != null) connection.close();
  46. }
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement