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.

Thanks

Recommended Answers

All 6 Replies

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 = (?)");
		try
		{
		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);
		//p.execute();
		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)
		{

			exception.printStackTrace();
		}
		
		
	}//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).

commented: Yes, you right, I did not spot that one +12

Figured it out. Thanks to all who responded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.