Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1656
  • Last Modified:

Connecting to MYSQL using JSP on godaddy.com

Hi. I am trying to connect to the mysql database on godaddy.com using JSP's. godaddy.com only provided me with their server name and port number. I was wondering what is the best way to connect to it and keep the connection open always so that all JSP's that need to access the database have connection?
Is it possible to set up the connection settings in the web.xml file and have no code for that in the JSP's whatsoever?
I tried connecting to it with the following code and no luck:
<sql:setDataSource
var="example"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://mysql server:port/databsename"
user=""
password=""
/>

 I also tried with the sample code on their website FAQ but I don't think that this keeps the connection open always.
<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="java.sql.*" errorPage="" %>
<%
String url = "jdbc:mysql://mysql mysql55.secureserver.net:4365/name";
String user= "";
String pass= "";
try{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from a_reg;");

while(rs.next())
{
%>
<table> <tr>
<td><%= rs.getString("firstname") %></td>
<td><%= rs.getString("lastname") %></td>
</tr></table>
<%}
rs.close();
conn.close();
}catch(Exception e)
{
out.println(e.toString());
}
%>

Does anyone know the best way to establish a connection so that all JSP's don't have any problems with commands like
<sql:query var="user">
select username from table where username = ?
<sql:param><c:out value='${param.username}' /></sql:param>
</sql:query>
0
milance445
Asked:
milance445
1 Solution
 
bloodredsunCommented:
Yes you can set up a datasource if the tomcat version is 5.0 as seen here http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html

this should allow jsps access to a datasource without requiring connection code
0
 
milance445Author Commented:
I saw this example, but I don't have access to the server.xml file on godaddy.com. Only web.xml. Is it possible to set up all the info like server name username,pass in the web.xml and not touch the server.xml file.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
http://forum.java.sun.com/thread.jspa?messageID=4159924

this link will surely help you as it has the solution too.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
milance445Author Commented:
I've already posted the code in my Q from the link http://forum.java.sun.com/thread.jspa?messageID=4159924...this is where i got it from also.
0
 
milance445Author Commented:
I have solved my own problem though. I managed to setup the web.xml file to open the connection.
0
 
bloodredsunCommented:
Could you please post the solution so that others can benefit from seeing how you did it.
0
 
milance445Author Commented:
The main problem was the URL and taglib location.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <context-param>
      <param-name>javax.servlet.jsp.jstl.fmt.locale</param-name>
      <param-value>en</param-value>
    </context-param>

    <context-param>
      <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
      <param-value>messages</param-value>
    </context-param>

    <context-param>
       <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>
       
    <param-value>jdbc:mysql://mysql##.secureserver.net:port/databasename,com.mysql.jdbc.Driver,user,pass</param-value>
    </context-param>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/standard/scriptfree</taglib-uri>
        <taglib-location>/WEB-INF/scriptfree.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/taglibs/standard/permittedTaglibs</taglib-uri>
        <taglib-location>/WEB-INF/permittedTaglibs.tld</taglib-location>
    </taglib>

</web-app>
0
 
GranModCommented:
PAQed with points refunded (250)

GranMod
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now