JDBC java and mysql

first you must include the sql package:

import java.sql.*;

In order to word with JDBC driver for mysql, you first need to load the driver:

String driver = “com.mysql.jdbc.Driver”;

Next there are minimum of item you must declare:

Connection conn = null;
Statement st = null;

Now, lets connect:

String url = “jdbc:mysql://” + server_name + “:3306/”;

            conn = DriverManager.getConnection(url + db_name, username, password);
        } catch (SQLException ex)
            Logger.getLogger(MySQLtest.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("fail to open mysql connection");

where password, username, server_name and db_name are all strings you get from user input/file and so on.

next you need to create a statement:


            st = conn.createStatement();
        } catch (SQLException ex)
            Logger.getLogger(MySQLtest.class.getName()).log(Level.SEVERE, null, ex);

to view results or record sets, you can call the Execute XXX method, where XXX stands for the specific flavour you need (create, update, count or select).

more info on the statement class here.



Updating android UI from a background thread / timer

Like many OSes the Android OS doesn’t like the UI thread to be interfered from the outside. Many times, you need to update the main UI content with a timer/thread data.

Here is how:

  1. Create a Handler object (Note – the object might leak in certain circumstances – you might want to make it a static one.) like so : Handler handler;
  2. overwrite the handleMessage with the function you want updating the UI. You can use the Message object to pass some variables (you can, of course, extend this class):

    handler=new Handler()
    public void handleMessage(Message msg)
  3. You then call the handler.sendEmptyMessage(0); or equivalent from your thread / timer (there are several options there).


Handler class and memory leaks:

Since the handler instance is holding a reference to its creating\using class (be it an activity or a service class) the class will not be garbage collected till the handler itself can be collected (which will happen only when its run out of messages to process) this might poses a leak where a certain service or activity are not collected since the handler still has a reference to it.

To bypass this you can declare a static handler with a weak reference. Or, you can call the Handler.removeMessages() (there are more methods like this) when your activity is paused.

Closing Android socket

Assume we have a Socket that is connected to a server. Usually, we will use a secondary thread to read from the socket in a blocking connection.

If the user want to terminate the connection, in Java, we can call socket.close(). But not in Android (well, we can, but it will do nothing). While the thread is still alive and waiting, close() will not be executed and the connection will remain active. So, what can be done?

One way is to terminate the thread. This is obviously not a very good way, and denied the thread from executing its final code correctly.

The second and the right one, is simply to call Socket.shutdownInput() method. This will create a disconnection for the waiting thread, the thread will die according to plan and your application will run just fine!

Listing all network interfaces in Java

Mostly used for server programs, sometime you need to list all the available network interfaces.

To do this, we will use the InetAddress Java class.

Here is the code that will get all interfaces into a String array:

public static String[] getCurrentAddresses()
String [] ret_val=null;
InetAddress [] addrs=null;
catch (UnknownHostException ex)
Logger.getLogger(SingleServer.class.getName()).log(Level.SEVERE, null, ex);
int i=0;
ret_val=new String[addrs.length+1];
return ret_val;

Now, the thing to note here, is that we must add the (AKA localhost) to this list manually, assuming this is needed. localhost can only be used for local testing, and there for adding it is up to you.

Developing Android applications

First thing – get eclipse. Some people recommend the J2EE – I don’t. the regular java version works great for me. Eclipse doesn’t have an installer. That’s not a problem, just extract it where you can find it, say, c:\program files.

Next thing – you go to the android website and download the sdk.

Download the ADT Plugin

  1. Start Eclipse, then select Help > Install New Software.
  2. Click Add, in the top-right corner.
  3. In the Add Repository dialog that appears, enter “ADT Plugin” for the Name and the following URL for the Location:
  4. Click OK.
  5. mark all the options
  6. wait a very long time.

Next thing, you need to enable the ADT Plugin. to do that:

  1. Start eclipse
  2. Select Window > Preferences… to open the Preferences panel (Mac OS X: Eclipse > Preferences).
  3. Select Android from the left panel.
  4. For the SDK Location in the main panel, click Browse… and locate your downloaded SDK directory.
  5. Click Apply, then OK.

Note: there are a lot of stuff to download, in Gb! this will take a while.