Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package ConnettoreMysql;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Random;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.ejb.*;
  19. import server.RemoteServer;
  20.  
  21. /**
  22. *
  23. * @author kvothe
  24. */
  25. @Stateless
  26. public class ConnectBean1 {
  27. RemoteServer replica1 = new RemoteServer(false, true, true, "localhost:3306","1");
  28. private Connection connection;
  29.  
  30. public ConnectBean1() {
  31.  
  32. try {
  33.  
  34. Class.forName("com.mysql.jdbc.Driver");
  35. connection
  36. = DriverManager
  37. .getConnection("jdbc:mysql://"+ replica1.getMyip()+"/progetto", "salvo", "distribuiti");
  38.  
  39. } catch (ClassNotFoundException | SQLException ex) {
  40. Logger.getLogger(ConnettoreMysql.class.getName())
  41. .log(Level.INFO, null, ex);
  42. System.err.println("CONNESSIONE FALLITA");
  43. }
  44.  
  45. }
  46.  
  47. public ResultSet doQuery(String query) throws SQLException {
  48.  
  49. Statement stm = connection.createStatement();
  50.  
  51. return stm.executeQuery(query);
  52. }
  53.  
  54.  
  55.  
  56. public void close() throws SQLException {
  57. if (!connection.isClosed()) {
  58. connection.close();
  59. }
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement