I am getting the above mentioned exception while executing the SQL query in Java.
statement2.executeUpdate("INSERT INTO visit_header"
+ "VALUES('"+visitnumber+"','"+date+"','"+cookie+"','"+ip+"')");
I want to know where it is going wrong.
As per initial look, you have a problem in your sql query:
statement2.executeUpdate("INSERT INTO visit_header" + "VALUES
Should be
statement2.executeUpdate("INSERT INTO visit_header " + "VALUES //Note space after header
There was no space between visit_header and VALUES, so your query was like this:
INSERT INTO visit_headerVALUES
Which is wrong.
You forgot to put space between visit_header and values:
statement2.executeUpdate("INSERT INTO visit_header" + " VALUES ('"+visitnumber+"','"+date+"','"+cookie+"','"+ip+"')");
Related
Im getting this :
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause.
For my code:
ResultSet results = state.executeQuery("SELECT * FROM User" + "WHERE user_name = '" +
txtUser.getText() + "'AND password = '" + txtPass.getText() + "'" );
I have already declared all of the variables, and i suspect the error has something to do with the structure however I am unsure. Thanks
If you pay close attention to your code on this line:
"SELECT * FROM User" + "WHERE user_name
You can see that this results in
SELECT * FROM UserWHERE user_name
You need to add in a space.
In addition you're opening yourself to SQL injection attacks by building your statement like this. You should really use a PreparedStatement and use placeholder. You also won't need to build the statement by hand which would help you avoid this very issue.
StringBuilder sql = new StringBuilder();
sql.append("SELECT C.ID,\n" +
" CODE\n" +
" FROM DB.TEATABLE C\n" +
" LEFT JOIN\n" +
" (SELECT IT.TEA,\n" +
" IT.COFFEETYPE,\n" +
" XREF.AUSTIN_COFFEE_LEAF_CODE AS LEAFCODE\n" +
" FROM DB.COFFEE_AUSTIN_XREF XREF,\n" +
" DB.INDIAPLAN TP,\n" +
" DB.COFFEE IT,\n" +
" DB.TEATABLE C\n" +
" WHERE C.ID = IT.TEA\n" +
" AND IT.COFFEETYPE = 'RIL'\n" +
" AND IT.INDIAPLANID =TP.ID");
con = getConnection();
pstmt = con.prepareStatement(sql.toString());
rs = pstmt.executeQuery();
We used to add new line charactor as above. Is it safe to add \n character in the query (in Oracle)?
Yes! But make sure your newlines are actually newlines.
I was trouble shooting for 3 hours thinking it was my sql syntax
because I used:
String N = System.getProperty("line.seperator");
I thought I had a newline character,
but I was actually inserting nulls into my statement.
Error Messages Included:
1:
org.postgresql.util.PSQLException:
ERROR: syntax error at or near "NULLnull"
2:
org.postgresql.util.PSQLException:
ERROR: conflicting NULL/NOT NULL declarations
for column "nullid" of table "t_1"
3:
org.postgresql.util.PSQLException:
ERROR: syntax error at or near "KEYnull"
4:
org.postgresql.util.PSQLException:
ERROR: syntax error at or near "null"
I thought I was my primary key statement:
id SERIAL PRIMARY KEY NOT NULL
For longer than I would like to admit.
Yes, it is perfectly fine to add newlines (\n or \r\n) to your query. Most parsers consider newlines as whitespace (just like a space or tab), and so does the one used by Oracle (and all other database I have used).
You have already proven this, because based on your comments your company already does this (and probably has so for years). I'm curious as to why you think it could be unsafe. Because obviously it works, and if it would be unsafe, Oracle would disallow it with a clear error.
Although very subjective, it could be better than not doing it, as it allows for a easier visual inspection if all lines end in whitespace. Consider the difference between:
Oops, no space
"select *" +
"from table"
Space
"select * " +
"from table"
Linebreak
"select *\n" +
"from table"
The difference between 1 and 2 is smaller than between 1 and 3. Especially in long queries this might matter to see if you forgot whitespace which might either lead to a syntax error, or worse to incorrect query behavior.
On the other hand, the newlines are extra visual clutter that might distract from reading the query. When in doubt, go with what is the norm or codestyle in your company.
I am executing an update and I want to insert the value that is returned from my getter into the my table.
statement.executeUpdate("INSERT INTO my_table " +
"VALUES(myClass.getValue(), 'abcd',now())");
I have tried debugging through and I found that the String value and datetime executes correctly. However it gives me an exception when I am calling my getter. The detail message that it shows is FUNCTION myClass.getValue does not exist.
My imports are in order. Any ideas?
statement.executeUpdate("INSERT INTO my_table " + "VALUES("+myClass.getValue() + ", 'abcd',now())");
Your get-call was interpreted as a String because of the missing ' " '.
Take a look at prepared statements, they are easy to read and use and you don't have to struggle with these problems.
Prepared Statement Version (also a lot more secure because they are preventing SQL Injection):
PreparedStatement pst = yourconnection.prepareStatement("INSERT INTO my_table VALUES(?,?,now())";
pst.setString(1,myClass.getValue());
pst.setString(2,"abcd");
pst.executeUpdate();
This is the SQL that you're trying to execute.
INSERT INTO my_table VALUES(myClass.getValue(), 'abcd',now())
You need to pass valid SQL to the executeUpdate method in order for it to run. Java won't interpolate variables and method calls inside strings for you. You have to either concatenate their values into the SQL string that you pass to executeUpdate, or use Prepared Statements instead.
You need to make a method call to your myClass object, not a string. The string will not be executed, its not code, just words.
statement.executeUpdate("INSERT INTO my_table VALUES(" + myClass.getValue() + ", 'abcd',now())");
I'm going to show you how to do it with prepared statements since the other answers did not show you:
PreparedStatement prepStmt = con.prepareStatement("INSERT INTO my_table VALUES( ? , 'abcd',now())"));
prepStmt.setString(1, myClass.getValue());
prepStmt.executeUpdate();
Notice the ?. It will get replaced by your Java call to myClass.getValue().
Please do not concatenate SQL strings.
I've read many threads regarding this topic, but everybody point to the character set in the database.
I'm using strings with special characters like 'ñ' and I'm not able to set them right in the database. As you may guess, the characters are changed to '?'.
The thing is that using this statement, I get it RIGHT:
stmt = con.prepareStatement("INSERT INTO LONG_CODES_TABLE (TIMESTAMP, TABLE_NAME, CODE__C, CODE_DESC)
VALUES (GET_TIMESTAMP, 'MUNICIPIOS', '" + municipio + "', '" + municipio + "') ");
And just in the same database and table, without changing anything, if I use the following I get the '?' character in the DB:
stmt = con.prepareStatement("INSERT INTO LONG_CODES_TABLE (TIMESTAMP, TABLE_NAME, CODE__C, CODE_DESC)
VALUES (GET_TIMESTAMP, 'MUNICIPIOS', ?, ?) ");
stmt.setString(1, municipio);
stmt.setString(2, municipio);
So, the character problem is happening ONLY if I use setString.
Any ideas?
EDIT: The value of the 'municipio' variable is, for example: 'ABADIÑO'.
Later, I can check the differences between doing it on way or the other by asking for that value with an sql statement, for example:
select * from long_codes_table
where table_name = 'MUNICIPIOS' and code__c = 'ABADIÑO'
One way I get the result row. The other way, I don't.
Thank you.
I had that behaviour, too. On top of that I observed that this error did not occur when the application was started from the IDE. That's how I realized that in the JVM - attributes, the one for the encoding was missing.
java %vm-opts% %clspth% -Dfile.encoding=UTF-8 ...
i written this query (in java class) to select some information from the MySQL database
and view it on jsp page...
SELECT instructor.name FROM
instructor,section,teach WHERE
teach.student_id='3'AND teach.section
= section.number AND section.instructor_id= instructor.ID
but there is exception was occur!
javax.servlet.ServletException:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near
'.student_id='3'AND teach.section =
section.number AND
section.instructor_id= ins' at line 1
,,
by the way, i was write it in PhpMyAdmin, and it's work..
App server: Sun GlassFish Enterprise Server v2.1
please help me...
Regards
You need a space after the '3'.
It seems that there is a space missing. '.student_id='3'AND -> '.student_id='3' AND
If your query is exactly as you show it:
SELECT instructor.name FROM instructor,section,teach WHERE teach.student_id='3'AND
teach.section = section.number AND section.instructor_id= instructor.ID
then you need a space after the '3' and before AND on the first line shown above.
What is the datatype of "teach.student_id"? Is it numeric or varchar. If it is numeric, no need to put the '3' inside single quotes.
e.g.
teach.student_id = 3
Thanks for all
i was write query in java class like this
ResultSet rs
=stmt.executeQuery("SELECT instructor.name " +
"FROM instructor,section,teach" +
"WHERE teach.student_id=" + "'" + idd + "'" +
" AND teach.section = section.number
AND section.instructor_id=
instructor.ID");
then i eliminate all lines, and put query in one line like this, then it's solved...
Regards