Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

2013-05-23

Common MySQL / JDBC programming error that causes unexpected syntax errors

What is wrong with this code?

...
    String sql = "INSERT INTO PRODUCT (CODE, DESCRIPTION) VALUES(?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);
        
    pstmt.setString(1, "A1");
    pstmt.setString(2, "Soccer ball");
        
    pstmt.executeUpdate(sql);
...

On execution, the execute method returns a com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException.

Why is that? The syntax looks OK...

It's tricky, but since the statement is prepared in advance, the method to call is pstmt.executeUpdate() without parameters.
If the SQL query is provided, the compiler accepts the code, but the '?' produce syntax errors.

PreparedStatement is preferred over Statement because it can give better performance and prevent many SQL Injection attacks.

The correct code is:

...
    String sql = "INSERT INTO PRODUCT (CODE, DESCRIPTION) VALUES(?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);
        
    pstmt.setString(1, "A1");
    pstmt.setString(2, "Soccer ball");
        
    pstmt.executeUpdate();
...


Credits: Mark Matthews

2012-12-28

How to install Jena command line tools?

Apache Jena is an open-source library for building Semantic Web applications using the Java programming language.
At the core of the library is a RDF implementation.
The SPARQL query language can be used to query RDF models.

The distribution includes a set of command line tools that can be very useful.
To install them, follow the next steps:

1. Download Jena binaries from http://www.apache.org/dist/jena/

2. Unzip file to an installation folder

3. Define the environment variable JENAROOT

4. Add %JENAROOT%\bat to the PATH environment variable

At the present moment, the following tools are available:

qtest
query
rdfcat
rdfcompare
rdfcopy
rdfparse
rset
RuleMap
schemagen
sparql
turtle
version

Consult the javadoc (jena package) for more details.

2012-10-04

Groovy one-liner to extract file name without extension

Does groovy have an easy way to get a filename without the extension? - Stack Overflow:

file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}

 or with a simple regexp:

file.name.replaceFirst(~/\.[^\.] $/, ''

2012-09-21

You code, you learn - flat Java files

"I don't need to keep my java files in all those boring package name folders (e.g. /net/proj/...). I'll just keep them all in a single folder then let the compiler worry about it"

The above statement is true, but you should known that the IDEs like Eclipse, Netbeans, IntelliJ, assume that the java files are stored in folders according to the package structure.

The IDEs will report errors when you try to open the project in them. And sooner or later, you or someone on your team will want to do it.

The renaming process can be eased using the Eclipse refactor-move functionality, but it will be a pain, especially if your are using version control.

I wasted 2 days to rearrange the Java files to the standard structure using Eclipse and SVN.

You code, you learn!

2012-09-20

You code, you learn - Groovy to Java

Here is one mistake you might end up doing:

"I'll code in Groovy first and then convert to Java. It should be a piece of cake!"

Be prepared for a bitter and hard to chew cake!

Groovy is very loose on typing so you'll end up doing all sorts of accidental programming errors, for instance, you can easily use methods that are not accessible on a static context. Generics are also a pain.

To convert the code you will have to do major refactoring with redesign of the class hierarchies.

I wasted 2 weeks on a project when I had to convert a 200 class application from Groovy to Java.

You code, you learn!

2012-08-19

Khan Academy Programming

Coders Get Instant Gratification With Khan Academy Programming | Wired Enterprise | Wired.com: The tutorials are interactive and live entirely in the browser. Instead of a video, each lesson contains a pane on the left side for students to enter code and a pane on the right that displays the output. The first lesson walks students through the process of writing code that will draw a face in the right pane. After learning to generate graphics, students work up to animation and eventually to games, such as a Pac-Man clone.

2012-05-03

Java String print tip

Are you getting confused with all the substring() indices during a debug session?
Try the following code:
System.err.println(myStr);
for (int i=0; i < myStr.length(); i++)
    System.err.print(i % 10);

You will get the following output:
my string value
012345678901234

% performs a division remainder operation, giving, in this case, always a number between 0 and 9.
And the numbers in the next line make it so much easier to see the string indices! :)

