String insert = "UPDATE PPN_WORKFLOW SET P1_F_Date = ?, SET P1_Completed = ?, SET C2_S_Date = ? WHERE ERF_No = ?";
stmt = conn.prepareStatement(insert);
stmt.setDate(1, date);
stmt.setInt(2, 1);
stmt.setDate(3, date);
stmt.setInt(4, erf_no);
stmt.executeUpdate();
I am trying to update a statement, but I am recieving this error
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3148)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:215)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPreparedStatement.java:137)
at Mails.MailtoERF.check(MailtoERF.java:60)
at Mails.MailtoERF.main(MailtoERF.java:122)
you can not set like that .you need to change syntax.One time SET is enough
UPDATE PPN_WORKFLOW SET P1_F_Date = ?, P1_Completed = ?..... where condition
You have a SET for each Column name, you need to separate columns with comma only and use SET only once, see syntax here:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You dont need SET for each column:
MySQL UPDATE SYNTAX:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
String insert = "UPDATE PPN_WORKFLOW SET P1_F_Date = ?, P1_Completed = ?,
C2_S_Date = ? WHERE ERF_No = ?";
Your Sql Update statement syntax is wrong.Do put 'SET' for each column in the query, you only do it at the first column
change it to
String insert = "UPDATE PPN_WORKFLOW SET P1_F_Date = ?, P1_Completed = ?, C2_S_Date = ? WHERE ERF_No = ?";
stmt = conn.prepareStatement(insert);
stmt.setDate(1, date);
stmt.setInt(2, 1);
stmt.setDate(3, date);
stmt.setInt(4, erf_no);
stmt.executeUpdate();
Heres the syntax for you reference
Related
I have written this code to add stuff to the database, however, when I try running it it does not work, i've been looking for ways to do it, but i just cant seem to find the solution, can anyone help?
String mySQL ="INSERT INTO Measurement (Level, Time, Date, TankID)"+"VALUES (textField1, currentTime,currentDate,(SELECT TankID FROM Tanks WHERE TankName = '2' AND Site_ID = '1'))";
stmt.executeUpdate(mySQL);
Both your SQL and prepared statement are malformed. Try using an INSERT INTO ... SELECT here:
String sql = "INSERT INTO Measurement (Level, Time, Date, TankID) ";
sql += "SELECT ?, ?, ?, TankID ";
sql += "FROM Tanks ";
sql += "WHERE TankName = '2' AND Site_ID = '1'";
stmt.setString(1, textField1);
stmt.setString(2, currentTime); // not sure about the type here
stmt.setString(3, currentDate); // also not sure about the type
stmt.executeUpdate();
Note that I am unsure about both the Java and SQL binding types of the columns for currentTime and currentDate. If not string, then the above would have to change slightly.
You should be using PreparedStatement to properly set the first parameter of the insert query and check the documentation of your DB server to use existing functions to get current time and date.
For example, mySQL has functions CURDATE() and CURTIME()
String query = "INSERT INTO Measurement (Level, Time, Date, TankID) "
+ "VALUES (?, CURTIME(), CURDATE(), (SELECT TankID FROM Tanks WHERE TankName = '2' AND Site_ID = '1'))";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, textField1); // could be textField1.getText() or textField1.getValue()
statement.executeUpdate();
}
Based on your Database type change the connection details
Follow this Link for creating JDBC Connection and Inserting data
If you did the above steps please ignore this..
I have a problem using mysql update in eclipse jsp file.
I just use pstmt.executeUpdate in my jsp file. that's work right.
but when I reuse pstmt.executeUpdate for updating another host's database i can't update mysql data.
when I update second executeUpdate without update's "where" query, it's work right. I can update mysql data.
but only the problem is that when i update executeUpdate with "where", I cannot update the mysql data.
case1(no problem)
use preparedStatement1, update query with 'where' -> OK
use preparedStatement2, update query without 'where' -> OK
case2(problem)
use preparedStatement1, update query with 'where' -> OK
use preparedStatement2, update query with 'where' -> update fail
(and I already test my code in mysql workbench, there is no problem first UPDATE and second UPDATE with 'where'. I have only a problem adding update with 'where' code in my eclipse.)
behind is a my code.
String DB_USER = (String)session.getAttribute("id");
String DB_PASSWORD = (String)session.getAttribute("password");
String DB_HOST = "jdbc:mysql://rds01.********.ap-northeast-2.rds.amazonaws.com:3306/inventory_" + DB_USER + "?useUnicode=true&characterEncoding=euckr";
Connection con;
PreparedStatement pstmt;
String query = "UPDATE inventory_"+DB_USER+" SET "
+ "color1_size1 = ?, color1_size2 = ?, color1_size3 = ?, color1_size4 = ?, color1_size5 = ?, color1_size6 = ?, color1_size7 = ?, color1_size8 = ? "
+ "WHERE NAME = ? AND YEAR = ?";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DB_HOST, DB_USER, DB_PASSWORD);
pstmt = con.prepareStatement(query);
pstmt.setInt(1, color1_size1);
pstmt.setInt(2, color1_size2);
pstmt.setInt(3, color1_size3);
pstmt.setInt(4, color1_size4);
pstmt.setInt(5, color1_size5);
pstmt.setInt(6, color1_size6);
pstmt.setInt(7, color1_size7);
pstmt.setInt(8, color1_size8);
pstmt.setString(9, name);
pstmt.setString(10, year);
pstmt.executeUpdate();
pstmt.close();
con.close();
String query_all = "UPDATE inventory_all SET "
+ "color1_size1 = ?, color1_size2 = ?, color1_size3 = ?, color1_size4 = ?, color1_size5 = ?, color1_size6 = ?, color1_size7 = ?, color1_size8 = ? "
+ "WHERE NAME = ? AND YEAR = ?";
//behind host is another host with upper host.
DB_HOST = "jdbc:mysql://rds01.************.ap-northeast-2.rds.amazonaws.com:3306/inventory_all?useUnicode=true&characterEncoding=euckr";
con = DriverManager.getConnection(DB_HOST, DB_USER, DB_PASSWORD);
pstmt = con.prepareStatement(query_all);
pstmt.setInt(1, color1_size1);
pstmt.setInt(2, color1_size2);
pstmt.setInt(3, color1_size3);
pstmt.setInt(4, color1_size4);
pstmt.setInt(5, color1_size5);
pstmt.setInt(6, color1_size6);
pstmt.setInt(7, color1_size7);
pstmt.setInt(8, color1_size8);
pstmt.setString(9, name);
pstmt.setString(10, year);
//------------upper code is OK, behind code is a problem ----------------
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch(Exception e) {e.printStackTrace();}
Use different PreparedStatement object for second query. Because
a
preparedstatement asks for the sql and immediatly compiles itself.
PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.executeUpdate();
PreparedStatement pstmt2 = con.prepareStatement(sql2);
pstmt2.executeUpdate();
oh, I know the problem. I add the mysql privilege only update, delete, insert not select in database id.
as I add the 'select' privilege to my id, I can update in my database.
thank for all answer!
I am trying to insert into a table without duplicates. I am using SQL derbyclient in Java. The code is not working (error with 'where not exists'). Any idea?
Connection connection = DriverManager.getConnection("jdbc:derby://localhost:1527/PetroleumDataStorageSystemDB;create=true");
PreparedStatement statement;
int i = 1;
int progress = 0;
for (Refuel refuelList1 : refuelList) {
progress = (i / refuelList.size()) * 100;
String sql = "INSERT INTO refuel (id_tank, time, fuel_volume, "
+ "refueling_speed) VALUES (?,?,?,?) "
+ "WHERE NOT EXISTS (SELECT * FROM refuel WHERE "
+ "id_tank = ? AND time = ? AND fuel_volume = ? AND "
+ "refueling_speed = ?)";
statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, refuelList1.getTankID());
statement.setString(2, refuelList1.getTime());
statement.setDouble(3, refuelList1.getFuelVolume());
statement.setDouble(4, refuelList1.getRefuelingSpeed());
statement.execute();
i++;
}
The problem is how you build your query. That isnt a valid INSERT syntaxis
Try something like this on your db first with dummy values.
INSERT INTO refuel (id_tank, time, fuel_volume, refueling_speed)
SELECT ?, ?, ?, ?
FROM refuel
WHERE NOT EXISTS (SELECT
FROM refuel
WHERE
id_tank = ?
AND time = ?
AND fuel_volume = ?
AND refueling_speed = ?);
There are at least 8 question mark in your statement.
You have to set all of them!
After that you can check again and see if there are other errors.
You can't have a WHERE clause after a VALUES list.
Have a look here for valid Derby INSERT statement syntax.
Try something like this instead (notice the use of sysibm.sysdummy1, which guarantees that you only ever insert a single record at most. If you put an actual table name in there, you may potentially insert multiple records a time. Careful with that.):
INSERT INTO refuel (id_tank, tank, fuel_volume, refueling_speed)
SELECT ?, ?, ?, ?
FROM sysibm.sysdummy1
WHERE NOT EXISTS (SELECT *
FROM refuel
WHERE id_tank = ?
AND time = ?
AND fuel_volume = ?
AND refueling_speed = ?)
Also, as noted by Davide Lorenzo MARINO, make sure you set all the binding parameters correctly.
When i am running the following SQL query i am getting the following error:
java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification
String strQuery = "UPDATE themed_night SET theme_night_name = ?, theme_night_date = TO_DATE(?, 'dd-MM-yy'), theme_night_description = ?, WHERE theme_id = ?";
PreparedStatement stmt = conn.prepareStatement(strQuery);//prepare the SQL Query
stmt.setString(1, title);
stmt.setString(2, output);
stmt.setString(3, details);
stmt.setString(4, themeID);
Is my SQL query syntax correct? I have double checked the columns and i have typed the correct names.
You have an extra comma in your statement. The correct statement should be
String strQuery = "UPDATE themed_night SET theme_night_name = ?, theme_night_date = TO_DATE(?, 'dd-MM-yy'), theme_night_description = ? WHERE theme_id = ?";
Also, you might need to enclose some values with single quotes (though I'm not sure about that)
I have a problem during an insert in Oracle using Java and JDBC. The error obtained is:
java.sql.SQLException: ORA-00917: missing comma
The data for the insert is taken from a form like a string and is parsed to the appropiated data type and then is saved in an object called edicio. That's all OK. Then, my intention is make an insert in the DB using the data of this object.
Here is the code of the DAO, where I'm making the insert:
public Edicio insertarCurs(Connection con, Edicio ed) throws SQLException {
PreparedStatement stm = null;
ResultSet rst = null;
// Insert
StringBuffer sql = new StringBuffer();
sql.append("INSERT INTO curs (id, nom, idarea, area, programa, datainici)");
sql.append(" VALUES (?, ?, ?, ?, ?, ?");
logger.info("Building insert works fine.");
try {
stm = con.prepareStatement(sql.toString());
// params
stm.setLong(1, ed.getIdEdicio());
stm.setString(2, ed.getNomEdicio());
stm.setLong(3, ed.getIdArea());
stm.setString(4, ed.getArea());
stm.setString(5, ed.getPrograma());
// Conversion from Java Date to SQL Date
java.sql.Date sqlDate = new java.sql.Date(ed.getDataInici().getTime());
logger.info("sqlDate before the insert is: "+ sqlDate); //0011-12-02
stm.setDate(6, sqlDate);
// Data and results commented
logger.info("Id edicio: "+ ed.getIdEdicio()); //6
logger.info("Nom edicio: "+ ed.getNomEdicio()); //test
logger.info("Id area: "+ ed.getIdArea()); //0
logger.info("Nom area: "+ ed.getArea()); //test
logger.info("Programa: "+ ed.getPrograma()); //test
logger.info("Data inici: "+ sqlDate); //2011-06-06
// We are going to execute the insert
int numRows = stm.executeUpdate();
// The program never reaches this point, fails doing the executeUpdate()
logger.info("Rows created: "+ numFiles);
...
The variable types are:
idEdicio = long
nomEdicio = String
idArea = long
area = String
programa = String
dataInici = Date
Can someone help me? Thank you in advance :)
Missing )
sql.append(" VALUES (?, ?, ?, ?, ?, ?");
should be
sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
^--- missing parenthesis