Get character from ASCII code in PHP, Java and JavaScript

The lower case letter „a“ has ASCII code „97“ and can be generated in PHP like this:

<?php
  $a = chr(97);
  echo $a;
?>

In JavaScript, the same can be accomplished by:

<script type="text/javascript">
  var a = String.fromCharCode(97);
  document.write(a);
</script>

And this is how it works in Java:

public static void main(String[] args){
  String a = new Character((char) 97).toString();
  System.out.println(a);
}

For and foreach loop example in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args)
{
  String[] firstNames =
  {
    "Marge", "Homer", "Lisa", "Bart", "Maggie"
  };
 
  System.out.println("- With for loop:");
 
  for (int i = 0; i < firstNames.length; i++)
  {
    System.out.println(firstNames[i]);
  }
 
  System.out.println("- With foreach loop");
 
  for (String name : firstNames)
  {
    System.out.println(name);
  }
}

MySQL NOW() equivalent in Java for prepared statements

If you are used to MySQL then you probably know the MySQL function NOW() which inserts the current date and time in a MySQL query. But if you use JDBC and prepared statements, then you can reconstruct this function with a GregorianCalendar (which is the successor of Date):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void insertArticle(Article article) throws SQLException
{
  //query = "INSERT INTO articles(title,content,date) VALUES (?,?,NOW())";
  query = "INSERT INTO articles(title,content,date) VALUES (?,?,?)";
 
  statement = connection.prepareStatement(query);
 
  statement.setString(1, article.getTitle());
  statement.setString(2, article.getContent());
  statement.setTimestamp(3, new Timestamp(new GregorianCalendar().getTimeInMillis()));
 
  logger.info(statement);
  statement.executeUpdate();
}

How to use Enums for String values

That’s how you can use enumerations for string values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * ISO 3166
 */
public enum Country
{
  DE
  {
    @Override
    public String toString()
    {
      return "Germany";
    }
  },
  IT
  {
    @Override
    public String toString()
    {
      return "Italy";
    }
  },
  US
  {
    @Override
    public String toString()
    {
      return "United States";
    }
  }
}
1
2
3
4
5
6
public static void main(String[] args)
{
  System.out.println(Country.DE); // Germany
  System.out.println(Country.IT); // Italy
  System.out.println(Country.US); // United States
}

Test expected exceptions with JUnit 3 and JUnit 4

With JUnit 4 it is pretty simple to test if an exception has been properly thrown because you can use annotations:

1
2
3
4
5
6
7
8
9
10
11
12
13
import static org.junit.Assert.fail;
import org.junit.*;
 
public class MyTestClass
{
 
  @Test(expected = NoMatchFoundException.class)
  public void testSomething() throws NoMatchFoundException
  {
    // Your critical code here!
    fail();
  }
}

If you are using JUnit 3 then it is not that easy because you don’t have annotations. That’s why you have to catch exceptions in the test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import junit.framework.TestCase;
 
public class MyTestClass extends TestCase
{
  public void testSomething() throws NoMatchFoundException
  {
    try
    {
      // Your critical code here!
    }
    catch (NoMatchFoundException ex)
    {}
  }
}

Connecting to a MySQL Database with Java (JDBC)

In the beginning of my study time I’ve programmed a MySQL connection in Java for a music collection. After 2 years now I have made a remake of [post id=816]this code[/post] to show how to use the Java Database Connectivity (JDBC). All you need is mysql-connector-java-5.0.8-bin.jar.

I’ve written a small sample for the connection with a WordPress database.
Connecting to a MySQL Database with Java (JDBC) weiterlesen