You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by sieg <si...@heintze.com> on 2007/07/17 16:56:05 UTC

Wanted: Working example of JDBC4 Annotations.

I'm trying to run the sample code in "A Tutorial Java 6 New Features" by Budi
Kurniawan page 167. The code for the book is available at
http://books.brainysoftware.com/download/jdk6Samples.zip. Chapter 8 is the
database chapter.

Neither the java command line javac nor the java6 compiler used by eclipse
can find the class java.sql.DataSet.

What do I have to do to make to compile the following code?

Better yet, does someone have some sample JDBC4 code that demonstrates using
annotations for SQL queries (insert, delete, select, update etc...) that
compiles and runs?

I did some google searching and found some JDBC4 tutorials, but the code
fragments did not include the import statements!

I was told that the only low cost database that includes a JDBC4 driver is
IBM's Apache Derby. Is this true?

Thanks,
Siegfried

Here is some sample code from the above zip file (I added the bash procedure
in the comments).




package query;
import java.sql.Connection;
import java.sql.DataSet;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
 * Begin commands to execute this file using Java with bash
 * export DERBY_HOME=/cygdrive/c/dev/derby/10.2.2.0
 * /cygdrive/c/dev/derby/10.2.2.0/bin/ij <<EOF
 * CONNECT 'jdbc:derby:test;create=true';
 * DROP TABLE FIRSTTABLE;
 * CREATE TABLE FIRSTTABLE
 *   (ID INT PRIMARY KEY,
 *   NAME VARCHAR(12));
 * INSERT INTO FIRSTTABLE VALUES (10,'TEN'),(20,'TWENTY'),(30,'THIRTY');
 * SELECT * FROM FIRSTTABLE;
 * EOF
 * javac -g QueryDemo.java
 * cd ../
 * javaw query.QueryDemo
 * cd query/
 * End commands to execute this file using Java with bash
 *
 */
public class QueryDemo {

