I have created a query that is working properly in MySQL workbench but when i run it in eclipse is not working. Connection with database is working properly because with simpler queries I had correct results.
java code:
String queryAlter = "SELECT count(*) ";
queryAlter += "INTO @exist ";
queryAlter += "FROM information_schema.columns ";
queryAlter += "WHERE table_schema = database() ";
queryAlter += "and COLUMN_NAME = 'question" + (100 + number + 1)+"' ";
queryAlter += "AND table_name = '"+ tableName+"'; ";
queryAlter += "set @query = IF(@exist <= 0, 'alter table "+ tableName+" add column question" + (100 + number + 1)+" varchar(2048) NULL', ";
queryAlter += "'select \\'Column Exists\\' status'); ";
queryAlter += "use joomla30; ";
queryAlter += "prepare stmt from @query; ";
queryAlter += "EXECUTE stmt;";
and I get this message:
An error occurred. Maybe user/password is invalid 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 'set @query = IF(@exist <= 0, 'alter table n0x3c_twoquestions_table add column qu' at line 1
also the string of query is:
SELECT count(*) INTO @exist FROM information_schema.columns WHERE table_schema = database() and COLUMN_NAME = 'question101' AND table_name = 'n0x3c_twoquestions_table'; set @query = IF(@exist <= 0, 'alter table n0x3c_twoquestions_table add column question101 varchar(2048) NULL', 'select \'Column Exists\' status'); use joomla30; prepare stmt from @query; EXECUTE stmt;
I also tried this in java that I have read from MySQL Query works in PhpMyAdmin but not in JAVA Eclipse :
String s1 = "SELECT count(*) INTO @exist FROM information_schema.columns WHERE table_schema = database() and COLUMN_NAME = 'question" + (100 + number + 1)+"' AND table_name = '"+ tableName+"'; ";
String s2 = "set @query = IF(@exist <= 0, 'alter table "+ tableName+" add column question" + (100 + number + 1)+" varchar(2048) NULL', 'select \\'Column Exists\\' status'); ";
String s3 = "use joomla30; ";
String s4 = "prepare stmt from @query; ";
stmt.addBatch(s1);
stmt.addBatch(s2);
stmt.addBatch(s3);
stmt.addBatch(s4);
stmt.executeBatch();
conn1.commit();
but I have this error:
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate().
You can use two different queries.
The first is to check the existence of the column:
String countQuery = "select count(*) from information_schema.columns WHERE table_schema = database() and COLUMN_NAME = 'question106' and table_name='n0x3c_twoquestions_table'";
Then if countQuery
returns 0, you can execute the alterQuery
:
String alterQuery = "alter table n0x3c_twoquestions_table add column question106 varchar(2048) NULL";
If you don't want to use two differents queries, you can handle this with exception catching:
String alterQuery = "alter table n0x3c_twoquestions_table add column question106 varchar(2048) NULL";
try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(alterQuery)) {
ps.execute();
} catch (SQLException e) {
if (e.getErrorCode() == 1060) {
columnExists = true;
} else {
// TODO handle exception
}
}
I found the error code MySQL Server error codes.