我目前正在用 Java 编写 mdb 迁移脚本。这是在 MDB 文件中添加和修改一些列名称。
我创建了新表,这不是问题,但我找不到更改列名的查询
假设我有一个名为 Employees 的表,其列为“ID”、“EName”、“Active”?现在我想更改列名称“Activity ?”到“Activity ”(即没有问号。)
如何使用 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();
}
}
}
关于java - 使用 JDBC java 更改 MDB 文件中的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669720/