mySQL syntax not working - mysql

Using a try-catch phrase I catch an error in the following code:
ResultSet rs = stmt.executeQuery("select * from 'user_tbl' where user_id = '"+user+"' ");
The error details are as follows:
com.mysql.jdbc.exceptions.jdbc4.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 ''user_tbl' where user_id = '12345678'' at line 1
Is there anything wrong with my syntax here? I'm using Netbeans as my IDE.

Single quotes are not required, try this:
ResultSet rs = stmt.executeQuery("select * from user_tbl where user_id = " + user);
If user_id is non-integer then enclose user variable in single quotes

You shouldn't have user_tbl inside ' marks, try removing them so it reads:
ResultSet rs = stmt.executeQuery(
"select * from user_tbl where user_id = '"+user+"' ");

Related

How do you pass and recive an output parameter from vb.net to a mysql

Dim Def_Command_MySQL1 As MySql.Data.MySqlClient.MySqlCommand
Def_Command_MySQL1.Parameters.Clear()
Def_Command_MySQL1.CommandText = "SET #OutID = 10;"
or
Def_Command_MySQL1.CommandText = "UPDATE ID_Max1 SET IdMax = IdMax + 1 , #OutID = IdMax + 1 where TypeID>1"
Def_Command_MySQL1.Parameters.Add(New SqlParameter("#OutID", SqlDbType.Int)).Value = 41
Def_Command_MySQL1.Parameters("#OutID").Direction = ParameterDirection.Output
Def_Command_MySQL1.ExecuteNonQuery()
IDMax_Update = Def_Command_MySQL1.Parameters("#OutID").Value
Err.Description "You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near '41 = 10' at line 1" String
or
Error = You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '1=IdMax + 1 where TypeID>1' at line 1
First you set AllowUserVariables on True(Default is False in your connection string see https://dev.mysql.com/doc/connector-net/en/connector-net-6-10-connection-options.html
second get rid of the parameter code, because you replace mysql user defined variables #OutID with an actual value, which results in your error message.
With
SET #OutID = 10;"
You set in mysql a user defined variable #OutID to the numer 10.
But in general, following code add two parameters to a insert query
Def_Command_MySQL1.CommandText = "INSERT INTO myTable (myColumn1, myColumn2) VALUES (#C1, #C2)"
Def_Command_MySQL1.Parameters.AddWithValue("#C1", 1)
Def_Command_MySQL1.Parameters.AddWithValue("#C2", 2)
Def_Command_MySQL1.ExecuteNonQuery()

JDBC update database

i have a form and onClick listener i want to update some data on my database exception says that i have a syntax error but when i execute query at mysql console it works here is my code all variables are checked
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = pquant + ? where pname = ?");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
Error
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 'DVD Verdatim' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at buy$1.actionPerformed(buy.java:62)
As the compiler mentioned, there is indeed a Syntax error in your SQL query. Try:
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = #0 + 21" + "'variableForPquant'" "where pname = #1" + "'variableForPname'");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
You need to use quotes when you are using variables in your SQL query. I believe it is either
"'variableName'" or '"variableName'"
I found my error i declare a variable pt and i execute the query from the statement that is empty pst i change to pt.executeUpdate(); and its ok now

Update table using GUID

I am using MySQL with ASP.NET/VB. In a table I use GUID instead of int identifiers. All goes as planned until I try to update a specific row, where I get a syntax error in the statement below:
Dim q As String = "UPDATE documents SET date_document = #date_document, document_type = #document_type, sender = #sender, receiver = #receiver, description = #description, document_number = #document_number, pages = #pages, handled_date = current_timestamp, handled_user_id = #handled_user_id, error_code = #error_code) WHERE id = #id"
My GUID parameter:
.Parameters.Add("#id", MySqlDbType.Guid, 16).Value = myguid
And the error:
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 ') WHERE id = '8873442f-2f0b-4372-ac08-8388220c6eca'' at line 1
Any ideas on what's going on?
You're chasing down the wrong issue. What character does your error syntax begin with? It starts off as ') Where id = ...
You're assuming it's the id. It's not. That works fine. The first character is a closing parenthesis. That's the clue. There is no opening parenthesis. Remove the ) because you don't need it with an update statement.

Having problems crafting SQL statements to get tables and values in JSP

conn = DriverManager.getConnection(connURL);
String sqlStr = "Select * from inventory where functions" + "like ? order by brand, model";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setString(1, "%" + search + "%");
ResultSet rs = pstmt.executeQuery();
Hi guys, i am having error with this code at line 2 and line 4. I believe that my coding contains errors.
I have a doubt that my SQL query is not correctly formatted. The pstmt.setString will set the search value to the ? in the SQL query.
The error:
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 ''%null%' order by brand, model' at line 1
The syntax of your sql query declaration is wrong. That is why you're getting that error. Why are you including + in between ?
Try
conn = DriverManager.getConnection(connURL);//this is bad use a connection pool
StringBuilder sb = new StringBuilder().append("Select * from inventory where functions");
sb.append(" like ? order by brand, model");
String sqlStr = sb.toString().intern();//use intern if this function is used many times

Sql error occurred when I'm trying this query

Here is the query
$query_order = "select * from orders where key = '$pay_key'";
Error shown
SELECT
*
FROM `orders`
where `key` = 'C90320'
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 'key = 'C90320'' at line 1*
key is a reserved word. Change your query to:
$query_order = "select * from orders where `key` = '$pay_key'";
Also, I would recommend escaping the $pay_key's value. Say something like:
$pay_key = mysqli_real_escape_string($pay_key);
$query_order = "select * from orders
where `key` = '".mysqli_real_escape_string($pay_key)."'";
try this
first of all you need to compare it using string so code should like this
$query_order = "select * from orders where `key` = '".$pay_key."'";