Creating jar file with MySQL JDBC Connector

So after trying to make a simpler method of executing some Java code that depends on MySQL JDBC drivers via CLI I came up with this solution. It basically involves 3 stages:

  1. Extract contents of MySQL JDBC connector (mysql-connector-java-5.1.28-bin.jar in this example)
  2. Create a manifest file and compile your Java files
  3. Package them all up nice and neatly into your very own jar

These steps assume you are in the correct directory with the required file(s) present when executing commands.

1. To extract the contents of the mysql-connector-java-5.1.28-bin.jar file you execute the following command:-

jar xf mysql-connector-java-5.1.28-bin.jar

This will put all the contents of the jar file in the current directory. This includes 3 directories (META-INFcom and org)

2. Create a manifest.txt file and compile your Java file(s).

The manifest just needs to include the main class of your program for this objective and will be written like so: ‘Main-Class: mainClass’ where you replace mainClass with the class name of your program containing the main() method (if you have a Java file called Dinosaur.java, you replace mainClass with Dinosaur).

a. Create the manifest file:-

echo Main-Class: Dinosaur>manifest.txt

b. Compile your Java file(s)

javac *.java

3. Finally, you need to put it all back together again in one big box …or jar

jar cvfm jarName.jar manifest.txt *.class META-INF com org

This will add all of the contents extracted from the MySQL JDBC Driver, your manifest information and any class files that were created via stage 2b.

To run this .jar file, use this command:-

java -jar jarName.jar

The code I am using for database connection testing and jar creation:-
import java.sql.*;
class className
{
public static void main(String[] args) throws Exception {
 Connection con = null;
 Statement st = null;
 ResultSet rs = null;
 int numberOfColumns=6;
 String url = "jdbc:mysql://host/database";
 String user = "username";
 String password = "password";
try {
 con = DriverManager.getConnection(url, user, password);
 st = con.createStatement();
 rs = st.executeQuery("SELECT * FROM table");
while (rs.next()) {
 for(int i=1; i<numberOfColumns;i++)
 {
 System.out.print(rs.getString(i) + " "); 
 }
 }
} catch (SQLException ex) {
 System.out.println(ex);
 } finally {
 try {
 if (rs != null) {
 rs.close();
 }
 if (st != null) {
 st.close();
 }
 if (con != null) {
 con.close();
 }
} catch (SQLException ex) {
 System.out.println(ex);
 }
 }
 }
}

The Impact on Society of Social Networking on the Web

 

We’ve entered a new ere of social networking. Once upon a time you may have met your friends down the pub to discuss a spot of relativity theory and the non-existence of colours. Gone are the days of leaving your home! With the emergence of services such as Facebook, Twitter and Google+ you seldom have to see another living soul in the flesh to communicate. Granted, we also have phones which most certainly contribute to this detached and isolated individualism that is society but that’s for another day.

At the click of a button, following a few keystrokes, you can spark the debate of a lifetime from your very own home. Not to mention the fact that you can stay in contact with people you never see, subconsciously spy on the lives of others thanks to their willingness to post their daily routines on Facebook, daily, and abuse the fat kid that made a funny vine. Is this such a bad thing?

Communicating through internet based social technologies is such a beautiful concept, it’s ridiculously convenient and merely replicates what has taken places since the dawn of time in a way that conforms to the current technological advances.

But is it healthy?  Surely, for the weak of mind and less in confidence, social networking seems fantastic – but is it not a hazard to their health? Lack of direct human interaction is, in my most humble opinion, never a good thing and social networking simply adds fuel to the fire. If you don’t like going out, and you don’t have to go out, why go out? The web is giving birth to a new global generation of recluse!

Bullying is another issue that comes as part of the spiders lair that is social networking (the web, get it?)! If you thought you left bullying behind you when you finished school, you might just be mistaken. Enter: Cyber bullying! According to the Megan Meier Foundation 39% of Social Network users have been cyber bullied in some way. This site also goes on to say that 1 in 10 teens who are subject to cyber bullying will attempt suicide! It’s worth noting that this foundation was set up at the end of 2007 by the mother of Megan Meier after Megan committed suicide following cyber bullying.

Cyber bullying and health aside, privacy is the next big concern. Love your privacy? Leave social networking well alone. There are countless examples of big social networking companies abusing their power to make money with your data. Sure enough they give you the option to opt out (in some cases) or stop certain people viewing your profile on Facebook, for example – but you provide more than enough ‘anonymous statistical data’ for them to sell on and earn a buck or two from your name. Perfected surveillance as a business model.

Anyway, my word limit is swiftly running out so if I don’t stop now I fear that this post will henceforth become an essay. I implore you to look at the following 2 sections on wikipedia to gain a little more insight on the past and current behaviours of Facebook:- Data Mining, Surveillance

Count your lucky stars!

