I would like to have a value from a row inserted into an other row here is my code:
static void addVipMonth(String name) throws SQLException
{
Connection conn = (Connection) DriverManager.getConnection(url, user, pass);
PreparedStatement queryStatement = (PreparedStatement) conn.prepareStatement("INSERT INTO vips(memberId, gotten, expires) " +
"VALUES (SELECT name FROM members WHERE id = ?, NOW(), DATEADD(month, 1, NOW()))"); //Put your query in the quotes
queryStatement.setString(1, name);
queryStatement.executeUpdate(); //Executes the query
queryStatement.close(); //Closes the query
conn.close(); //Closes the connection
}
This code is not valid. How do I correct it?
I get an error 17:28:46 [SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MyS QL server version for the right syntax to use near ' NOW(), DATE_ADD( now(), INT ERVAL 1 MONTH )' at line 1 – sanchixx
It was due to error in SELECT .. statement.
Modified statement is:
INSERT INTO vips( memberId, gotten, expires )
SELECT name, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )
FROM members WHERE id = ?
You don't require VALUES key word when inserting with a select.
You used a wrong DATEADD function syntax. Correct syntax is Date_add( date_expr_or_col, INTERVAL number unit_on_interval).
You can try your insert statement as corrected below:
INSERT INTO vips( memberId, gotten, expires )
SELECT name FROM members
WHERE id = ?, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )
Refer to:
INSERT ... SELECT Syntax
DATE_ADD(date,INTERVAL expr unit)
Related
I would like to have a value from a row inserted into an other row here is my code:
static void addVipMonth(String name) throws SQLException
{
Connection conn = (Connection) DriverManager.getConnection(url, user, pass);
PreparedStatement queryStatement = (PreparedStatement) conn.prepareStatement("INSERT INTO vips(memberId, gotten, expires) " +
"VALUES (SELECT name FROM members WHERE id = ?, NOW(), DATEADD(month, 1, NOW()))"); //Put your query in the quotes
queryStatement.setString(1, name);
queryStatement.executeUpdate(); //Executes the query
queryStatement.close(); //Closes the query
conn.close(); //Closes the connection
}
This code is not valid. How do I correct it?
I get an error 17:28:46 [SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MyS QL server version for the right syntax to use near ' NOW(), DATE_ADD( now(), INT ERVAL 1 MONTH )' at line 1 – sanchixx
It was due to error in SELECT .. statement.
Modified statement is:
INSERT INTO vips( memberId, gotten, expires )
SELECT name, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )
FROM members WHERE id = ?
You don't require VALUES key word when inserting with a select.
You used a wrong DATEADD function syntax. Correct syntax is Date_add( date_expr_or_col, INTERVAL number unit_on_interval).
You can try your insert statement as corrected below:
INSERT INTO vips( memberId, gotten, expires )
SELECT name FROM members
WHERE id = ?, NOW(), DATE_ADD( now(), INTERVAL 1 MONTH )
Refer to:
INSERT ... SELECT Syntax
DATE_ADD(date,INTERVAL expr unit)
I'm trying to insert values into a table (inquiry) the first value is of type Date , and I'm getting an SQL error SQL State S1009. what is the proper way to convert the date , what am I doing wrong?
String sqlStatement = "INSERT INTO inquiry (INQUIRY_DATE,INQUIRY_NOTE,INQUIRER_ID,PROGRAM_ID,CLASS_ID,CORPORATE_ID)\n"
+ "VALUES (?,?,?,?);";
ps = con.prepareStatement(sqlStatement);
java.sql.Date sDate = new java.sql.Date(inquiry.getInquiryDate().getTime());
int parameterIndex = 1;
ps.setDate(parameterIndex, sDate);
ps.setString(parameterIndex++, inquiry.getInquiryNote());
ps.setInt(parameterIndex++, inquiry.getInquirer().getInquirerID());
ps.setInt(parameterIndex++, inquiry.getProgramID());
ps.setInt(parameterIndex++, inquiry.getClassProgramID());
ps.setInt(parameterIndex++, 1);
sqlStatement = "INSERT INTO inquiry (INQUIRY_DATE,INQUIRY_NOTE,INQUIRER_ID,PROGRAM_ID,CLASS_ID,CORPORATE_ID)\n"
+ "VALUES (?,?,?,?);";
The parameterized query doesn't have enough ?, you queried 6 columns with 2 ? missing, it should be VALUES (?,?,?,?,?,?); ? are used for holding the places for your setXXX() column values
this is y inserting syntax
String add="INSERT INTO time_table VALUES("+coursename+"','"+coursecode+"','"+days+"','"+year+"','"+dep+"','"+time+"','"+hall+"','"+lecturer+"','"+credithours+"')";
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 '','5','2','2','8','1','9','PA','2')' at line 1
that is what am receiving as an error.
I think correct version of your query is this :
"INSERT INTO time_table VALUES('"+coursename+"','"+coursecode+"','"+days+"','"+year+"','"+dep+"','"+time+"','"+hall+"','"+lecturer+"','"+credithours+"')";
you forgot ' in beginning of +coursename+
Don't ever use this way, it can cause Syntax error, ot SQL Injection, you have to use PreparedStatement instead for example :
String add = "INSERT INTO time_table VALUES(?, ?, ?, ?, ?,?,?,?,?)";
try (PreparedStatement insert = connection.prepareStatement(add)) {
insert.setString(1, coursename);
insert.setString(2, coursecode);
insert.setString(3, days);
insert.setString(4, year);
insert.setString(5, dep);
insert.setString(6, time);
insert.setString(7, hall);
insert.setString(8, lecturer);
insert.setString(9, credithours);
insert.executeUpdate();
}
Note if your attributes are int or float or date or ... in your table, then you have to use the correct set, for example if the year is an in you can use insert.setInt(4, year); instead.
Your real problem is in your query you miss ' here :
INSERT INTO time_table VALUES('" + coursename + "'
//----------------------------^
i'm writing this code to get the values from textfields and update a register in my database.. but it shows "Syntax error : Encountered "\" at line 1,column 196
and when i select another row from the jtable the column number in syntax error change..Can anyone tell me what's the wrong here and how can i fix it
here's the code :
String up = "UPDATE BENEFICTOR SET ID='"+T1.getText().toString()+"', FID='"+T2.getText().toString()+"', FULLNAME='"+T3.getText().toString()+"', GENDER='"+T4.getSelectedItem().toString()+"', IDNUM='"+T5.getText().toString()+"', STATUS='"+T6.getSelectedItem().toString()+"', ORIGINALAREA='"+T7.getText().toString()+"', RECENTAREA='"+T8.getText().toString()+"', EVAL='"+T9.getSelectedItem().toString()+"', PHONENYMBER1='"+T10.getText().toString()+"', PHONENUMBER2='"+T11.getText().toString()+"', SITUATION='"+T12.getText().toString()+"', VISIT='"+T13.getText().toString()+" WHERE ID="+T1.getText().toString();
Get rid of that code and use a PreparedStatement. The PreparedStatement will replace each "?" for you with the related parameter and use the proper delimiters:
To get you started:
String sql = "UPDATE BENEFICTOR SET ID = ?, FID = ?, .... WHERE ID = ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, T1.getText() );
stmt.setString( 2, T2.getText() );
...
stmt.setString( ?, T1.getText() );
stmt.executeUpdate();
stmt.close();
assemble sql like this is not smart,you have just miss a ','
VISIT='"+T13.getText().toString()+" WHERE ID="+T1.getText().toString();
and the other careless behaviour is ...
"UPDATE BENEFICTOR SET ID='"+T1.getText().toString()+"',
" WHERE ID="+T1.getText().toString();
focus on the quotes that you added at first.
How can i use this sql query in Java Program.
String SelectSQL = "SELECT (CASE WHEN cnt >= 1
THEN 'True'
ELSE 'False'
END) result_str
FROM ( SELECT COUNT(*) cnt FROM event WHERE externaleventid ='1256294' )";
I am using Preparatory statement to pass the externaleventid. Other query works fine when I am using column name but when I don't use column name I am getting the error "Invalid column name" When I run this query in SQL developer it executes successfully.
Added java code:
String query = " SELECT (CASE WHEN COUNT(*) >= 1 THEN 'True' ELSE 'False' END) AS result_str FROM event WHERE externaleventid = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,"1256294");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String externaleventid = rs.getString("DISPLAYNAME");
System.out.println("externaleventid : " + externaleventid);
}
You should write
"SELECT (CASE WHEN cnt >= 1
THEN 'True'
ELSE 'False'
END) AS result_str
FROM ( SELECT COUNT(*) AS cnt FROM event WHERE externaleventid ='1256294' )";
instead of
"SELECT (CASE WHEN cnt >= 1
THEN 'True'
ELSE 'False'
END) result_str
FROM ( SELECT COUNT(*) cnt FROM event WHERE externaleventid ='1256294' )";
You are trying to get the result from a column named DISPLAYNAME which does not exist in your query. One option is to ignore the name and retrieve by index:
rs.getString(1);
Another is to retrieve by name, but use the name of the alias that was on the query:
rs.getString("result_str");