I have been able to get mysql to work with Java but not with tomcat. When i pull up index.jsp in my browser, I get this: "The user is equal to: aaaa" (in TestSQLLoad.java, the variable "user" is init'd with a value of "aaaa"). Can somone tell me why tomcat is not correctly working with mysql???
thanks
mysql-connector-java-3.0.11-stable-bin.jar is in currently in /usr/local/jakarta/common/lib
index.jsp:
part of WEB-INF/web.xml:
part of server.xml:
WEB-INF/classes/package1/TestSQLLoad.java:
thanks
mysql-connector-java-3.0.11-stable-bin.jar is in currently in /usr/local/jakarta/common/lib
index.jsp:
Code:
<html>
<body>
<%package1.TestSQLLoad tst = new package1.TestSQLLoad();
tst.init();%>
The user is equal to: <%= tst.getUser() %>
</body>
</html>
part of WEB-INF/web.xml:
Code:
<web-app>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
part of server.xml:
Code:
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamesp$
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="localhost_log." suffix=".txt" tim$t" timestamp="true"/>
<Context path="" docBase="/usr/local/apache2/htdocs" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>****</value>
</parameter>
<parameter>
<name>password</name>
<value>****</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/mysql?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
</Host>
WEB-INF/classes/package1/TestSQLLoad.java:
Code:
package package1;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class TestSQLLoad{
String user = "nothing yet!!!";
public void init() {
try{
String user = "aaaa";
Context ctx = new InitialContext();
if(ctx == null ) {
throw new Exception("Boom - No Context");
}
DataSource ds = (DataSource)ctx.lookup("java:jdbc/mysql");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
user = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
String q = "select user from user";
ResultSet rst = stmt.executeQuery(q);
if(rst.next()) {
user=rst.getString(1);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public String getUser() {
return user;
}
}