package myBeans;
import java.sql.*;
public class AddBookBean {
private String ISBN, bookTitle, authorLName, authorFName, bookPublisher, bookSynopsis;
private int yearOfPub, bookQuantity;
Connection conn;
Statement stm;
ResultSet rst;
ResultSetMetaData rsmd;
public void setISBN(String variable) {
ISBN = variable;
}
public void setBookTitle(String variable) {
bookTitle = variable;
}
public void setAuthorLName(String variable) {
authorLName = variable;
}
public void setAuthorFName(String variable) {
authorFname = variable;
}
public void setBookPublisher(String variable) {
bookPublisher = variable;
}
public void setBookSynopsis(String variable) {
bookSynopsis = variable;
}
public void setYearOfPub(int variable) {
yearOfPub = variable;
}
public void setBookQuantity(int variable) {
bookQuantity = variable;
}
public String getUpdate() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
stm = conn.createStatement();
stm.executeUpdate(INSERT INTO Book (ISBN, Title, Year, Quantity, Description) VALUES ("ISBN", "bookTitle", yearOfPub, bookQuantity, "bookSynopsis"));
stm.close();
conn.close();
return "<h1> Success </h1>";
}
catch(Exception e) {
String errorMessage = "";
StackTraceElement[] error = e.getStackTrace();
for(int i=0; i<error.length; i++) {
errorMessage += error[i] + "\n";
return "<h1> Fail: </h1>" + errorMessage;
}
}
}
}
SQL Statement
Started by thieflock, May 04 2009 12:04 PM
3 replies to this topic
#1
Posted 04 May 2009 - 12:04 PM
This is the first time I have used SQL statements. Line 44 is giving me a compile error, not sure why.
|
|
|
#2
Posted 04 May 2009 - 12:49 PM
stm.executeUpdate(INSERT INTO Book (ISBN, Title, Year, Quantity, Description) VALUES ("ISBN", "bookTitle", yearOfPub, bookQuantity, "bookSynopsis"));
Your entire SQL query should be in quotes.
stm.executeUpdate("INSERT INTO Book (ISBN, Title, Year, Quantity, Description) VALUES ('ISBN', 'bookTitle', 'yearOfPub', 'bookQuantity', 'bookSynopsis');");
Also, I like to use single quotes for anything in the query that should have quotes. This saves me escaping quotes a lot. The executeUpdate method is passed a String reference.
:)
#3
Posted 04 May 2009 - 01:05 PM
Thanks so much mate.
#4
Posted 04 May 2009 - 08:15 PM
I think "back quotes" (aka grave accent?)can also be used to seperate values from fields.


Sign In
Create Account


Back to top










