27. oktober 2003 - 15:58
Der er
10 kommentarer og 1 løsning
sql WHERE error
I've done an application where I run queries to a database. The first question I made worked very well... but then when I did another query in an other method I got: java.SQLException: syntax-error in WHERE. When I remove WHERE the query works.. what could be wrong? I have checked all the variables that I use in the question and I connected to the database in that method aswell, the same way I did in the first method that worked..
Annonceindlæg fra DE-CIX
27. oktober 2003 - 16:03
#1
can you post your SQL statement here?!?
27. oktober 2003 - 16:04
#2
We need to see the querys !!
27. oktober 2003 - 16:07
#3
Is java.SQLException: syntax-error in WHERE the hole error message ??? Post the code that don't work - please..
27. oktober 2003 - 16:18
#4
OK... here's the connection: public static void connect (){ try{ // DatabasDriver Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); // DatabasKoppling connection = DriverManager.getConnection("jdbc:odbc:speak", "jacob", "nowijustwrotesomthinghereforekspertview"); }catch(Exception e){} } Here's the method that works: public static void speak (String x){ try { String say = "<html><u>You</u>: "+x+"</html>"; write(say); databaseid = "analyze"; runqueries = true; connect(); StringTokenizer sentence = new StringTokenizer (x); // Loopen som ställer frågor while (sentence.hasMoreTokens() & runqueries) { // Fråga PreparedStatement preparedStatement = connection.prepareStatement("SELECT databaseid, group FROM "+ databaseid +" WHERE inword ='"+ sentence.nextToken() +"';"); // Ställ frågan ResultSet databaseids = preparedStatement.executeQuery(); // Spara databaseid i String while(databaseids.next()) { databaseid = databaseids.getString("databaseid"); group = databaseids.getString("group"); } System.out.println("Group is:"+group);//TA BORT if (databaseid.equals ("answer")) {runqueries = false; getanswers(); String y = random(answersarray); System.out.println("ggooogel"); answer = "<html><u>Waldo</u>: "+ y +"</html>"; write(answer);} }//slut-på-loop if (runqueries) {answer = "<html><u>Waldo</u>: Waldo don't understand..</html>"; write(answer);} } catch (Exception e) {e.printStackTrace();} } And here is the one where the problem is: // Hämtar svaren och sätter dem i en array public static void getanswers() { try{ // DatabasDriver //Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); connect(); // DatabasKoppling //Connection connection = DriverManager.getConnection("jdbc:odbc:speak", "jacob", "maxell"); System.out.println(databaseid);//FELSÖK PreparedStatement getanswers = connection.prepareStatement("SELECT outword FROM "+ databaseid +" WHERE group ='"+group+"';"); //PreparedStatement getanswers = connection.prepareStatement("SELECT outword FROM " + databaseid+ " WHERE group like '1%';"); //PreparedStatement getanswers = connection.prepareStatement("SELECT outword FROM answer"); System.out.println("förbi prepare");//FELSÖK ResultSet answers = getanswers.executeQuery(); System.out.println("förbi execute");//FELSÖK int x = 0; while(answers.next()) { answersarray [x] = answers.getString("outword"); System.out.println(answersarray [x]); x++; } } catch (SQLException e){e.printStackTrace();} } All methods are in the same class, don't think about variables, some are declared globally, but they do what I want them to do, I've checked that.
27. oktober 2003 - 16:29
#5
GROUP er et reserveret ord.
27. oktober 2003 - 16:29
#6
GROUP is an reserved word.
27. oktober 2003 - 16:29
#7
Try call your field something else.
27. oktober 2003 - 16:30
#8
so I can't use it in my database-column?
27. oktober 2003 - 16:32
#9
No I think that is the problem. try rename it io questiongroup, answergroup, usregroup or somethinggroup.
27. oktober 2003 - 16:41
#10
Yeeey! It worked.. Should have known that myself.. but It's stupid that access didn't warn me about the name :s Thank u! PS. make "Svar" maybe?
27. oktober 2003 - 16:46
#11
answer
Kurser inden for grundlæggende programmering