I am taking a web design and development class and we have been thrown into MySQL along with java without much knowledge beforehand. Was wondering if anyone could help me with understanding how to query the database and update entries, as I am confused on how to accomplish this.
I am able to insert new entries and display table info, but when it comes to adding to existing info nothing seems to work. I can get the statement to work if I supply the data in the SQL statement i.e UPDATE tableName SET columnName1 = value1, WHERE criteria. But when I attempt to use user input and send variables along nothing seems to work.

Any help would be appreciated.


9 Years
Discussion Span
Last Post by jsully1

OK, I am still having trouble with this. All I want right now is to get something to work so I am going small. I am accessing a books database that consists of three tables, and all I want is to extract the authorID(primary key) by having the user enter the authors lastName. Currently this is what I have:

public void AddTitle() throws SQLException {
		//PreparedStatement addTitle = (PreparedStatement) connection.prepareStatement
		//("INSERT INTO titles (title) VALUES (?) WHERE authorId FROM authorISBN = (?)");
		PreparedStatement p = connection.prepareStatement("SELECT authorID FROM authors WHERE lastName = ?") ;
		String authorsFirstName, authorsLastName, newTitle, id;
		//authorsFirstName = GetAuthorFirstName();
		authorsLastName = GetAuthorLastName();
		//newTitle = GetNewTitle();
		p.setString(1, authorsLastName);
		ResultSet rs = p.executeQuery();
		Integer key = rs.getInt("authorID");

			//Object key = rs.();
			System.out.printf("Key for author %s is %d \n", authorsLastName, key);
		}//end try
		catch(Exception exception)

	}//end AddTitle method

And I receive a java.sql.SQLException: Before start of result set when I try and use the method. Any suggestions would be helpful. Thanks


authorsLastName, I do not see that variable passed down to AddTitle() method. If you not able to solved it, provide the whole code for this servlet


Uhm, Peter, what's this

authorsLastName = GetAuthorLastName();


However, with the way it's written (if it compiles at all) authorsLastName is an instance variable anyway (as it is not declared in the method simply defined), and that is probably not a good idea.

Otherwise, knowing exactly what SQLException you get would help. Post the rest of the message. I can almost guarantee, however, that it has to do with what GetAuthorLastName() is returning (or not).

Votes + Comments
Yes, you right, I did not spot that one
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.