I was trying to find some example to connect MySQL database with my Java Shared Hosting Plan on GoDaddy.com but I could not. Well, I don’t want to say that GoDaddy has the best plan for hosting Java application. I still can not figure out how to use JSTL, custom url-pattens in the web.xml and forward requests from a servlet to JSP page. Also, we have to put http://<domain_name>/servlet/<servlet_name> to access servlet. But, here is simple working example to show you database connection from hosting to MySQL server on GoDaddy Shared Hosting.
Take a look at the example at link : GoDaddyJavaMySQL
DBConnector.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.gaurangjadia.code.base; import java.sql.Connection; import java.sql.DriverManager; public class DBConnector { private String host = ""; private int port = 3306; private String databasename = ""; private String username = ""; private String password = ""; private String connectionstring = ""; public DBConnector(String hostname, int port, String db, String username, String password){ this.setHost(hostname); this.setPort(port); this.setDatabasename(db); this.setUsername(username); this.setPassword(password); this.setConnectionstring("jdbc:mysql://" + getHost() + ":" + getPort() + "/" + getDatabasename()); } public Connection connect() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection objConnection = DriverManager.getConnection(this.getConnectionstring(), this.getUsername(), this.getPassword()); return objConnection; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getDatabasename() { return databasename; } public void setDatabasename(String databasename) { this.databasename = databasename; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getConnectionstring() { return connectionstring; } public void setConnectionstring(String connectionstring) { this.connectionstring = connectionstring; } } |
GoDaddyJavaMySQL.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.gaurangjadia.code.controllers; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.gaurangjadia.code.base.DBConnector; @WebServlet(description = "Main Controller to handle all requests", urlPatterns = { "/index.html" }) public class GoDaddyJavaMySQL extends HttpServlet { private static final long serialVersionUID = 1L; private DBConnector objDBConnector; public GoDaddyJavaMySQL() { super(); } public void init(ServletConfig config) throws ServletException { try { objDBConnector = new DBConnector("server_ip", 3306, "database_name", "user_name", "password"); } catch (Exception ex) { ex.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); try { response.setContentType("text/html; charset=ISO-8859-1"); Connection conn = objDBConnector.connect(); Statement strQuery = conn.createStatement(); ResultSet rs = strQuery.executeQuery("SELECT * FROM table_name"); out.write("<html><head><title>Connect JavaEE Application to MySQL Database on GoDaddy Shared Linux Hosting Server </title></head><body>"); out.write("<h2>Connect JavaEE Application to MySQL Database on GoDaddy Shared Linux Hosting Server.</h2>"); out.write("<table border=\"1\">"); while(rs.next()){ out.write("<tr><td>" + rs.getString("Column_1") + "</td><td>" + rs.getString("Column_2") + "</td><td>" + rs.getString("Column_3") + "</td></tr>"); } out.write("</table>"); out.write("</body></html>"); } catch (Exception ex) { ex.printStackTrace(out); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
< ?xml version="1.0" encoding="UTF-8"?> <web -app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display -name>GoDaddyJavaMySQL</display> <welcome -file-list> </welcome><welcome -file>index.html</welcome> <welcome -file>index.htm</welcome> <welcome -file>index.jsp</welcome> <welcome -file>default.html</welcome> <welcome -file>default.htm</welcome> <welcome -file>default.jsp</welcome> <servlet> <description></description> <display -name>GoDaddyJavaMySQL</display> </servlet><servlet -name>GoDaddyJavaMySQL</servlet> <servlet -class>com.gaurangjadia.code.controllers.GoDaddyJavaMySQL</servlet> <servlet -mapping> </servlet><servlet -name>GoDaddyJavaMySQL</servlet> <url -pattern>/GoDaddyJavaMySQL</url> </web> |