【发布时间】:2014-09-29 08:55:33
【问题描述】:
我创建了一个在 MySQL 工作台中可以正常工作的查询,但是当我在 eclipse 中运行它时却无法正常工作。与数据库的连接工作正常,因为通过更简单的查询我得到了正确的结果。
java代码:
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;";
我收到这条消息:
发生错误。也许用户/密码无效 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'set @query = IF(@exist 附近使用正确的语法
查询字符串也是:
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;
我也在java中试过这个,我从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();
但我有这个错误:
java.sql.BatchUpdateException: 无法通过 executeUpdate() 发出 SELECT。
【问题讨论】:
标签: java mysql eclipse mysql-workbench