通过JSP在mysql表中创建表

通过JSP在mysql表中创建表

我正在创建一个代码,其中将创建一个 mysql 表,该表的名称将由用户提供。用户将输入单独的数据,代码将连接所有数据,并用“*”分隔所有数据,并使用输出的名称创建表。但它显示错误

OVS$5PKT Lenny$Dk Blue com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Blue (SlNo INTEGER, ProcessName VARCHAR(25), Water INTEGER, ChemicalName V...' at line 1

如果我在一个字段中输入单个数据而不将其连接起来,代码可以正常工作。但是,当用户在多个字段中输入数据并用分隔符连接时,它会抛出错误。

以下是代码 -

<%@ page import = "java.sql.*"%>
<%
    java.lang.String createDate = request.getParameter("createDate");
    java.lang.String origin = request.getParameter("origin");
    java.lang.String factoryName = request.getParameter("factoryName");
    java.lang.String buyerName = request.getParameter("buyerName");
    java.lang.String styleNumber = request.getParameter("styleNumber");
    java.lang.String poNumber = request.getParameter("poNumber");
    java.lang.String colorName = request.getParameter("colorName");
    java.lang.String washType = request.getParameter("washType");

    String conHelper = buyerName + "$" + styleNumber + "$" + colorName;

    out.println(conHelper);

    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_USER = "root";
    String DB_PASS = "";

    int originLength = origin.length();
    
    try{

        if(originLength == 7){

            String DB_URL = "jdbc:mysql://localhost:3306/erp-pre-cost(in-house)";
            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            Statement stmt = conn.createStatement();

            String createTable = "CREATE TABLE IF NOT EXISTS " + conHelper +
                       " (SlNo INTEGER, " + 
                       " ProcessName VARCHAR(25), " + 
                       " Water INTEGER, " + 
                       " ChemicalName VARCHAR(50), " + 
                       " gL FLOAT, " + 
                       " Doages INTEGER, " + 
                       " UOM ENUM('Kg', 'gm'), " +
                       " MachineRPM INTEGER, " +  
                       " pH FLOAT, " + 
                       " Time INTEGER, " + 
                       " Remarks VARCHAR(30))";

             stmt.executeUpdate(createTable);
             out.println("Success!");

        }else if(originLength == 11){

            String DB_URL = "jdbc:mysql://localhost:3306/erp-pre-cost(sub-contract)";
            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            Statement stmt = conn.createStatement();

            String createTable = "CREATE TABLE IF NOT EXISTS " + conHelper +
                       " (SlNo INTEGER, " + 
                       " ProcessName VARCHAR(25), " + 
                       " Water INTEGER, " + 
                       " ChemicalName VARCHAR(50), " + 
                       " gL FLOAT, " + 
                       " Doages INTEGER, " + 
                       " UOM ENUM('Kg', 'gm'), " +
                       " MachineRPM INTEGER, " +  
                       " pH FLOAT, " + 
                       " Time INTEGER, " + 
                       " Remarks VARCHAR(30))";

             stmt.executeUpdate(createTable);
             out.println("Success!");

        }else{
            out.println("Failed!");
        }

    }catch(Exception e){
        out.println(e);
    }

%> 

请帮忙!

答案1

如果表名中有空格,则应使用 ` 字符括住表名,例如

CREATE TABLE IF NOT EXISTS `OVS$5PKT Lenny$Dk Blue`
    (SlNo INTEGER,
    /*...*/
    Remarks VARCHAR(30));

相关内容