Java Cheat Sheet

Some snippets of code, and various commands to act as crib notes for Java.

 

How to make executable JAR files

To make executable JARs, you first need a manifest file called MANIFEST.MF in the META-INF directory. The manifest file may contain a number of possible entries; however, for our purposes here, we just need to specify the name of the Java class that contains the Java-based extractor program's main() method:

		Main-Class: com.threaded.wibble.wobble.werble
	
 

How to update just a class and the manifest in an already constructed JAR file.

jar uvfm some.jar amanifest com/threaded/wibble/wobble/werble.class

 

A JDBC Servlet talking to MySQL

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class jJDBCServletMysql extends HttpServlet {

public void doGet (HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
    {

    	String url   = "jdbc:mysql://db3.threaded.com/mysql";

    	String query = "SELECT * FROM Person";
						
try {
	    Class.forName("com.mysql.jdbc.Driver");
 
Connection con = DriverManager.getConnection( url, "mysgl_login", "mysgl_password" );

            Statement stmt = con.createStatement ();

            ResultSet rs = stmt.executeQuery (query);

            printResultSet ( resp, rs );

            rs.close();
            stmt.close();
            con.close();
}   
        catch (SQLException ex) {
            
            PrintWriter out = resp.getWriter();
	    resp.setContentType("text/html");
			
            while (ex != null) {  
                out.println ("SQL Exception:  " + ex.getMessage ());
                ex = ex.getNextException ();  
            } 
        } 
        catch (java.lang.Exception ex) {
      	    PrintWriter out = resp.getWriter();
		resp.setContentType("text/html");	
		out.println ("Exception:  " + ex.getMessage ());
	}
    } 


    private void printResultSet ( HttpServletResponse resp, ResultSet rs )
        throws SQLException
    {
try  {
	    PrintWriter out = resp.getWriter();

	    out.println("<html>");
	    out.println("<head><title>jbs jdbc/mysql servlet</title></head>");
	    out.println("<body>");
	    out.println("<center><font color=AA0000>");
	    out.println("<h3>jbsJDBCServlet</h3>");
	    out.println("<h3>Data Retrieved:</h3>");
	        
	    out.println("<table border='1'>");

            int numCols = rs.getMetaData().getColumnCount ();
            while ( rs.next() ) {
                out.println("<tr>");
                for (int i=1; i<=numCols; i++) {
                    out.print("<td>" + rs.getString(i) + "</td>" );
                } 
                out.println("</tr>");
            } 

out.println("</table>");
	        
	    out.println("</font></center>");
	    out.println("</body>");
	    out.println("</html>");
	    out.close();
	} 
        catch ( IOException except)  {  //throwing exception away
        } 
    } 
} 

	
	

Copyright threaded.com 2005. All rights reserved.