Archive for January, 2007

PHP - Loops

Posted in: PHP, Examples by Lisa on Wednesday, January 31, 2007 @ 6:20 am

We use loops to repeat execution of block of code.
There are several loop types. Depending on situation, sometimes it is better to use one kind of loop than another, but every loop can be transformed into another one. This is not so important for beginners, you just need to know that there is good and better use of particular loop.

We have following loops:

for (*1; *2; *3)
{
// block
}

for loop is usually used when we know exactly how many times we need to execute repeating block of code.

for loop has three parameters. First, *1 is statement, and it is executed at the loop very start, and it often used for counter initialization. Second, *2 is a condition which is true or false. Condition is checked at every iteration start. If condition is true, execution will continue, if it is false execution will break. Third, *3 is statement and it is executed at the end of every loop iteration. It is usually used for counter increasing.

while (*)
{
// repeating block of code
}

Continue reading 'PHP - Loops'

Comments (1)| Share This

PHP - Data types and conversions between them

Posted in: PHP, Examples by Lisa on Tuesday, January 30, 2007 @ 8:14 am

PHP variables holds data. Data has a type. It is a number, text, array… There are four basic, two compound and two special types.
Basic types are integer, float, string and boolean.
Compound types are array and object,
Special types are resource and NULL.

When you are declaring variable, you can declare its type, like this:

integer $variable;
string $variable;
boolean $variable;

You can also declare variable without datatype, in which case PHP will try to determine type of data which a variable holds. Declaring variable without a type is simple, just:

$variable;

Continue reading 'PHP - Data types and conversions between them'

Comments (0)| Share This

Implementing Simple Java GZIP Utility

Posted in: Java, Examples by Sasa on Friday, January 26, 2007 @ 7:04 pm

This examaple shows how to implement a GZIP program in Java as a command-line utility. It is demonstrating the use of Java I/O streams and shows the simplicity of plugging one stream type into another.

Continue reading 'Implementing Simple Java GZIP Utility'

Comments (0)| Share This

Java JDBC Example - Basic Database Operation

Posted in: Java, Examples by Sasa on Friday, January 26, 2007 @ 6:49 pm

This is a simple code that shows how to use JDBC. It shows how to select data from the northwind database running on localhost to which we connect using USERNAME/PASSWORD login. This is the basic database operation. INSERT/UPDATE/DELETE are even simpler since they don't require looping through the result set.

Continue reading 'Java JDBC Example - Basic Database Operation'

Comments (2)| Share This

RSS and PHP SimpleXML Example

Posted in: PHP, Examples by Lisa on Tuesday, January 23, 2007 @ 9:52 am

RSS is one of the most popular applications of XML on the web. Almost every major media company exports its content as an RSS feed.

This example shows how to use SimpleXML, a feature of PHP 5, to parse an RSS feed.

Continue reading 'RSS and PHP SimpleXML Example'

Comments (2)| Share This

Spark it Up

Posted in: Cool Tech, News & Events by Marko on Monday, January 22, 2007 @ 10:52 pm

After years of developing our app on PHP and MySQL, we hit the ceiling. The lack of frameworks and structure made further development just too costly. Adding all the new features that we have in the pipeline wasn't fun any more.

So we went to the drawing board and came up with the new platform. After researching all the technologies and frameworks out there, many of which are are providing training on, we chose Java with Spring and Hibernate frameworks.

Welcome Spark!

Marakana Spark

The new Spark platform lets us crank new features in days with a rock-solid reliability. Find out more about it - come to the next San Francisco Java Meetup and you'll get to peak under the hood.

Comments (0)| Share This

The Way Of Web 2.0 Development

Posted in: Cool Tech by Marko on Monday, January 22, 2007 @ 10:44 pm

Want to read a really great article about how to built super useful Web 2.0 apps? Check out the free Getting Real publication by 37Signals.

This one really resonates with us and explains the basic philosophy behind Ruby on Rails platform.

Comments (0)| Share This

PHP - Hello World Example

Posted in: PHP, Examples by Marko on Wednesday, January 17, 2007 @ 8:39 am

Today, we are starting the PHP examples series. Almost every programming class, book or a tutorial starts with the Hello World example.
We are going to honor this tradition by making Hello World our first PHP example in this series.

