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
Related
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
This question already has an answer here:
insert query with varchar as data type for primary key
(1 answer)
Closed 8 years ago.
hello i have a problem with this resultset.
public ResultSet seleccionar(String id, String pass) throws SQLException {
PreparedStatement stmt;
String consulta = "SELECT iduser from personas ";
consulta+= " WHERE nombre=?";
consulta+= " AND pass= ?";
objetoConexion = Conexion.getInstance().getConnection();
stmt = objetoConexion.prepareStatement(consulta);
stmt.setString(1,id);
stmt.setString(2,pass);
ResultSet rs = stmt.executeQuery(consulta);
rs.first();
return rs;
this is my error
.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 '? AND pass= ?' at line 1
You need to check you spaces on your String consulta.
Have you considered using a StringBuffer and logging your SQL statements to the console so you can see what the queries look like?
You have to put a space before AND.
String consulta = "SELECT iduser from personas ";
consulta+= " WHERE nombre='?";
consulta+= "' AND pass='?'";
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+"' ");
I get this error, while I'm testing the code below:
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, LASTNAME, FIRSTNAME, SCHOOL) VALUES ('333', 'aaa', 'aaa', 'aaa')' at line 1
I just recycled the code that I used in manipulating ms sql database. So the syntax must be wrong. What might be the correct syntax for adding records into mysql database?
Here is my current code:
idnum = TextBox1.Text
lname = TextBox2.Text
fname = TextBox3.Text
skul = TextBox4.Text
Using sqlcon As New MySqlConnection("Server=localhost; Database=testing;Uid=root;Pwd=nitoryolai123$%^;")
sqlcon.Open()
Dim sqlcom As New MySqlCommand()
sqlcom.Connection = sqlcon
sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (#ParameterID, #ParameterLastName, #ParameterFirstName, #ParameterSchool)"
sqlcom.Parameters.AddWithValue("#ParameterID", TextBox1.Text)
sqlcom.Parameters.AddWithValue("#ParameterLastName", TextBox2.Text)
sqlcom.Parameters.AddWithValue("#ParameterFirstName", TextBox3.Text)
sqlcom.Parameters.AddWithValue("#ParameterSchool", TextBox4.Text)
sqlcom.ExecuteNonQuery()
End Using
Please help, thanks
Try removing the square brackets from the student table name, and adding a space between table name and column list: [student] (ID, ...
Without your table schema definition I can't be 100% sure, but ParameterID appears to be an int, but you are passing as a string (i.e. value is wrapped in single quotes). Try converting TextBox1.Text to an int first (using Try.Parse)
I'd like to obtain the metadata from the results of a mysql query so I can put it into a datatable in c#. I primarily need the column name and the data type. Here's a simple example.
show columns from (
select sum(subtotal) as subtotal
, sum(tax1+tax2+tax3+tax4) as tax
, sum(subtotal)+sum(tax1+tax2+tax3+tax4) as total
from tbltransmaster
where batchnum in (
SELECT BatchNum
FROM tblshiftmaster
WHERE ClosingTS <> -1
ORDER BY OpeningTS ASC
)
) as x
It generates this error though
Error Code : 1064 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 '(
select sum(subtotal) as subtotal
, sum(tax1+tax2+tax3+tax4) as tax
, sum' at line 1
Could someone provide me with some advice, or perhaps a link to assist with this?
show columns only works for actual tables and views, not for queries.
Since you mention C#, you're probably using ADO.NET ? You can directly fill a DataTable which figure out the data types and column names.
DbConnection conn = null;
DbConnection conn = null;
DbCommand cmd = null;
DbDataAdapter da = null;
DataTable tbl = new DataTable();
conn = new MySqlConnection(connectionString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = query;
da = new MySqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(tbl);
If you want to get the colum names only, you could fetch the result using a DataReader instead:
DbDataReader dbReader = cmd.ExecuteReader();
for (int i = 0; i < dbReader.FieldCount; i++) {
Type colType = dbReader.GetFieldType(i);
String colName = dbReader.GetName(i);
}
DbDataReader also have the .GetSchemaTable() which returns a DataTable describing the metadata of the result.