I'm getting the following error :
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 '' at line 1.
While trying to run the following code:
String sql = "INSERT INTO `tutors`.`appointments`"
+ "(`tutorID`, `tuteeName`, `tuteeEmail`, `time`, `date`)"
+ ("VALUES(?, ?, ?, ?, ?");
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, working.get(0).getTutorID());
ps.setString(2, tuteeName);
ps.setString(3, tuteeEmail);
ps.setDate(4, date);
ps.setTime(5, time);
ps.executeUpdate();
What is it producing the ' ' that the exception is referring to? I'm just learning to work with SQL in java, so I'm sure this is just a silly syntactical error...
You were not closing the VALUES parenthesis:
String sql = "INSERT INTO `tutors`.`appointments`"
+ " (`tutorID`, `tuteeName`, `tuteeEmail`, `time`, `date`)"
+ ("VALUES(?, ?, ?, ?, ?)");
Problem with your sql string. It should be
"INSERT INTO `tutors`.`appointments`"
+ "(`tutorID`, `tuteeName`, `tuteeEmail`, `time`, `date`)"
+ "VALUES(?, ?, ?, ?, ?)";
may be in your first '(' in third line.
String sql = "INSERT INTO `tutors`.`appointments`"
+ "(`tutorID`, `tuteeName`, `tuteeEmail`, `time`, `date`)"
+ ("VALUES(?, ?, ?, ?, ?");
it should be
String sql = "INSERT INTO `tutors`.`appointments`"
+ " (`tutorID`, `tuteeName`, `tuteeEmail`, `time`, `date`)"
+ " VALUES(?, ?, ?, ?, ?");
add some space in begining of second and third line also.
Related
my error:
SEVERE: null
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 'WHERE CustLogin LIKE 'patryk'' at line 1.
This is update:
"UPDATE customers SET CustPassword = ?, CustFirstName = ?," +
" CustLastName = ?, CustAddress = ?, CustCity = ?, CustProv = ?, CustPostal = ?," +
" CustCountry = ?, CustHomePhone = ?, CustBusPhone = ?, CustEmail = ?, AgentId = ?," +
" WHERE CustLogin LIKE ?";
kompilator has problem with stmt.executeUpdate();
there are screens code :
I think update don't have mistakes and database is working properly because for example, I can add customer
Do you have some ideas?
I think the error is because of the "," before WHERE CustLogin...
Replace it with,
"UPDATE customers SET CustPassword = ?, CustFirstName = ?," +
" CustLastName = ?, CustAddress = ?, CustCity = ?, CustProv = ?, CustPostal = ?," +
" CustCountry = ?, CustHomePhone = ?, CustBusPhone = ?, CustEmail = ?, AgentId = ?" +
" WHERE CustLogin LIKE ?";
String sql = " INSERT INTO `tblservice` (`ServiceID`,`accountID`, `Kind`, `Description`, `Price`, "
+ "`Quantity`, `Total`, `DateAndTime`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setInt(1, this.accountID);
pstm.setString(2, "" + SelectionBox.getSelectedItem());
pstm.setString(3, desc);
pstm.setFloat(4, Float.parseFloat(PriceTF.getText()));
pstm.setFloat(5, Float.parseFloat(QuantityTF.getText()));
pstm.setFloat(6, this.getTotal());
pstm.setDate(7, dateAdded);
pstm.executeUpdate();
Error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '?, ?, ?, ?, ?, ?, ?)' at line 1
Instead of make NULL in your query VALUES (NULL, ...) use setNull for example :
pstm.setNull(1, java.sql.Types.INTEGER);
It will take the type, of your field, in this example i consider it is a java.sql.Types.INTEGER it can be java.sql.Types.VARCHAR or any sql type
So your query should be like this :
String sql = "INSERT INTO tblservice (ServiceID, accountID, Kind, Description,
Price, Quantity, Total, DateAndTime)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setNull(1, java.sql.Types.INTEGER);
pstm.setInt(2, this.accountID);
....
I have a problem with PreparedStatement.
This is my function that adds new user to MySQL database:
public static void createUser(String fn, String sn, String log, String pass, int accNum, String qst, String answ) {
try {
Connection conn = (Connection) mySQLConnector.getConnection();
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(
"INSERT INTO users"
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer)"
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?");
ps.setString(1, fn);
ps.setString(2, sn);
ps.setString(3, log);
ps.setString(4, pass);
ps.setInt(5, accNum);
ps.setDouble(6, 0);
ps.setString(7, qst);
ps.setString(8, answ);
ps.executeUpdate();
ps.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
And this is an error that I get:
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 '' at line 1
I have no idea what is wrong with my query. ColumnNames are ok, function arguments are also ok.
I've tried adding '' to columns names (like that: 'FirstName') but i t still doesn't work...
EDIT:
Adding spaces did not helped.I even made it in one line:
"INSERT INTO users (FirstName, LastName, Login, Password,
AccountNumber, Ballance, Question, Answer) VALUES (?, ?, ?, ?, ?, ?,
?, ?)"
and still gives the same error
You are missing spaces. Change your SQL to :
"INSERT INTO users " // space added
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer) " // space added
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
EDIT : I missed the missing closing bracket at the end of the VALUES clause.
I think you just need to add spaces and bracket
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(
"INSERT INTO users "
+ "(FirstName, LastName, Login, Password, AccountNumber, Ballance, Question, Answer) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
Fix this:
"VALUES (?, ?, ?, ?, ?, ?, ?, ?");
to this:
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
I have this Java Object which I will use to store counted elements from table:
private DCDataObj dc;
public class DCDataObj
{
private int datacenter; // Datacenters
.............
public DCDataObj()
{
}
public DCDataObj(int datacenter..........)
{
this.datacenter = datacenter;
..........
}
public int getDatacenter()
{
return datacenter;
}
public void setDatacenter(int datacenter)
{
this.datacenter = datacenter;
}
..........
}
I use this SQL query to count the components into the Oracle table:
ps = conn.prepareStatement("SELECT COUNT(1) AS CNT FROM COMPONENTSTATS CS, COMPONENTTYPE CT "
+ " WHERE CS.COMPONENTTYPEID = CT.COMPONENTTYPEID AND CT.COMPONENTTYPEID IN ( "
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? " // 10
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? " // 20
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? " // 30
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) " // 40
+ " GROUP BY CT.NAME ORDER BY CT.NAME");
ps.setInt(1, 1000);
......
I get the result using this Java code:
ResultSet result = ps.executeQuery();
while (result.next())
{
dc = new DCDataObj(
result.getInt(1),
result.getInt(2),
result.getInt(3),
...........
);
}
I get this problem when I execute the query:
java.sql.SQLException: Invalid column index
Can you help me how I can solve this problem?
UPDATE
The SQL query works. I get this result:
CNT
----------------------
1
1
1 1
I suspect that the problem is into the return type. I suppose that I get the result as array. But can I somehow inset the result from the query into the Java object without using Array?
On each line, you end with a "?" but the next line starts with another "?" without a comma. Then you wind up with part of the string looking like ", ? ?," which is invalid JDBC syntax. You need commas in between all your "?" placeholders.
Try this, with commas added at the end of your lines "10", "20", and "30".
ps = conn.prepareStatement("SELECT CT.NAME AS COMPONENT_TYPE, COUNT(1) AS CNT FROM COMPONENTSTATS CS, COMPONENTTYPE CT "
+ " WHERE CS.COMPONENTTYPEID = CT.COMPONENTTYPEID AND CT.COMPONENTTYPEID IN ( "
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " // 10
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " // 20
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " // 30
+ " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) " // 40
+ " GROUP BY CT.NAME ORDER BY CT.NAME");
EDIT
Now that I see your data, I see the problem. You cannot call getInt referencing the data, only the column header name or the 1-based column index. Also, your "COMPONENT_TYPE" is alphanumeric, please use getString instead of getInt. That also means you'll have to change your DCDataObj class to have a String for datacenter, not an int.
Try
dc = new DCDataObj(
result.getString("COMPONENT_TYPE"),
...........
);
or
dc = new DCDataObj(
result.getString(1),
...........
);
A colon in a bind variable or INTO specification was followed by an inappropriate name, perhaps a reserved word. You need to change the variable name and retry the operation. Did you try to get results from your query using pl/sql or SQL plus or your oracle terminal? Just to ensure you're executing the right query.
There is no column called "DATACENTER" fetched in the SELECT statement. It should be either COMPONENT_TYPE or CNT in result.getInt call.
I have this code:
Date start = new Date(Integer.parseInt(jTextField4.getText()), Integer.parseInt(jTextField16.getText()), Integer.parseInt(jTextField17.getText()));
Date end = new Date(Integer.parseInt(jTextField5.getText()), Integer.parseInt(jTextField18.getText()), Integer.parseInt(jTextField19.getText()));
statement = connection.createStatement();
preparedStatement1 = connection.prepareStatement("insert into sportmangg(customer_code,"
+ "sportman_code, start, finish, salary,amount,box salary,private salary, food salary, "
+ "other salary, bime salary, number) "
+ "values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?");
preparedStatement1.setString(1,jTextField15.getText());
preparedStatement1.setString(2, jTextField1.getText());
preparedStatement1.setDate(3, start);
preparedStatement1.setDate(4, end);
preparedStatement1.setInt(5, Integer.parseInt(jTextField6.getText()) );
preparedStatement1.setInt(6,Integer.parseInt(jTextField14.getText()) );
preparedStatement1.setInt(7, Integer.parseInt(jTextField7.getText()));
preparedStatement1.setInt(8, Integer.parseInt(jTextField8.getText()));
preparedStatement1.setInt(9, Integer.parseInt(jTextField9.getText()));
preparedStatement1.setInt(10, Integer.parseInt(jTextField11.getText()));
preparedStatement1.setInt(11, Integer.parseInt(jTextField10.getText()));
preparedStatement1.setInt(12, Integer.parseInt(jTextField20.getText()));
preparedStatement1.executeUpdate();
but it has this 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 'salary,private salary, food salary, other salary, bime salary, number) values ('' at line 1
What is the problem?
You really shouldn't have spaces in the field name. Try surrounding it with ``
Column names with spaces in them are a very bad idea.
If you must have them, surround them with backticks:
`private salary`
You missed ) in the last line of your SQL query so it should be:
+ " values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ? )";
Maybe you can try this:
https://github.com/stuparmihailo/util4j/releases/tag/v1.0
It's some simple project and has nice way for creating statements:
String query = "INSERT INTO table VALUES (?,?,?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(query);
StatementUtil.fill(stmt, 45, "text", 2, null, new Date(), false, 3.5);
You should replace private salary with private_salary and keep working with acceptable column name conventions.
column or table names should not have spaces. Join them by underscore. and make them upper case... these are not rules but accepted ways of working with DB objects. If names cannot be changed in the DB and you are stuck with something like some salary, then some salary should help.
mehdi;
I think what you have to do is all of this:
change names of
space-named columns (private salary,
food salary, other salary, bime
salary) either by replacing spaces by underscores
(recommended by naming conventions) or by
surrounding names with grave accent char:
`box salary`, `private salary`, `food salary`, `other salary`, `bime
salary`
Fix this line adding final parentheses
+ "values (? ,?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?");
it must say:
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
Finally I'd recommend to take out prepareStatement argument to a String or StringBuffer variable, say "sqlString" or something, so you can manipulate it more transparently. Something like this:
String sqlString = "";
sqlString += " insert into sportmangg";
sqlString += " (customer_code, sportman_code, start, finish,";
sqlString += " salary, amount, box_salary, private_salary,";
sqlString += " food_salary, other_salary, bime_salary, number)";
sqlString += " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
preparedStatement1 = connection.prepareStatement(sqlString);
(or if you use StringBuffer use append method)