java - 查询在 MySql workbench 中运行,但不在 Java eclipse 中运行

标签 java mysql eclipse mysql-workbench

我创建了一个在 MySQL Workbench 中正常工作的查询,但是当我在 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 服务器版本对应的手册,了解在第 1 行的 'set @query = IF(@exist <= 0, 'alter table n0x3c_twoquestions_table add column qu' 附近使用的正确语法

查询的字符串也是:

 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;

我也在我从MySQL Query works in PhpMyAdmin but not in JAVA Eclipse读到的java中尝试过这个:

                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。

最佳答案

您可以使用两个不同的查询。

首先是检查列是否存在:

String countQuery = "select count(*) from information_schema.columns WHERE table_schema = database() and COLUMN_NAME = 'question106' and table_name='n0x3c_twoquestions_table'";

然后如果countQuery返回0,就可以执行alterQuery:

String alterQuery = "alter table n0x3c_twoquestions_table add column question106 varchar(2048) NULL";

如果你不想使用两个不同的查询,你可以通过异常捕获来处理:

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
    }
}

我找到了错误代码 MySQL Server error codes .

关于java - 查询在 MySql workbench 中运行,但不在 Java eclipse 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25160103/

相关文章:

java - IMAP 列表扩展 JAVA

java - 在国际范围内部署 Android 应用程序

php - 使用 PHP、MySQL 和 HTML 的表单中存在错误

eclipse - 无法在 Eclipse 中安装更新或插件

eclipse - 在 Eclipse SWT/JFace 中具有自动完成功能的 TextCellEditor?

Java - 默认构造函数概念

java eclipse : failed to load nxt usb comm driver

mysql - 为每个数据库运行查询(mysql)

php - Codeigniter Ajax请求与mysql一样查询编码字符串为字符

Java Do While 语句在 Try Catch 语句之后无限循环