Showing posts with label databases. Show all posts
Showing posts with label databases. 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

2011-07-01

World's data will grow 50 times

In 2011 alone, 1.8 zettabytes (or 1.8 trillion gigabytes) of data will be created, the equivalent to every U.S. citizen writing 3 tweets per minute for 26,976 years. And over the next decade, the number of servers managing the world's data stores will grow by ten times.

Interestingly, the amount of data people create by writing email messages, taking photos, and downloading music and movies is minuscule compared to the amount of data being created about them, the EMC-sponsored study found.

The IDC study predicts that overall data will grow by 50 times by 2020, driven in large part by more embedded systems such as sensors in clothing, medical devices and structures like buildings and bridges.

Source: Computerworld

2009-07-04

No to SQL

It appears there is a "rebellion" against relational databases, as reported in Computer World.

This is the typical problem when you just use off-the-shelf software without considering other options.

Some years ago, Prof. PMMVAS, in a class, presented a good summarization of the trade-offs in the database world:

Data structure complexity VS Query capabilities

Relational databases have simple data structures (tables) but sophisticated querying capabilities (SQL SELECT).

Hierarchical databases (e.g. XML) have more complex data structures (records) and less powerful queries.

Object databases have complex data structures (object graphs, code+data encapsulation) but poor querying capabilities.


It is an interesting discussion on how to keep our digital "bookshelves" organized...