Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package db;
- /**
- *
- * @author pathos
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Driver;
- import java.sql.SQLException;
- import java.util.Properties;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- public class Connector
- {
- private static final String user = "root";
- private static final String password = "root";
- private static final String dbUrl = "jdbc:mysql://localhost:3306/users?user=root&password=root";
- private static final String dbUrl2 = "jdbc:mysql://localhost:3306/users?";
- static Connection connection = null;
- public static Connection getConnection() throws ClassNotFoundException, InstantiationException,IllegalAccessException
- {
- try {
- //Driver d = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance ();
- //connection = d.connect(dbUrl,new Properties());
- Connection con = //DriverManager.getConnection(dbUrl);
- DriverManager.getConnection(dbUrl2+"user="+user+"&password="+password);
- return connection;
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- return null;
- }
- }
- public static Connection getConnection(boolean pool) throws ClassNotFoundException,InstantiationException,IllegalAccessException
- {
- if (pool)
- {
- Connection conn = null;
- try
- {
- Context context = new InitialContext();
- Context envctx = (Context) context.lookup("java:comp/env");
- DataSource ds = (DataSource) envctx.lookup("jdbc/DB");
- conn = ds.getConnection();
- }
- catch (NamingException e)
- {
- e.printStackTrace();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- return conn;
- }
- }
- else return getConnection();
- }
- }
Add Comment
Please, Sign In to add comment