I am running JSP on Tomcat and MySQL through WAMP server. I am unable to connect the JSP to MySQL. Here is the code
<%
try {
/* Create string of connection url within specified format with machine name,
port number and database name. Here machine name id localhost and
database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost:3306/database";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "");
// check weather connection is established or not by isClosed() method
if(!connection.isClosed())
%>
<%
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
<%
out.println("Unable to connect to database."+ex.toString());
}
%>
I have even placed mysql-connector-java-5.0.8.jar in /lib folder. Can anyone help me with this?
Edit: through the discussion, it was determined that the class wasn’t being found, because the server needed to be restarted.
It would really help if you could tell us what the exception actually is.
From what you’ve given, it could be a few things:
I’ve never accessed resources this way, either. Typically, I use a JNDI lookup with a DataSource from an InitialContext.