Jump to content

SQL Statement

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
thieflock

thieflock

    Newbie

  • Members
  • PipPip
  • 29 posts
This is the first time I have used SQL statements. Line 44 is giving me a compile error, not sure why.

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;

			}

		}

	}

}



#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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
thieflock

thieflock

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks so much mate.

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I think "back quotes" (aka grave accent?)can also be used to seperate values from fields.