Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. Error sqlException:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
  2. Communications link failure The last packet sent successfully to the server was 0
  3. milliseconds ago. The driver has not received any packets from the server.
  4.  
  5. public class HelloServlet extends HttpServlet {
  6.  
  7. @Override
  8. public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  9.  
  10. PrintWriter out = resp.getWriter();
  11. //out.println("Dear vistor, you are accessing my first project deployed on cloud. Thank you");
  12.  
  13. try {
  14.  
  15. Class.forName("com.mysql.jdbc.Driver");
  16. String url = String.format("jdbc:mysql://IP:3306/DatabaseName?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
  17. Connection connection = DriverManager.getConnection(url, "userName", "Password");
  18. try (Statement statement = connection.createStatement()) {
  19. ResultSet resultSet = statement.executeQuery("select test from tableTest;");
  20. while (resultSet.next()) {
  21. out.println(resultSet.getString(1));
  22. }
  23. }
  24. } catch (ClassNotFoundException ex) {
  25. out.println("Error ClassNotFoundException:" + ex.toString());
  26. } catch (SQLException ex) {
  27. out.println("Error sqlException:" + ex.toString());
  28. }
  29. catch(Exception ex){
  30. out.println("Exception:" + ex.toString());
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement