connect to online mysql database (newbie)
Posted by: Lee Boonstra
Date: February 23, 2008 05:45AM

Hi there,

I'm training myself on Java Servlets & JSP's.
I know mySQL from my experiences with PHP, so that's why I choosed for a MySQL database. Now I want to test with my local Tomcat (5.0) server, to make a connection to my online database.

I copied the mysql connector jar, in my www-root/WEB-INF/lib
And I wrote this servlet:

package com.ladysign.nl.moodmate.db;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DatabaseConnect extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Connection conn;
	Statement stmt;
	
	public void init() throws ServletException{
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}catch (Exception ex){
			System.out.println("Exception is : " + ex.toString() );
		}
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}  	
	

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException{
		
		String host = "212.61.10.22";
		String db = "databasename";
		String username = "myusername";
		String password = "mypass";
		
		try{
			conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + db, username, password);
			stmt = conn.createStatement();
		} catch(SQLException ex){
			System.out.println("SQLException : " + ex.getMessage() );
		}
	}
}

Now I get this exception back:
Communications link failure
Last packet sent to the server was 0 ms ago.

Is this because I'm connecting to an online database? - And how can I solve this???



Edited 1 time(s). Last edit at 02/23/2008 05:45AM by Lee Boonstra.

Options: ReplyQuote


Subject
Written By
Posted
connect to online mysql database (newbie)
February 23, 2008 05:45AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.