6. Java for Web Development for PHP developer – a rant

This is hour six. After NetBeans installation on hour 5th, it is time to do work. Checked with how to Run Servlet, How to run single Servlet instead of whole project, how to use web.xml file and other things about folder structure. Also, learned about, how to pass the POST-ed data from HTML page to Servlet.

Here is a code written in Eclipse:


import java.sql.*;
/**
*
* @author DOTNET
*/
public class mysqlConnEx {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// TODO code application logic here
try
{
//(load and register the JDBC driver classes
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/java";
// get a connection object
// (getConnection is a static method of DriverManager class. if err occurs then SQLException Obj otherwise Connection Obj)
Connection Conn =  DriverManager.getConnection( url,"root", "root");
//System.out.println("URL: " + url);
System.out.println("Connection: " + Conn);
Statement St = Conn.createStatement();
ResultSet Rs = St.executeQuery("Select * from Emp");
while(Rs.next())
System.out.println(Rs.getString("Enum") + "  " + Rs.getString("Ename") + "   "+ Rs.getDouble("Salary"));
Rs.close();
St.cancel();
Conn.close();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

The same code I have tried to test in NetBeans but the code was not working. Faced problem about MySQL. The error got is com.mysql.jdbc.driver. huh! No explanation. I was getting better error explanation in Eclipse. After searching on Google, I got the solution. Solution is to mention the file in Library from the project's properties box.

Here is the code on Netbeans:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testPackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author satya
*/
public class testservlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//(load and register the JDBC driver classes
Class.forName("com.mysql.jdbc.Driver");
//Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/java";
// get a connection object
// (getConnection is a static method of DriverManager class. if err occurs then SQLException Obj otherwise Connection Obj)
Connection Conn = DriverManager.getConnection(url, "root", "root");
//System.out.println("URL: " + url);
out.println("Connection: " + Conn);
Statement St = Conn.createStatement();
ResultSet Rs = St.executeQuery("Select * from Emp");
while(Rs.next()) {
out.println(Rs.getString("Enum") + "  " + Rs.getString("Ename") + "   "+ Rs.getDouble("Salary"));
}
Rs.close();
St.cancel();
Conn.close();
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet testservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet testservlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
}
catch (Exception sqle) {
out.println(sqle.getMessage());
}
finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
In the course of writing code to NetBeans, I played with Exception. It got error :
unreported exception java.io.IOException; must be caught or declared to be thrown, when I removed throws from the method declaration. I removed throws from method declaration and added catch like this:
catch (IOException eio) {
eio.printStackTrace();
}
catch (ClassNotFoundException cle) {
out.println(cle.getMessage());
}
catch (SQLException sqle) {
out.println(sqle.getMessage());
}

Just silly. I am learning. The 'out' was giving error as well. I replaced it with System.out.println().