Okay, ignore the stars… this is a classic ‘dodgy but works’ get the job done kind of scenario where I had to modify a program last year to compare words from file X to words in file Y on a character by character basis to verify spelling – a crude spell checker, if you will.

 

Anyway, edited the code to count braces ), {, ( and }, in a given source file -> useful if your code wont compile and you want to make sure there’s the right number of brackets and braces present. Of course, strings may include such braces and throw an offset to your result but regardless, here the source and image be:-

Search.java Example

Search.java Example

//imports
import java.io.*;
import java.util.*;

//class
public class search
{
  public int n,j,k,l;
  private ArrayList<String> fc = new ArrayList<String>(); //file content

  //main 
  public static void main(String[] argz)
  {

    search s = new search(); //instance of class
	s.n=0;
	s.j=0;
	s.k=0;
	s.l=0;
    s.loadFile(); //load file content to ArrayList
    s.results();
  }

  //ask user for path to file and load contents word by word into arraylist
  public void loadFile()
  {
    System.out.println("Enter path to text file for brace counting"); //ask for file path
    Scanner scan = new Scanner(System.in); //create scanner object to obtain file path
    String filename=scan.next(); //get file path and store in string var
    File loadedFile=null; //create null File var
    try
    {
      loadedFile = new File(filename); //instantiate File obj using given file path
      scan = new Scanner(loadedFile); //instantiate new Scanner object using File object
    }
    catch(FileNotFoundException e) //handle file not found exception
    {
      //notify user that file not found and ...
      System.out.println("File '"+filename+"' not found.");
      //...exit
      System.exit(0);
    }

    String newword=""; //string var to hold each word

    while (scan.hasNextLine ()) //while next line is available ...
    {

      String line =scan.nextLine(); //...get next line from text file

      //for the length of the line, go through each character to locate next word in line
      //and store word in the newword var for adding to the file content arraylist
      for (int i=0;i<line.length();i++)
      {
	//if character does not equal , ; : ? . [whitespace] [line-break] [carriage-return]... add char to newword var...
        if (line.charAt(i) == '(')
        {
		n++;

        }
        if (line.charAt(i) == ')')
        {
		j++;

        }
        if (line.charAt(i) == '{')
        {
		k++;

        }
        if (line.charAt(i) == '}')
        {
		l++;

        }
        else //...else store word in file content arraylist and set newword var to "" aka empty string value
        {

        }
      }

    }
  }

private void results()
{
System.out.println("( found: " + n);
System.out.println(") found: " + j);
System.out.println("{ found: " + k);
System.out.println("} found: " + l);
}
}

Java for breakfast, lunch and tea – and I don’t mean coffee

So, right now I’m meant to be in a social computing workshop but slept through my alarm thanks to the good ol’ codeine I took as a midnight snack to reduce ze tooth ache. I could go in for the next 90 minutes or so but I’m pretty sure that I’m still asleep.

That aside, nothing else is planned for this glorious, cold and whitish-grey day so following in yesterday’s footsteps I will tackle my Internet and Distributed Programming (aka sockets and threads) lab problems. All of them. Today.
The downside of that is that they just look so freakin’ tedious… write code on local machine, compile, run, close console ’cause windows doesn’t have an escape combination that works, repeat – transfer code to igor, SSH into igor, compile, run, close SSH because windows doesn’t have an escape combination that works… etc etc
I bid you a good day, my pedigree chums!

A day in the life of php

So, I had today off (saaaame as every Tuesday!)  What better way to spend it than doing 10 weeks worth of lab tasks for Databases, Networks and the web. That’s that out the way.

Despite the fact that it was a whole term’s worth of exercises, they were quite simple for someone who’s been using PHP for the best part of 15 years. Reallly simple. Posting form data and spamming superglobals, loops in HTML forms (.php files, obviously), creating and eating (deleting) cookies, cookie based CSS, arrays, MySQL communication etc etc.

My next task at hand is to do the coursework for that course which, judging by the requirements, should only take a day or 2 at ze most. That will involve creating a database and web based CD sales site.

The only downside to this is all of our lecturer’s example code seems hella outdated. Using “print” instead of “echo”, not that there’s anything wrong with that, print is just weird and always returns 1 and can only print one string which makes no sense for something that isn’t a method but kind of looks like a method if you want it to. Using mysql_ functions when the PHP.net docs clearly, by clearly i mean big ass pink box at top of documentation screaming READ ME, states the method is deprecated and will be removed in future revisions of PHP.  It’s not the end of the world, just less fortunate for anyone who is unaware that the relative content is outdated 🙁

 

Anyway, plan for the term -> do everything in the first few weeks and waste my time looking for a job! Money bitches, that’s what we needin’

 

1) Dynamic web coursework x2 (60/40)

2) Internet and distributed Programming labs and coursework

3) PAP coursework

4) Social Computing coursework.