Java : Remove all occurrences of char from string

So if you wish to remove all occurences of a certain char from a string you just need to use the String in-built function  – replace(char,emptyChar).

For example, I wish to replace all occurences of ‘:’ from

String str = “bc:14:01:b0:3a:b8”;

To do so I replace the char ‘:’ with the empty one “” like this:

str = str.replace(“:”, “”);

And I get the desired output:

bc1401b03ab8

Yup, Is this simple ;).

Java – MYSQL – Prepared Statements – Insert Batch

//create prepared statement, bind it to db connection and SQL query.
PreparedStatement = dbConnection.prepareStatement(insertTableSQL);
 
//For each input value set input type, input position and value.
preparedStatement.set<inputType>(<index>, <value>);
(...)
preparedStatement.set<inputType>(<index>, <value>);


//For each new row to be inserted add it to batch 
preparedStatement.addBatch();
 
//After adding to batch all rows to be inserted. 
//It's time to call execute batch.
preparedStatement.executeBatch();


For a complete example please see source.

(Source: http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/)

Java – How to get current datetime

I want to thank mkyong for sharing this with the world. (source: http://www.mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/)

Basically if you want to get the current datetime in Java there are two ways:

  1. Using Date() + SimpleDateFormat()
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println(dateFormat.format(date));
  2. Using Calender() + SimpleDateFormat()
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    System.out.println(dateFormat.format(cal.getTime()));

Making My First Android Application – Hello World!

Now that I have set my development environment it’s time to make my very first application. The very well known “Hello World!”.

For this I follow another one of TugaBest tutorials that he shared in AndroidPT Forum and that you can access it here.

The first thing that it is needed is a AVD (Android Virtual Device) to emulate a device. To create one, on window console,go to android-sdks directory and simply type :

>android list targets

A list of possible devices and android versions is presented to you. In my case, I’m going to chose one that is running android 2.2 (id = 1). So I type

>android create avd -t 1 -n my_avd_1 -a

(-t means target, -n means name, -a means to create snapshot)

say no to create a custom hardware profile. Because you are not doing any changes to the default profile.

Now it’s time to create a new project in Eclipse

File > New > Project > Android > Android Application Project.

The New Android Application Window opens and I filled all the fields.

  • Application Name
  • Project Name
  • Package Name
  • Minimum Required SDK (I chose API 8: Android 2.2 (Froyo))
  • Target SDK (I chose API18: Android 4.3)
  • Compile With (API18 Android 4.3)
  • Theme

Then I pressed Next.

On the next window I’ve checked:

  • Create custom launcher icon
  • Create activity
  • Create Project in Workspace

And then Next again.

The next window lets me customize the logo for my application. After customization I pressed Next.

Its now time to create a new Activity.
An Activity is the simplest entity used to execute actions. The same application can have several activities. But the user can only interact with each one of a time.

I chose to start with a Blank Activity.

And then Next again.

Then finally I supplied:

  • The Activity Name  (HelloWolrd)
  • Layout Name (activity_hello_world)
  • Navigation Type (None)

And then pressed Finish.

A little more theory…

An Android Interface is a combination of object hierarchies. These objects are called Views and they are of drawable type.  Each of these drawable objects/views is used as a layout element (such as, a button, an image or a label). And each type of element it’s a subclass of View. For example, the subclass of View that represents Text is called TextView.

In the image is listed all the steps needed to display the text “Hello, World!”.

First we need to create the subclass of view that represents the Text Element.

TextView tv = new TextView(this);

The input (this) of the constructor its an Android Context. A Context is an handler to the system. It stores services like resources, database access and settings. Each Activity and therefore each subclass of Activity inherits from Context.

After creating the object/view we need to set the actual text to be displayed. So we need to add the line:

tv.setText(“Hello, World!”);

In order to be seen in the monitor we need to set a UI for this object. That’s why we need to add the line:

setContentView(tv);

Accordingly to the amazing tutorial posted by TugaBest in AndroidPT Forum. The only thing that it’s missing is to run the created app.

To do it so,

Go to Eclipse Menu Run > Run

On the next window (Run Configurations) select your app under Android Application and run it.