db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sieg <siegfr...@heintze.com>
Subject Wanted: Working example of JDBC4 Annotations.
Date Tue, 17 Jul 2007 14:56:05 GMT

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.


Mime
View raw message