(original) (raw)

/* * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * Portions Copyright (c) 2012 IBM Corporation */ import com.sun.rowset.CachedRowSetImpl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import javax.sql.rowset.CachedRowSet; public class DerbyTest { private static String DERBY_DB_FILE = "derbydb"; private static Connection getConnection() throws Exception { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String url = "jdbc:derby:" + DERBY_DB_FILE + ";create=true"; Connection conn; try { Class.forName(driver); conn = DriverManager.getConnection(url); } catch (Exception e) { throw e; } return conn; } private static void createTable(Connection conn) throws SQLException { Statement statement = conn.createStatement(); try { statement.execute("DROP TABLE mytable"); } catch (SQLException ex) { // ignore exception when mytable doesn't exist } String createTableSQL = "CREATE TABLE mytable" + "(id INT NOT NULL PRIMARY KEY," + "description VARCHAR(30) NOT NULL)"; statement.executeUpdate(createTableSQL); statement.close(); } private static void prepareData(Connection conn) throws SQLException { Statement statement = conn.createStatement(); String insertTableData = "INSERT INTO mytable(id,description)" + "VALUES(1, 'r1')"; statement.executeUpdate(insertTableData); insertTableData = "INSERT INTO mytable(id,description)" + "VALUES(2, 'r2')"; statement.executeUpdate(insertTableData); statement.close(); } private static void createConflict(Connection conn) throws SQLException { CachedRowSet rs = new CachedRowSetImpl(); rs.setReadOnly(false); String querySql = "SELECT id, description FROM mytable"; rs.setCommand(querySql); rs.execute(conn); rs.moveToInsertRow(); rs.updateObject(1, 2); rs.updateObject(2, "new str"); rs.insertRow(); rs.moveToCurrentRow(); rs.acceptChanges(conn); } public static void main(String[] args) throws Exception { Connection conn = getConnection(); createTable(conn); prepareData(conn); try { createConflict(conn); } catch (SQLException ex) { String errMsg = ex.getMessage(); if (!errMsg.startsWith("1")) { System.out.println("The conflict number should be 1 but now " + "it is " + errMsg); throw ex; } } conn.close(); } }