how to verify in mysql if the number of rented books is lass 1

Hi to everyone!i have a book database,client db and rent db.In thje book db i have the number of each book! Clients came and rent books. I have to verify that the number of books in the table is >=1!How can I do this?Can you please help me!Thanks!
[278 byte] By [Killer86a] at [2007-11-27 2:03:53]
# 1

Completely untested, but should point you in the right general direction...

// Connect to database.

Connection C = DriverManager.getConnection("jdbc:mysql://host/database_name","username","password");

// Prepare SQL statement to select details of requested book (by book_id?).

PreparedStatement PStmt = C.prepareStatement("SELECT book_id, book_name, book_count FROM books where book_id=?");

// Requested book id...:)

int requestedBookId = 20;

// Set placeholder value for book id.

PStmt.setInt(1, requestedBookId);

// Execute...

ResultSet RS = PStmt.executeQuery();

// Get record...

RS.first();

// Is the current book count greater than 0...

if ( RS.getInt("book_count") > 0 )

{

// Do whatever with the "client" and "rent" databases

// (did you mean "client" and "rent" tables?)

}

// Close all

PStmt.close();

RS.close();

C.close();

beetle_buma at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Please don't capitalize variablenames.Back to the question: use SELECT COUNT(*) in your SQL query.
BalusCa at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Thanks for the ideea!

And one question: you know i have the 3 tables : books,clients,and rent!how can i do that if somebody rents a book than automaticaly the number of the rented book in the book table substracts with one, i mean nr_book = nr_book -1.

Can somebody help me!

thanks to all

Killer86a at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
nr_book = nr_book -1You can just do that this way in SQL.
BalusCa at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
ok but how can i write it in java,because i am working with java connected with mysql!i have to make a method or something,am i right? any help pls!
Killer86a at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Creating a simple Statement or PreparedStatement is sufficient. Just write SQL in a String and let it execute by Statement or PreparedStatement.
BalusCa at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
"...Please don't capitalize variablenames...."Oops! Sorry, I know - bad practise!!! I'm new to Java:)Message was edited by: beetle_bum
beetle_buma at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

Sorry but i couldn't resolv the probleme!

I have to modify only those values of nrBooks which are rented!

I forgott to say that I have a join table,the idbook,and idclient from book and client table are Fk keys int the rent table.

String modif = "UPDATE book,client,rent set nrbook =nrebook-1 where

book.idbook=rent.idbooki and client.idclient=rent.idclient";

but this sintaz changes all the nrbook to nrbook-1!how shall i do this only for the book wich is rented

Thanks again for the help!!!

Killer86a at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

You haven't supplied the id of the specific book you're trying to update...

UPDATE book,client,rent SET

nrbook = nrebook-1

WHERE book.idbook=rent.idbooki AND

client.idclient=rent.idclient AND

book.idbook=?

... see use of placeholder "?" as posted earlier.

Gotta say I'm not entirely clear on why you need to join to the "client" and "rent" tables to reduce an available count (ie nrebook) in the "book" table which you already know the id of.

beetle_buma at 2007-7-12 1:46:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...