    public static void main(String[] args) {

        Connection connection = null;
        try {
            String url = "jdbc:inetdae7:localhost:1433?"
                    + "database=Adaptor";
            String login = "sa";
            String password = "admin";
            connection = DriverManager.getConnection(url, login,
                    password);

            // Create Query object
            RoleQueries qo = connection
                    .createQueryObject(RoleQueries.class);

            // Get all roles
            DataSet<Role> rows = qo.getAllRoles();
            for (Role role : rows) {
                System.out.println(role);
            }

            // Create new Role object
            if (! rows.isReadOnly()) {
                System.out.println("\nCreate new role");
                Role r = new Role();
                r.role_id = 12345;
                r.name = "Supervisor";
                r.description = "Do monitoring job";
    
                boolean insertResult = rows.insert(r);
                rows.sync(connection);
                System.out.println("\tInserted: " + insertResult);
            }

            // Retrieve Role by name
            System.out.println("\nGet role by name:");
            DataSet<Role> rows2 = qo.getRoleByName("Supervisor");
            Role role = rows2.get(0);
            System.out.println(role);

            if (role != null) {
                // Modify Role
                System.out.println("\nModify current role:");
                role.description = "Do supervising job";
                boolean modifyResult = rows2.modify(role);
                rows2.sync(connection);
                System.out.println("\tModified: " + modifyResult);
            }
        } catch (SQLException e) {

            for (Throwable t : e) {
                t.printStackTrace();
            }
        } finally {

            // Close connection
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}






import java.sql.BaseQuery;
import java.sql.DataSet;
import java.sql.Select;
public interface UserQueries extends BaseQuery {

    // Select all users
    @Select(sql = "SELECT userId, firstName, lastName FROM Users", 
            readOnly=false, connected=false, tableName="Users")
    DataSet<User> getAllUsers();

    // Select user by name */
    @Select(sql = "SELECT userId, firstName, lastName FROM Users "
            + "WHERE userName=?", readOnly=false, connected=false,
            tableName = "Users")
    DataSet<User> getUserByName(String userName);

    // Delete user
    @Update("DELETE Users WHERE firstName={firstName} " +
            "AND lastName={lastName}")
    int deleteUser(String firstName, String lastName);
}

class User {
    public String userId;
    public String firstName;
    public String lastName;
    
}
-- 
View this message in context: http://www.nabble.com/Wanted%3A-Working-example-of-JDBC4-Annotations.-tf4097303.html#a11650724
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Wanted: Working example of JDBC4 Annotations.

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Siegfried,

This is a bit confusing. Sun decided to distribute Apache Derby and  
call it JavaDB. The bits you download from the JavaDB site and the  
bits you get already installed when you install Sun's Java SE 6 JDK  
and the bits you can download from the Apache Derby site are the same  
(modulo release levels and documentation). [Also, Sun will offer  
support for the JavaDB bits but not the Apache Derby bits.]

more below.

On Jul 17, 2007, at 8:52 AM, sieg wrote:

>
> Could you tell me more about javaDB? I did a google search and found
> http://developers.sun.com/javadb/ and I see a download button.
>
> Is it the same as the apache derby that I already downloaded and  
> installed?

yes.
>
> It also says "Now in Java 6". Since I have java 6 installed, do I  
> have it
> installed twice?

yes.
>
> When I tried to figure out how to start the server process for Java  
> DB (I
> figured there must be a server process unlike Microsoft Access) I  
> clicked on
> the quick start link and discovered I'm back to apache derby which  
> I already
> have installed (quite seperately from Java 6) and running.

If you're happy with the Apache Derby installation then there is no  
need to install or use the JavaDB either from download or from Java  
SE 6 JDK.

Regards,

Craig
>
> I'm confused.
> Thanks,
> Siegfried
>
> -- 
> View this message in context: http://www.nabble.com/Wanted%3A- 
> Working-example-of-JDBC4-Annotations.-tf4097303.html#a11652174
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Wanted: Working example of JDBC4 Annotations.

Posted by sieg <si...@heintze.com>.
Could you tell me more about javaDB? I did a google search and found
http://developers.sun.com/javadb/ and I see a download button.

Is it the same as the apache derby that I already downloaded and installed?

It also says "Now in Java 6". Since I have java 6 installed, do I have it
installed twice?

When I tried to figure out how to start the server process for Java DB (I
figured there must be a server process unlike Microsoft Access) I clicked on
the quick start link and discovered I'm back to apache derby which I already
have installed (quite seperately from Java 6) and running.

I'm confused.
Thanks,
Siegfried

-- 
View this message in context: http://www.nabble.com/Wanted%3A-Working-example-of-JDBC4-Annotations.-tf4097303.html#a11652174
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Wanted: Working example of JDBC4 Annotations.

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Hi,

The JDBC annotations, did not make the final release of the JDBC 4.0 
spec and is not available in Java SE 6.

It looks the book was released prior to java SE 6 going final.

Currently Java DB is one of the few JDBC drivers which supports JDBC 4.0.

Regards
Lance

sieg wrote:
> I'm trying to run the sample code in "A Tutorial Java 6 New Features" by Budi
> Kurniawan page 167. The code for the book is available at
> http://books.brainysoftware.com/download/jdk6Samples.zip. Chapter 8 is the
> database chapter.
>
> Neither the java command line javac nor the java6 compiler used by eclipse
> can find the class java.sql.DataSet.
>
> What do I have to do to make to compile the following code?
>
> Better yet, does someone have some sample JDBC4 code that demonstrates using
> annotations for SQL queries (insert, delete, select, update etc...) that
> compiles and runs?
>
> I did some google searching and found some JDBC4 tutorials, but the code
> fragments did not include the import statements!
>
> I was told that the only low cost database that includes a JDBC4 driver is
> IBM's Apache Derby. Is this true?
>
> Thanks,
> Siegfried
>
> Here is some sample code from the above zip file (I added the bash procedure
> in the comments).
>
>
>
>
> package query;
> import java.sql.Connection;
> import java.sql.DataSet;
> import java.sql.DriverManager;
> import java.sql.SQLException;
> /*
>  * Begin commands to execute this file using Java with bash
>  * export DERBY_HOME=/cygdrive/c/dev/derby/10.2.2.0
>  * /cygdrive/c/dev/derby/10.2.2.0/bin/ij <<EOF
>  * CONNECT 'jdbc:derby:test;create=true';
>  * DROP TABLE FIRSTTABLE;
>  * CREATE TABLE FIRSTTABLE
>  *   (ID INT PRIMARY KEY,
>  *   NAME VARCHAR(12));
>  * INSERT INTO FIRSTTABLE VALUES (10,'TEN'),(20,'TWENTY'),(30,'THIRTY');
>  * SELECT * FROM FIRSTTABLE;
>  * EOF
>  * javac -g QueryDemo.java
>  * cd ../
>  * javaw query.QueryDemo
>  * cd query/
>  * End commands to execute this file using Java with bash
>  *
>  */
> public class QueryDemo {
>
>     public static void main(String[] args) {
>
>         Connection connection = null;
>         try {
>             String url = "jdbc:inetdae7:localhost:1433?"
>                     + "database=Adaptor";
>             String login = "sa";
>             String password = "admin";
>             connection = DriverManager.getConnection(url, login,
>                     password);
>
>             // Create Query object
>             RoleQueries qo = connection
>                     .createQueryObject(RoleQueries.class);
>
>             // Get all roles
>             DataSet<Role> rows = qo.getAllRoles();
>             for (Role role : rows) {
>                 System.out.println(role);
>             }
>
>             // Create new Role object
>             if (! rows.isReadOnly()) {
>                 System.out.println("\nCreate new role");
>                 Role r = new Role();
>                 r.role_id = 12345;
>                 r.name = "Supervisor";
>                 r.description = "Do monitoring job";
>     
>                 boolean insertResult = rows.insert(r);
>                 rows.sync(connection);
>                 System.out.println("\tInserted: " + insertResult);
>             }
>
>             // Retrieve Role by name
>             System.out.println("\nGet role by name:");
>             DataSet<Role> rows2 = qo.getRoleByName("Supervisor");
>             Role role = rows2.get(0);
>             System.out.println(role);
>
>             if (role != null) {
>                 // Modify Role
>                 System.out.println("\nModify current role:");
>                 role.description = "Do supervising job";
>                 boolean modifyResult = rows2.modify(role);
>                 rows2.sync(connection);
>                 System.out.println("\tModified: " + modifyResult);
>             }
>         } catch (SQLException e) {
>
>             for (Throwable t : e) {
>                 t.printStackTrace();
>             }
>         } finally {
>
>             // Close connection
>             try {
>                 connection.close();
>             } catch (SQLException e) {
>                 e.printStackTrace();
>             }
>         }
>     }
> }
>
>
>
>
>
>
> import java.sql.BaseQuery;
> import java.sql.DataSet;
> import java.sql.Select;
> public interface UserQueries extends BaseQuery {
>
>     // Select all users
>     @Select(sql = "SELECT userId, firstName, lastName FROM Users", 
>             readOnly=false, connected=false, tableName="Users")
>     DataSet<User> getAllUsers();
>
>     // Select user by name */
>     @Select(sql = "SELECT userId, firstName, lastName FROM Users "
>             + "WHERE userName=?", readOnly=false, connected=false,
>             tableName = "Users")
>     DataSet<User> getUserByName(String userName);
>
>     // Delete user
>     @Update("DELETE Users WHERE firstName={firstName} " +
>             "AND lastName={lastName}")
>     int deleteUser(String firstName, String lastName);
> }
>
> class User {
>     public String userId;
>     public String firstName;
>     public String lastName;
>     
> }
>