1

我目前正在用 Java 编写一个 mdb 迁移脚本。这只是在 MDB 文件中添加和修改一些列名。

我创建了新表,这不是问题,但我找不到更改列名称的查询

假设我有一个名为Employees的表,其列为“ID”、“EName”、“Active?” 现在我想更改列名“活动?” 到“活跃”(即不带问号。)

如何使用 JDBC java 来做到这一点。谢谢

4

2 回答 2

3

实现目标的一种方法是将以下 VBScript 代码另存为RenameColumn.vbs...

Option Explicit

Dim objArgs, dbPath, tblName, oldColName, newColName
Dim dbe  ' As DAO.DBEngine
Dim db   ' As DAO.Database
Dim fld  ' As DAO.Field

Set objArgs = WScript.Arguments
dbPath = objArgs(0)
tblName = objArgs(1)
oldColName = objArgs(2)
newColName = objArgs(3)

Set dbe = CreateObject("DAO.DBEngine.120")
Set db = dbe.OpenDatabase(dbPath)
Set fld = db.TableDefs(tblName).Fields(oldColName)
fld.Name = newColName
Set fld = Nothing
Set db = Nothing
Set dbe = Nothing

...然后您的 Java 程序可以使用如下代码调用它:

import java.io.*;

public class RenameColumns {

    public static void main(String[] args) {
        String dbPath = "C:\\__tmp\\Database1.accdb";
        String tblName = "Employees";
        String oldColName = "Active?";
        String newColName = "Active";

        String cmd = 
                "cscript C:\\__tmp\\RenameColumn.vbs"
                    + " \"" + dbPath + "\""
                    + " \"" + tblName + "\""
                    + " \"" + oldColName + "\""
                    + " \"" + newColName + "\"";
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            p.waitFor();
            BufferedReader rdr = 
                    new BufferedReader(new InputStreamReader(p.getErrorStream()));
            int errorLines = 0;
            String line = rdr.readLine();
            while (line != null) {
                errorLines++;
                System.out.println(line);  // display error line(s), if any
                line = rdr.readLine();
            }
            if (errorLines == 0) {
                System.out.println("The operation completed successfully.");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
于 2013-07-18T01:55:42.410 回答
0

似乎 MS A ccess 不允许重命名列(参考链接 12)。唯一的前进方向是,

  • 将数据备份到不同的表中
  • 删除列
  • 添加具有新名称的列
  • 从备份的表中填充表。
于 2013-07-16T06:57:53.717 回答