2012-02-24

Setting up a Java programming environment

Author of this guide: Joana

Software needed for the programming environment.

Note:
- to avoid discrepancies, please install exactly the versions mentioned here as they are the same provided in the laboratory machines
- also deactivate all auto-update options in Eclipse to keep your environment as similar as possible to the one provided in the laboratory

----------------------------------

Software:

Requirements:
- You can install the needed software in your preferred operating system, including Windows, Linux and Mac OS X.

--- --- --- ---

Most tools have a command line (shell) interface that needs to be installed in order to solve some problems along the way during our course.
In Windows XP consider installing the PowerToy "Open Command Here".
http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx
Windows 7 already provides that feature built-in in the secondary context menu (available when holding shift and right-clicking a folder).
In Mac OX there is a similar tool called 'Go2Shell'
http://itunes.apple.com/us/app/go2shell/id445770608?mt=12

--- --- --- ---

Environment variables:
You can define system and user environment variables.
Each operating system provides this through different ways.
The main variables are:

PATH
To allow the operating system to found the binaries of the programs you want to be running in the shell (console) you need to include all the relevant paths in the system's environment variable PATH. This allows you to call just 'javac', 'java', 'ant', and 'svn' and the system will know where to find the necessary binaries.

CLASSPATH
When using Java's Development Kit (JDK) another interesting variable is the CLASSPATH. It informs the java program where to look for the needed libraries. However there are better ways to achieve this goal, namely using the Ant build files.

To further explore this topic, consider reading the article on Wikipedia:
http://en.wikipedia.org/wiki/Environment_variable

--- --- --- ---

LIST:

Java Developer Kit ~ JDK
Java Runtime Environment ~ JRE
-> follow the installation instructions on official webpage:
http://www.oracle.com/technetwork/java/index.html
http://www.oracle.com/technetwork/java/javase/index-137561.html
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

--- ---

Eclipse IDE for Java EE Developers
Tools for Java developers creating Java EE and Web applications, including a Java IDE, tools for Java EE, JPA, JSF, Mylyn and others. It requires JRE to run.
Instructions can be found here: http://wiki.eclipse.org/Eclipse/Installation

It needs to be enriched with 2 plugins:

- Eclipse Subversive - SVN Plugin that allows the integration of eclipse with SVN tools and visually navigate SVN repositories like the one where the group's projects will be hosted.
It consists in two parts: Subversive plug-in and Subversive SVN Connectors. Both parts are required in order to work with Subversive, so you need to install Subversive plug-in and pure Java SVNKit connector.
http://www.eclipse.org/subversive/downloads.php#indigo_stable

- Google Plugin for Eclipse (GPE) adds functionality to Eclipse for creating and developing Google Web Toolkit (GWT) applications. GWT is downloaded with the GPE. It will assist the developers in the building of browser-based applications (user interface) for the project. GWT compiles your Java source code into optimized, stand-alone JavaScript files that automatically run on all major browsers, as well as mobile browsers for Android and the iPhone. It will also be used by the distributed systems course students with the JBoss Application Server.

A third plugin can also be useful:
- Log Watcher adds a view to Eclipse that allows log files to be monitored for changes, similar to the Unix tail utility.
http://graysky.sourceforge.net/

--- ---

- GWT browser plugin - GWT Development Mode needs a browser plugin to operate. Please install the appropriate one for your browser. It integrates with a variety of browsers and lets you debug GWT code from within the Eclipse IDE.

--- ---

- Apache Ant 1.8.2 - Ant is a Java library and command-line build tool similar in purpose to Make. It helps with the typical tasks of software development: compile, build, deploy, install and clean. It can be run from inside the Eclipse IDE (in the proper View) but you also need to run it in the command line.

--- ---

