%@ page import="javax.naming.InitialContext,
javax.sql.DataSource,
javax.naming.Context,
java.util.Hashtable,
javax.naming.NamingException,
java.sql.*,
java.util.Enumeration,
java.util.StringTokenizer" %>
<%@ page contentType="text/html; charset=UTF-8" %>
Atlassian Database Check Utility
Atlassian Database Check Utility.
<%
String operation = request.getParameter("operation");
String driverStr = request.getParameter("driver");
String jdbcURL = request.getParameter("jdbcURL");
String username = request.getParameter("username");
String password = request.getParameter("password");
String initialContext = request.getParameter("initialContext");
String provider = request.getParameter("provider");
String ds = request.getParameter("ds");
if (operation == null)
{
out.println("Please choose an operation below: ");
}
else if ("reset".equals(operation))
{
operation = null;
driverStr = null;
jdbcURL = null;
username = null;
password = null;
initialContext = null;
provider = null;
ds = null;
}
else if (operation != null && "standard".equals(operation))
{
out.println("Connecting to database via JDBC URL:");
try
{
out.println("");
out.print("- Found driver (" + driverStr + ") on classpath? ");
Driver driver = null;
try
{
driver = (Driver) Class.forName(driverStr).newInstance();
if (driver == null)
throw new ClassNotFoundException("Could not find " + driverStr + " on classpath.");
out.println(" ok ");
}
catch (java.lang.ClassNotFoundException e)
{
out.println("Driver failed to load. Check your classpath: " + e);
}
out.println("");
out.print("
- Open a connection to the database via JDBC (" + jdbcURL + ")? ");
try
{
out.println("Found driver (" + driverStr + ") on classpath");
Class.forName(driverStr);
Connection conn = DriverManager.getConnection(jdbcURL, username, password);
Statement st = conn.createStatement();
if (driverStr.indexOf("oracle") == -1)
{
st.executeQuery("SELECT 1;");
}
else
{
st.executeQuery("SELECT 1 from dual");
}
out.println(" ok ");
conn.close();
}
catch (SQLException e)
{
out.println("Connection failed to open on the JDBC URL: " + e);
}
}
catch(Exception e)
{
out.println(e);
}
out.println("
");
out.println("");
}
else if (operation != null && "datasource".equals(operation))
{
out.println("Connecting to database via Datasource: ");
out.println("");
try
{
DataSource dsrc = null;
Context ctx = null;
if ((initialContext != null || provider != null) && (!"".equals(initialContext) || !"".equals(provider)))
{
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, initialContext);
ht.put(Context.PROVIDER_URL, provider);
try
{
out.print("- Build InitialContext with INITIAL_CONTEXT_FACTORY (" + initialContext + ") and PROVIDER_URL ("+ provider + ")?" );
ctx = new InitialContext(ht);
out.println(" ok ");
}
catch (NamingException e)
{
out.println("Couldn't build InitialContext. Check your JNDI configuration: " + e);
}
out.println("");
}
else
{
try
{
out.print("
- Gain InitialContext?: " );
ctx = new InitialContext();
out.println(" ok ");
}
catch (NamingException e)
{
out.println("Couldn't build InitialContext. Check your JNDI configuration: " + e);
}
}
out.print("" );
try
{
out.print("
- Locate Datasource (" + ds + ") in InitialContext?: " );
dsrc = (DataSource) ctx.lookup(ds); //let the user specify the exact JNDI name themselves
if (dsrc == null)
throw new NamingException("Could not locate " + ds);
out.println(" ok ");
}
catch (NamingException e)
{
out.println("Couldn't locate Datasource (" + ds + "). Check your JNDI configuration: " + e);
}
catch (ClassCastException e)
{
out.println("Couldn't locate Datasource (" + ds + "). Whatever we found, it wasn't a Datasource: " + e);
}
out.print("" );
out.print("
- Open a connection via Datasource (" + ds + ")? " );
try
{
Connection conn = dsrc.getConnection();
Statement st = conn.createStatement();
st.executeQuery("SELECT 1;");
conn.close();
out.println(" ok ");
}
catch (SQLException e)
{
out.println("Couldn't open a connection on Datasource (" + ds + "): " + e);
}
catch (NullPointerException e)
{
out.println("Couldn't open a connection on Datasource (" + ds + "): " + e);
}
out.println("
");
out.print("" );
}
catch (NullPointerException e)
{
out.println(e);
}
catch (Exception e)
{
out.println(e);
}
}
if (driverStr == null)
driverStr = "";
if (jdbcURL == null)
jdbcURL = "";
if (username == null)
username = "";
if (password == null)
password = "";
if (ds == null)
ds = "";
if (initialContext == null)
initialContext = "";
if (provider == null)
provider = "";
%>
<%
if (operation != null && "showClasspath".equals(operation))
{
%>
Current view of System Classpath:
<%
String classpathrep = System.getProperty("java.class.path");
StringTokenizer st = new StringTokenizer(classpathrep, ":;");
while (st.hasMoreTokens())
out.println("- " + st.nextToken());
%>
<%
}
%>
Drivers currently visible on the classpath system. If you do not see your driver here it does not mean you cannot access it. It might need to be explicitly loaded by the JVM.
<%
Enumeration enu = DriverManager.getDrivers();
while (enu.hasMoreElements())
{
Object o = enu.nextElement();
out.println("- " + o.getClass().getName() );
}
%>
Example MySQL JDBC URL: jdbc:mysql://localhost/test
Example Postgres JDBC URL: jdbc:postgres://localhost:5432/test