Using data in the database

Asked

Viewed 271 times

-1

I am creating a quiz. I managed to create the screen to create the questions, this recording in the database everything right. Only now I will do the "game" itself, so I would have to take a question that is stored in the bank and return to the screen, as I do this query in the bank? ResultSet is giving error the code below.

 String SQL = "Aqui montaria o SQL de Busca de Dificuldades no jogo
 WHERE" ;

 String Nivel=" ";

 if(JrbFácil.isSelected()){ 

 SQL += "DICICULDADE = FACIL"

 }

 if(JrbMédio.isSelected()){

 SQL += "DICICULDADE = MEDIO"

 }

 if(JrbDificil.isSelected()){

 SQL += "DICICULDADE = DIFICIL"

 }

 PreparedStatement stmt = this.connection.prepareStatement("select *from quiz where id = ?");

 stmt.setInt(1, 1); 

 ResultSet rs = stmt.executeQuery();
  • What a mistake you’re making young?

  • is not compiling

  • Beauty, post the output with the build error...

  • See if this is of any help: Search method involving Java and SQL Server

  • What is the use of the string SQL?

  • "Not compiling" is something very generic. Are you displaying an error? An Exception? Just looking at your code I noticed scores (á, í, ...), it is worth remembering that java is case sensitive. I recommend that you rephrase your question by posting the full code you are using and if possible inform the fields of the table that is searching for the information in the database. Help to be helped.

  • You imported the java.sql.ResultSet?

Show 2 more comments

2 answers

1

1-) Test the query in the database to see if it is returning data.
2-) Check that both SELECTS are correct, using the above step.
3-) Check that the application is running SELECT.
4-) Do not forget to put a space after the WHERE when mounting the query in this way.
5-) Check if the connection is ok.

Using the result set:

PreparedStatement stmt = this.connection.prepareStatement("select * from quiz where id = ?");

stmt.setInt(1, 1); 

ResultSet rs = stmt.executeQuery();
String campo1;
String campo2;
while(rs.next()){
 campo1 = rs.getString("NOME DO CAMPO 1");
 campo2 = rs.getString("NOME DO CAMPO 2");
}

Remembering, that it is considered good practices to use the name of the field instead of the Dice.

Were just assumptions, post the stacktrace or build error to help in solving the problem

0

Although the question is not clear, already tried to check your SQL is correct?

select *from quiz where id = ?

I think the right thing would be:

select * from quiz where id = ?
  • What is the difference between the first syntax and the second?

  • He forgot the * pasted in the from, possibly this may give some kind of error.

  • It doesn’t change anything Renato, I just tested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.