Guest User

Untitled

a guest
Sep 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package db;
  6.  
  7.  
  8. /**
  9. *
  10. * @author pathos
  11. */
  12.  
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.Driver;
  16. import java.sql.SQLException;
  17. import java.util.Properties;
  18. import javax.naming.Context;
  19. import javax.naming.InitialContext;
  20. import javax.naming.NamingException;
  21. import javax.sql.DataSource;
  22.  
  23. public class Connector
  24. {
  25.  
  26. private static final String user = "root";
  27. private static final String password = "root";
  28. private static final String dbUrl = "jdbc:mysql://localhost:3306/users?user=root&password=root";
  29. private static final String dbUrl2 = "jdbc:mysql://localhost:3306/users?";
  30. static Connection connection = null;
  31. public static Connection getConnection() throws ClassNotFoundException, InstantiationException,IllegalAccessException
  32. {
  33. try {
  34. //Driver d = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance ();
  35. //connection = d.connect(dbUrl,new Properties());
  36.  
  37. Connection con = //DriverManager.getConnection(dbUrl);
  38. DriverManager.getConnection(dbUrl2+"user="+user+"&password="+password);
  39. return connection;
  40. }
  41.  
  42. catch (SQLException e)
  43. {
  44. e.printStackTrace();
  45.  
  46. return null;
  47. }
  48.  
  49.  
  50. }
  51.  
  52. public static Connection getConnection(boolean pool) throws ClassNotFoundException,InstantiationException,IllegalAccessException
  53. {
  54. if (pool)
  55. {
  56. Connection conn = null;
  57. try
  58. {
  59. Context context = new InitialContext();
  60. Context envctx = (Context) context.lookup("java:comp/env");
  61. DataSource ds = (DataSource) envctx.lookup("jdbc/DB");
  62. conn = ds.getConnection();
  63. }
  64. catch (NamingException e)
  65. {
  66. e.printStackTrace();
  67. }
  68. catch (SQLException e)
  69. {
  70. e.printStackTrace();
  71. }
  72. finally
  73. {
  74. return conn;
  75. }
  76. }
  77. else return getConnection();
  78. }
  79. }
Add Comment
Please, Sign In to add comment