无法准备数据库查询

时间:2016-05-06 12:29:19

标签: java mysql database

我正在编写一个java函数,我正在尝试构建数据库查询,但字符串的元素没有被替换。

这是我的疑问:

BOOK 

Host: 
Database: AVIATION

Generation Time: May 12, 2015 at 07:32:00 AM

Generated by: phpMyAdmin 3.5.7 / MySQL 5.6.10

SQL query: SHOW columns FROM BOOK 

Rows: 4


Field Type Null Key Default Extra

timestamp varchar(15) NO NULL

concept varchar(50) NO  NULL 

page varchar(5) YES  NULL 

chapter varchar(20) NO  NULL 

section varchar(20) YES NULL

course varchar(10) NO NULL

book varchar(20) YES NULL

module varchar(2) YES NULL 

comments varchar(250) YES NULL

java函数

public String prepareQuery(String q) {
    String[] array = q.split("\n");
    String q2 = " ";

    q2 += "CREATE TABLE BOOK ( \n";

    for (int i = 10; i < array.length; i++) {
            q2 += array[i];
            q2 += ",\n";
    }

    q2 += "\n);";
    // Replacing double spaces to single space
    q2 = q2.replaceAll("  ", " ");
    // this part is not working as if i see output YES NULL is still there
    q2 = q2.replaceAll(" Yes NULL ,", ",");
   // changing NO to NULL and working
    q2 = q2.replaceAll("NO" , "NOT");

    return q2;

}

此功能正在替换&#34;是null&#34;。

之外的所有内容

1 个答案:

答案 0 :(得分:1)

如果我用这行替换你的代码,对我来说是

public String prepareQuery(String q) {
        String[] array = q.split("\n");
        String q2 = " ";

        q2 += "CREATE TABLE BOOK ( \n";

        for (int i = 10; i < array.length; i++) {
            q2 += array[i];
            if (i == array.length - 1)
                q2 += "\n";
            else
                q2 += ",\n";
        }

        q2 += "\n);";
        // Replacing double spaces to single space
        q2 = q2.replaceAll("  ", " ");
        // this part is not working as if i see output YES NULL is still there
        q2 = q2.replaceAll(" YES NULL,", ",");
        q2 = q2.replaceAll(" YES NULL ,", ",");
       // changing NO to NULL and working
        q2 = q2.replaceAll("NO" , "NOT");

        return q2;

    }