Code:
public BookPrx createBook(String isbn, String title, java.util.List<String> authors, Ice.Current current)
throws BookExistsException
{
SQLRequestContext context = SQLRequestContext.getCurrentContext();
assert context != null;
/*st = new StringTokenizer(title);
while (st.hasMoreTokens()) {
// stmt.setString(i2, st.nextToken().t);
}*/
// s.execute("Insert into MOBTEST.BOOKS (ID,ISBN,TITLE,RENTER_ID) values (1,null,'marius',null)");
// s.close();
// connection.close();
try
{
connection = getConnection();
stmt = connection.prepareStatement("SELECT * FROM locations WHERE time = ?");
stmt.setString(1, isbn);
java.sql.ResultSet rs = stmt.executeQuery();
if(rs.next())
{
throw new BookExistsException();
}
connection.close();
//
// First convert the authors string to an id set.
//
connection = getConnection();
java.util.List<Integer> authIds = new java.util.LinkedList<Integer>();
for(String author : authors)
{
Integer id;
stmt = connection.prepareStatement("SELECT * FROM headings WHERE time = ?");
stmt.setString(1, author);
rs = stmt.executeQuery();
if(rs.next())
{
// If there is a result, then the database
// already contains this author.
// id = rs.getInt(1);
assert !rs.next();
}
else
{
// Otherwise, create a new author record.
stmt = connection.prepareStatement("INSERT INTO headings (time,geo,mag,x,y,z,originaltrackid,accuracy) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, author);
stmt.setString(2, author);
stmt.setString(3, author);
stmt.setString(4, author);
stmt.setString(5, author);
stmt.setString(6, author);
stmt.setString(7, author);
stmt.setString(8, author);
int count = stmt.executeUpdate();
assert count == 1;
rs = stmt.getGeneratedKeys();
boolean next = rs.next();
assert next;
// id = rs.getInt(1);
}
// Add the new id to the list of ids.
authIds.add(1);
}
connection.close();
//write into headings
connection = getConnection();
// Otherwise, create a new author record.
stmt = connection.prepareStatement("INSERT INTO headings (time,geo,mag,x,y,z,originaltrackid,accuracy) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, title);
stmt.setString(2, title);
stmt.setString(3, title);
stmt.setString(4, title);
stmt.setString(5, title);
stmt.setString(6, title);
stmt.setString(7, title);
stmt.setString(8, title);
int count = stmt.executeUpdate();
assert count == 1;
rs = stmt.getGeneratedKeys();
boolean next = rs.next();
assert next;
// Integer bookId = rs.getInt(1);
connection.close();
// Create the new book.
connection = getConnection();
stmt = connection.prepareStatement("INSERT INTO locations (time, latitude,longitude,altitude,hdop,vdop,speed,originaltrackid,course) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)",
java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, isbn);
stmt.setString(2, title.subSequence(0, 4).toString());
stmt.setString(3, (String) title.subSequence(5, 10));
stmt.setString(4, title);
stmt.setString(5, title);
stmt.setString(6, title);
stmt.setString(7, title);
stmt.setString(8, title);
stmt.setString(9, title);
//alternative
int count2 = stmt.executeUpdate();
assert count2 == 1;
rs = stmt.getGeneratedKeys();
boolean next2 = rs.next();
assert next2;
// Integer bookId = rs.getInt(1);
connection.close();
connection = getConnection();
// Create new authors_books records.
stmt = connection.prepareStatement("INSERT INTO accelerations (devicetype, trackid, time, x, y, z, rotx, roty, rotz, tmp, originaltrackid) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",java.sql.Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, isbn);
stmt.setString(2, isbn);
stmt.setString(3, isbn);
stmt.setString(4, isbn);
stmt.setString(5, isbn);
stmt.setString(6, isbn);
stmt.setString(7, isbn);
stmt.setString(8, isbn);
stmt.setString(9, isbn);
stmt.setString(10, isbn);
stmt.setString(11, isbn);
count = stmt.executeUpdate();
assert count == 1;
rs = stmt.getGeneratedKeys();
boolean next3 = rs.next();
assert next3;
connection.close();
return BookPrxHelper.uncheckedCast(current.adapter.createProxy(BookI.createIdentity(1)));
}
catch(java.sql.SQLException e)
{
JDBCException ex = new JDBCException();
ex.initCause(e);
throw ex;
}
}
I think i should not open an connection everytime I am write something in a table.