This is your first PHP application.
As you can see, you must escape PHP code from HTML with

<? PHP Code here ?>

Another one, commonly used is

<?php PHP Code here ?>

echo is a command for printing messages.

Continue reading 'PHP - Hello World Example'

Comments (0)| Share This

Java JSP - Login System Example

Posted in: Java, Examples by Sasa on Wednesday, January 17, 2007 @ 8:04 am

Here is an example of a simple login system.

<html>
<body>
<form action="login.jsp" method="post">
    <h1>Please Login</h1>
    Login:    <input type="text" name="login"><br>
    Password: <input type="password" name="password"><br>
    <input type=submit value="Login">
</form>
</body>
</html>
Login JSP Page
<%
String login = request.getParameter("login");
String password = request.getParameter("password");
if ( login.equals("admin") && password.equals("guess") ) {
        // Valid login
        session.setAttribute("authorized", "yes");
} else {
        // Invalid login
        session.setAttribute("authorized", "no");
}
%>
<html>
<body>
<h1>Login Confirmation</h1>
You logged in with <%= login+"/"+password %>
<br><br>
<a href="secret.jsp">See the secret</a>
</body>
</html>
The Secret Page
<jsp:include page="secret_include.jsp"/>
<html>
<body>
<!– Assume user is logged in and good to go –>
<h1>Welcome</h1>
Here is the secret
</body>
</html>
The Code Protecting The Secret
<%
String authorized = (String)session.getAttribute("authorized");
if ( authorized.equals("no") ) {
%>
<jsp:forward page="login.html"/>
<%
}
%>

Comments (12)| Share This

Java JSP - Simple JSP Example Example

Posted in: Java, Examples by Sasa on Wednesday, January 17, 2007 @ 7:58 am

This example illustrates how to write a simple JavaServer Page. Here we implement a game of Craps:

1. Roll two dice
2. If the sum of the two is 7 or 11, you win

Continue reading 'Java JSP - Simple JSP Example Example'

Comments (1)| Share This

Java JDBC - Fetching a Record Example

Posted in: Java, Examples by Sasa on Wednesday, January 17, 2007 @ 7:55 am

This program gets a customer from a NorthWind-like database.

The example demonstrates the use of prepared statements, SQL SELECT * calls, and use of result meta info.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ShowCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

Continue reading 'Java JDBC - Fetching a Record Example'

Comments (0)| Share This

Java JDBC - Updating a Record Example

Posted in: Java, Examples by Sasa on Monday, January 15, 2007 @ 8:59 am

This program modifies a customer in a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL UPDATE calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ModifyCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

Continue reading 'Java JDBC - Updating a Record Example'

Comments (0)| Share This

Java JDBC - Listing Records Example

Posted in: Java, Examples by Sasa on Sunday, January 14, 2007 @ 4:02 pm

This program lists customers from a NorthWind-like database.

The example demonstrates the use of JDBC statements and SQL SELECT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
ListCustomers
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

Continue reading 'Java JDBC - Listing Records Example'

Comments (0)| Share This

Java JDBC - Inserting a Record Example

Posted in: Java, Examples by Sasa on Sunday, January 14, 2007 @ 1:15 pm

This program adds a customer into a NorthWind-like database.

The example demonstrates the use of prepared statements and SQL INSERT calls.

To run this example against a MySQL database you would do:

/path/to/java
-Djdbc.drivers=com.mysql.jdbc.Driver
-classpath .:mysql-connector-java-3.1.8-bin.jar
AddCustomer
"jdbc:mysql://my.server.com:3306/Northwind?user=myuser&password=mypassword"

Continue reading 'Java JDBC - Inserting a Record Example'

Comments (0)| Share This

Java Networking - Web Server Example

Posted in: Java, Examples by Sasa on Saturday, January 13, 2007 @ 6:28 pm

This program demonstrates a simple multi-threaded web server.

This example demonstrates:

- Server Socket
- Reading/writing from/to sockets
- Threads
- Reading files from disk
- Regular Expression matching
- Date formatting
- Nested (inner) classes

Continue reading 'Java Networking - Web Server Example'

Comments (4)| Share This

RSS Feed   Subscribe to our blog