我目前正在用 Java 编写一个 mdb 迁移脚本。这只是在 MDB 文件中添加和修改一些列名。
我创建了新表,这不是问题,但我找不到更改列名称的查询
假设我有一个名为Employees的表,其列为“ID”、“EName”、“Active?” 现在我想更改列名“活动?” 到“活跃”(即不带问号。)
如何使用 JDBC java 来做到这一点。谢谢
实现目标的一种方法是将以下 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();
}
}
}