我拥有的是一个需要清理的旧 Access 数据库。我首先尝试从旧数据库中获取字段并将它们插入到新数据库中。我能够连接到两个数据库,但没有记录被插入到新的 DB Employee 表中。不确定我的 SQL 是否有问题。有人可以帮忙吗?
运行.java:
package javaapplication1;
/**
*
* @author Tyler Bradshaw
*/
public class Run {
public static void main(String[] args) {
Convert.ConnectToOld();
Convert.ConnectToNew();
Convert.EmployeeTransfer();
}
}
转换.java:
package javaapplication1;
/**
*
* @author Tyler Bradshaw
*/
import javax.swing.JOptionPane;
public class Convert {
static DbWork oldDatabase = new DbWork();
static DbWork newDatabase = new DbWork();
public static void ConnectToOld(){
oldDatabase.Constructor("oldDatabase");
if (oldDatabase.isConnected())
System.out.println("Connected to old database.");
}
public static void ConnectToNew(){
newDatabase.Constructor("newDatabase");
if (newDatabase.isConnected())
System.out.println("Connected to new database.");
}
public static void EmployeeTransfer(){
String readOld = "SELECT * FROM EmployeeTable";
try{
if (!oldDatabase.isConnected()){
ConnectToOld();
}
if (!newDatabase.isConnected()){
ConnectToNew();
}
oldDatabase.processQuery(readOld);
while(oldDatabase.nextRecord()){
String empID = oldDatabase.getField("ID");
String fName = oldDatabase.getField("First Name");
String lName = oldDatabase.getField("Last Name");
String mI = oldDatabase.getField("MI");
String homeAdd = oldDatabase.getField("Home Address");
String state = oldDatabase.getField("State");
String zip = oldDatabase.getField("Zip");
String dob = oldDatabase.getField("Date of Birth");
String hireDate = oldDatabase.getField("Hire Date");
String termDate = oldDatabase.getField("Termination Date");
String licDate = oldDatabase.getField("License Date");
/* validation */
String loadNew = "INSERT INTO EmployeeTable (EmployeeID, FirstName,"
+ "LastName, MI, HomeAddress, Sta , ZIP, DateOfBirth, HireDate,"
+ "TerminationDate, LicenseDate) VALUES ('empID', '" + fName + "', '" + lName + "', '" + mI + "',"
+ "'" + homeAdd + "', '" + state + "', '" + zip + "', '" + dob + "', '" + hireDate + "', '" + termDate + "', '" + licDate + "')";
newDatabase.processUpdate(loadNew);
}
System.out.println("Done?");
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error");
}
}
}
DbWork.java:
package javaapplication1;
import java.sql.*;
public class DbWork {
private Connection connection;
private ResultSet rs;
private String errorMessage;
public void Constructor(String dsn) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String URL = "jdbc:odbc:"+dsn;
connection = DriverManager.getConnection(URL,"","");
} catch (Exception e) {
errorMessage = "Cannot Connect to Database.";
}
}
public boolean isConnected() {
try {
if (connection == null || connection.isClosed()) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
public boolean processQuery(String selectQuery) {
try {
Statement stmt = connection.createStatement();
rs = stmt.executeQuery(selectQuery);
return true;
}
catch (Exception l) {
errorMessage = "Result Set for query failed.";
rs = null;
return false;
}
}
public boolean nextRecord() {
try {
if (rs.next()) {
return true;
} else {
errorMessage = "End of File";
return false;
}
}
catch (Exception e) {
errorMessage = "Exception occured when moving to next record.";
return false;
}
}
public String getField(int fieldNum) {
try {
int columnIndex = fieldNum;
return rs.getString(columnIndex);
}
catch (Exception e) {
errorMessage = "Exception occured when returning field.";
return null;
}
}
public String getField(String fieldName) {
try {
String columnLabel = fieldName;
return rs.getString(columnLabel);
}
catch (Exception e) {
errorMessage = "Exception occured when returning field.";
return null;
}
}
public void close() {
connection = null;
}
public String getErrorMessage() {
return errorMessage;
}
public boolean processUpdate(String statement) {
String sql = statement;
try {
Statement stmt = connection.createStatement();
int nbr = stmt.executeUpdate(sql);
return true;
}
catch (Exception e) {
errorMessage = "Database update failed.";
rs = null;
return false;
}
}
}