- Apache Subversion 1.7.2 - Subversion is a free/open source version control system (VCS). It manages files and directories, and the changes made to them, over time. This allows you to recover older versions of your data or examine the history of how your data changed and who changed it.
Eclipse Subversive plugin (Eclipse section) provides a graphical interface. You need a client that can run in the shell. There are other standalone graphical interfaces like TortoiseSVN, SVNx, etc. In any case, you need a working command line version.

In this page you can find several clients binary packages:
http://subversion.apache.org/packages.html
We do not need you to install a server because the project will be hosted in the sigma server machine and will be accessed through the SVN+SSH protocol.

----------------------------------





2011-11-17

Computer Programming for Children

New and more sophisticated tools are changing the way that the next generation learns to program computers. Children can now create elaborate scenes and games without the cryptic commands that were once the only way to tell computers what to do. The most talented children can also use some of the sophisticated tools normally used by professional programmers, because the tools are now often easy enough for someone to pick up with only a few months of study.

Source: NYTimes.com

2011-10-03

Selecting the optimal programming language

There are many programming languages to choose from, and it's a personal choice for many--you might just pick your favorite, or you might choose the one with the best performance figures. Sometimes, however, other factors are just as important as performance. In this article, learn how to analyze the relevant factors when selecting a programming language. A few project scenarios are outlined to illustrate different variables in your myriad choices.

Source: IBM Developer Works

2011-06-07

Energy-efficient programming

A University of Washington project sees a role for programmers to reduce the energy appetite of the ones and zeroes in the code itself. Researchers have created a system, called EnerJ, that reduces energy consumption in simulations by up to 50 percent, and has the potential to cut energy by as much as 90 percent.

Source: Code green: Energy-efficient programming to curb computers’ power use

2011-01-31

Big Oh notation explained

Assuming a hypothetical computer called the Random Access Machine where:
  • Each simple operation takes exactly one time step.
  • Loops and subroutines are not considered simple operations, but a composition of many single-step operations.
  • Each memory access takes exactly one time step.
  • Memory is unlimited.
The run time is measured by counting up the number of steps an algorithm takes on a given problem instance. We then have the worst, average, and best case complexity functions.


The Big Oh notation further simplifies function classification by disregarding multiplicative constants and defining an upper bound.

f(n) = O(g(n)) means c . g(n) is an upper bound on f(n).
Thus there exists some constant c such that f(n) is always <= c . g(n), for large enough n >= n0 (for some constant n0).


Reference: Algorithm Design Manual by Steven Skiena, book and web site.

2010-10-11

Groovy regular expression operators

The Java regular expression (regex) typical invocation sequence is:

     Pattern p = Pattern.compile("a*b");
     Matcher m = p.matcher("aaaaab");
     boolean b = m.matches();

A matches method is defined by the Pattern class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement

    boolean b = Pattern.matches("a*b", "aaaaab");

Groovy is a promising new language for the Java platform that includes the following regular expressions operators that greatly simplify the use of java.util.regex:

~ creates a Pattern from String

=~ creates a Matcher, and in a boolean context, it is "true" if it has at least one match, "false" otherwise.

==~ tests if String matches the pattern

2010-07-13

Is there a general way to code to avoid concurrency problems?

(...) Brian Goetz pointed it out to me. (...) The technique is to never write any multi-threading or synchronizing code, except in specialist classes which handle all the issues under the covers.

Ideally, you use what has already been developed by the experts out there: use ConcurrentHashMap rather than HashMap as your default Map; use a BlockingQueue to pass things between threads without having to think about synchronization to share the data; GC is dirt cheap now for short-lived objects so don't think twice about copying out data to a temporary object for manipulation or iterating; and so on.

(...) Inevitably you will have to write some classes yourself that handle concurrency issues. (...) You should isolate that concurrency managing code into separate dedicated classes and, most importantly, spend a lot of time getting those classes right and reusable because concurrency is hard, even for the experts.

--Jack Shirazi

2010-04-30

Java regular expressions testbed

