Yu Sasaki's Blog



JDBC – using MySQL to make a connection between my local host and remote database

I created a “Student table” last time, and today I learned how to make a connection on Eclipse so I tried doing it.

I was given a code for SELECT statement using ORACLE driver.

My task was to make some modification in the code so that it can establish a connection with the remote host using mysql driver, instead of Oracle one, and successfully process the SELECT statement.

I changed the code like this:

// Invoke the dbQuery method
String driverName = “com.mysql.jdbc.Driver“;
String serverName = “zenit.senecac.on.ca“;
String portNumber = “3306“;
String databaseName = “my_account“;

String url = “jdbc:mysql://” +
serverName + “:” + portNumber
+ “/” + databaseName;

String u = “my_account“;
String p = “my_password“;

String query = “SELECT * FROM  Students“;

SELECT * FROM Students

SELECT * FROM Students

Works fine!

See you!

Filed under: EJB605, Java, Servlets & JSP

Learning the structure of Servlet

Today I learned Servlet in EJB605 class, and felt like learning it in more detail so I’ve read through some chapters of Java book.

Here are my understandings:

In the javax.servlet package, there is an interface called “Servlet” and followings are abstract methods to be overridden in future implemented classes.

Servlet interface

  • service(ServletRequest, ServletResponse)
  • init(ServletConfig)
  • destroy()
  • (a few more methods follow)

The interface is implemented by javax.servlet.GenericServlet class, which is an abstract class again, and that class is inherited by javax.servlet.http.HttpServlet class, which is now all set for being inherited by a Servlet we can implement. Here’s the hierarchy.

  • Servlet interface
    • implemented by GenericServlet class
      • extended by HttpServlet class

        • extended by our Servlet class (the most derived)

After a Servlet gets loaded by Web container, it initiates an object by calling the constructor of the Servlet. Then it calls init() method, and service() method follows. In the service() method, one of doX() method is called (or used to delegate another doX) to process the client request. After finishing the service, destroy() method is called to finish the life of Servlet.

The service() method has two parameters – HttpServletRequest and HttpServletResponse.

The interesting thing here to me is both are interfaces extended (not implemented) from the other interfaces – ServletRequest and ServletResponse.

  • ServletRequest interface
    • extended by HttpServletRequest interface
  • ServletResponse interface
    • extended by HttpServletResponse interface

I didn’t know the fact that an interface can extend another interface…so this was an eye opener. Anyway, the service() method has parameters of references for two objects that implemented HttpServletRequest and HttpServletResponse interfaces polymorphically. How the actual classes for these interfaces implement their methods are all dependent on vendor, so we do not need to care about it. A variety of methods are available in each request and response objects.

See you!

Filed under: EJB605, Java, Servlets & JSP

JDBC – using mysql driver on Mac

Today I learned how to interact with JDBC.

Tasks were :

  • Create a table called “Students” onto mysql database in my zenit account
  • Insert Students data into the table

At first time, I had a problem with importing the mysql driver.

I needed to set up a CLASSPATH environment variable so that the Java compiler can make use of the driver wherever it’s placed, but I had no idea how to set up classpath on my Mac. After googling, I found out some ways to set up CLASSPATH on Mac, and the easiest way that I figured out is this: putting the following line in my .bashrc file.

export CLASSPATH=/Library/Java/Extensions/mysql-connector-java-3.1.8-bin.jar;

Now that my Java compiler knows where to get the actual mysql driver class by CLASSPATH keyword, I am able to compile my Java program with the following simple command.

javac -cp CLASSPATH -d classes db/*.java

If I don’t use CLASSPATH, I have to use the following long command.

javac -cp /Library/Java/Extensions/mysql-connector-java-3.1.8-bin.jar -d classes db/*.java

See you!

Filed under: EJB605, Java