?
Solved

combine insert and update sql statement in JSP

Posted on 2011-05-01
3
Medium Priority
?
865 Views
Last Modified: 2012-05-11
I would like to insert data in a table a table called Reservations and Update the Books table to change the reserved row from "N" to "Y". It is a website that let the user reserve a book...if the book is reserved, the username is inserted in the reservations table and the status of the books will change to (REserved=='Y')...

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:dBase","","");
stmt=con.createStatement( );

stmt.executeUpdate("INSERT INTO Reservations(ISBN,ReservedDate)"
+ "VALUES('"+isbn_n+"','"+date_d+"')" AND "UPDATE Books SET Reserved='Y' WHERE BookTitle='"+Book_Title+"'");


out.println("You have reserved the book successfully");


}

Open in new window


it is seems like the way i combine the two is wrong...could you pls help with to discover the mistake...

Thanks folks
0
Comment
Question by:ozzyfanta
3 Comments
 
LVL 23

Accepted Solution

by:
cmalakar earned 800 total points
ID: 35500570
You must execute the statements separately and use transactions.

Look into this tutorial.

http://download.oracle.com/javase/tutorial/jdbc/basics/transactions.html
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 800 total points
ID: 35500942
You cannot do an update and an insert in the same SQL statement.

My recommendation is to visualize "Reserve a Book" as a complete business process.  Encapsulate the entire process in a stored procedure.  And yes, use transactions.

One more thought.  From database purist standpoint, you've got some redundancy in your design. In other words, a single fact (book is reserved) represented two different ways (a row in reservation table, a Y/N flag in book table).   Best:  eliminate the Y/N flag; if needed as a column, derive it in a view. Better: maintain the Y/N flag using triggers rather than in application code to drastically lesson the probability of getting out-of-sync.  Good:  maintain both the reservation table, flag and transaction in a backend procedure; i.e. a business process layer (as described in paragraph 1)

Not so good:  trying to stay in sync from JSP code.


0
 
LVL 6

Assisted Solution

by:AkAlan
AkAlan earned 400 total points
ID: 35502108
You can do an Update/Insert using the same Stored Procedure in SQL. You just pass in the PK value for an Update or not for an Insert using the following example.

USE [YourDatabase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[Update_InsertToYourTable]

	@TrainingClassID int,
	@ClassDate datetime,
	@Instructor varchar(50),
	@ClassLocation varchar(50),
	@ClassDescription varchar(50),
	@ClassDuration varchar(50),
	@DatabaseUpdatedBy varchar(50),
	@DatabaseUpdatedOn datetime
AS
SET NOCOUNT ON
IF @TrainingClassID = 0 BEGIN
	INSERT INTO HR_TrainingClasses (
		[ClassDate],
		[Instructor],
		[ClassLocation],
		[ClassDescription],
		[ClassDuration],
		[DatabaseUpdatedBy],
		[DatabaseUpdatedOn]
	)
	VALUES (
		@ClassDate,
		@Instructor,
		@ClassLocation,
		@ClassDescription,
		@ClassDuration,
		@DatabaseUpdatedBy,
		@DatabaseUpdatedOn
	)
	SELECT SCOPE_IDENTITY() As InsertedID
END
ELSE BEGIN
	UPDATE HR_TrainingClasses SET 
		[ClassDate] = @ClassDate,
		[Instructor] = @Instructor,
		[ClassLocation] = @ClassLocation,
		[ClassDescription] = @ClassDescription,
		[ClassDuration] = @ClassDuration,
		[DatabaseUpdatedBy] = @DatabaseUpdatedBy,
		[DatabaseUpdatedOn] = @DatabaseUpdatedOn
	WHERE [TrainingClassID] = @TrainingClassID

END

SET NOCOUNT OFF

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question