Regular expressions are one of the most useful programming tools, particularly for text processing. Java has a nice implementation, stored in the java.util.regex package.

The following page by David Matuszek contains a Java Applet to test regular expressions. I use it a lot and recommend it!

http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html

Have fun!

2010-04-18

Java tools I had never used

...until recently.

javap - The Java Class File Disassembler - Converts class files back to java source code.

JConsole - monitoring tool that uses the extensive instrumentation of the Java Virtual Machine to provide information about the performance and resource consumption of applications running

2010-03-24

My developer workbench

A very dear friend is having a hard time getting back into 'developer shape', so I decided to blog about how I organize my developer work files:

/dev
This is where I keep the current projects I am working on.
It is important to me to keep this folder as clean as possible.
If I have a project that is inactive, I just create a ZIP archive and delete it.

/dev/examples
This is where I keep code examples, historically called the 'grrreat tests' :-) (Miguel and João, you know why)
The idea is to keep small projects, each with a single purpose. It is the best way to test libraries before using them in larger projects.
I get snippets from here all the time. If you don't know, snippets are fragments of code that can be copy-pasted and easily reused.

/devlib
This is a collection of libraries. You can save a lot of time reusing existing code and learning from it.
The collection is organized in 3 sections: dist, doc, and source; for binaries, documentation, and source code, respectively.
I keep multiple versions of libraries, each in a separate subfolder, because sometimes it is useful to compare changes, using a tool like WinMerge.


If you only take away one thing, remember that single-purpose code examples are the way to write quality code, fast!

Feel free to share comments about your development practices. I'm always looking for ways to improve!

2010-01-04

Homoiconicity

A programming language has the homoiconicity property when the primary representation of the programs is also a data structure in a primitive type of the language itself. It makes it easier to metaprogram.

Of course, LISP s-expressions (symbolic expressions) come to mind...

(function arg1 arg2 ...)

I knew there had to be an advantage in all those parenthesis ;-)

2009-12-15

Erlang at Facebook

These are some excerpts from a presentation about the Facebook chat implementation.



System challenges
- How does synchronous messaging work on the Web?

- "Presence" is hard to scale
-- Need a system to queue and deliver messages
-- Millions of connections, mostly idle
-- Need logging, at least between page loads

(...)




Architectural details of Channel servers
- Distributed design
- User id space is partitioned (division of labor)
-- Each partition is serviced by a cluster (availability)
- Presence aggregation
-- Channel servers are authoritative
-- Periodically shipped to presence servers
- Open source: Erlang, Mochiweb, Thrift, Scribe, fb303, et al

(...)

Key Erlang Features we love:
- Concurrency (based on user-mode threads)

- Distribution Connected network of nodes
-- Remote processes look like local processes
-- Any node in a channel server cluster can route requests
-- Naive load balancing

- Fault Isolation

- Error logging
-- Stacktraces point the way to bugs (functional languages win big here)

- Hot code swapping

- Monitoring and Error Recovery
-- Supervision hierarchies
-- Organize (and control) processes
-- Organize thoughts
-- Systematize restarts and error recovery
-- simple_one_for_one for dynamic child processes

- net_kernel (Distributed Erlang)
-- sends nodedown, nodeup messages
-- any process can subscribe
-- heart: monitors and restarts the OS process

- Remote Shell
-- Ad-hoc inspection of a running node
-- Command-and-control from a console
-- Combines with hot code loading

- Erlang top (etop)
-- Shows Erlang processes, sorted by reductions, memory and message queue
-- OS-like functionality ... for free

- Hibernation

- Symmetric MultiProcessing (SMP)
-- Take advantage of multi-core servers
-- erl -smp runs multiple scheduler threads inside the node
-- SMP is emphasized in recent Erlang development

- hipe_bifs
-- hipe_bifs:bytearray_update() allows for destructive array assignment
-- Cheating single assignment because in Erlang is destructive assignment is hard because it should be

Reference: Erlang at Facebook by Eugene Letuchy on Apr 30, 2009