简体   繁体   English

使用Java动态创建MySQL表列

[英]Creating MySQL table columns dynamically using Java

I have an error while trying to alter a MySQL table by adding columns to it dynamically where the column names are values stored in a Java array variable. 尝试通过动态向其添加列来更改MySQL表时出现错误,其中列名是存储在Java数组变量中的值。

 try
{  
Class.forName("com.mysql.jdbc.Driver");        
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","123");  
Statement stmt=con.createStatement();        
stmt.executeUpdate("create table distable (patient integer)");  
PreparedStatement pst=con.prepareStatement("alter table distable add ? varchar(50)");  
for(i=1;i<=col;i++)
{
pst.setString (1,out[i]);
pst.executeUpdate();
}        
con.close();        
}
catch(Exception e)
{
 System.out.println(e);
}

Here con is the variable which holds the connection. con是保存连接的变量。 Using the connection I have created a table in MySQL and the name of the table is "distable". 使用该连接,我在MySQL中创建了一个表,该表的名称为“ distable”。 The table "distable" initially contains one integer column named Patient. 表“ distable”最初包含一个名为Patient的整数列。

I need to alter the table "distable" and add new columns to it in run time. 我需要更改表“ distable”并在运行时向其添加新列。 As I said the name of each column has to be the values stored in an array in Java. 正如我所说的,每列的名称必须是存储在Java数组中的值。 Hence I have used PreparedStatement, which contains a '?' 因此,我使用了PreparedStatement,其中包含一个“?” which is a substitute for column names. 可以替代列名。

The number of columns to be inserted at runtime is stored in the variable "col". 运行时要插入的列数存储在变量“ col”中。 I have used setString() to set the column name at run time. 我已经使用setString()在运行时设置列名。 The column names are stored in the array named "out". 列名存储在名为“ out”的数组中。 I have used executeUpdate() to execute the query. 我已经使用了executeUpdate()来执行查询。

When I run this code, the error i get is: 当我运行此代码时,我得到的错误是:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'GSM22449' varchar(50)' at line 1 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在第1行的'GSM22449'varchar (50)'附近使用

'GSM22449' is the first value stored in the array [ ie out[1]=GSM22449]. “ GSM22449”是存储在数组中的第一个值[ out [1] = GSM22449]。 Therefore it is actually fetched and substituted at the place of '?' 因此,实际上是在“?”的位置获取并替换了它。 in the query. 在查询中。 But the query is not executed. 但是查询没有执行。

Where does the error lies? 错误在哪里? Is there any problem with syntax? 语法有问题吗?

I think you do not need to specify "COLUMN" keyword while adding a column. 我认为您在添加列时无需指定“ COLUMN”关键字。 Here is the right syntax to add the column 这是添加列的正确语法

ALTER TABLE table_name
ADD column_name datatype

So your statement creation line should look like this, 因此,您的语句创建行应如下所示:

PreparedStatement pst=con.prepareStatement("alter table distable add ? varchar(50)"); 

Update : Just figured out that ? 更新 :刚发现吗? parameters works only for data, and does not work for the column names. 参数仅适用于数据,不适用于列名。 If at all you need to generate the column names this way, you may use string concatenation. 如果根本需要以这种方式生成列名,则可以使用字符串连接。 For example, 例如,

"alter table distable add " + COLUMN_NAME + " varchar(50)"

Make sure that the column names are not user inputs, otherwise it will invite XSS (cross site scripting). 确保列名不是用户输入,否则将邀请XSS(跨站点脚本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM