import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
public class ExecSQLServerDBFiles {
/**
* @category ExecSQLServerDBFiles Tool
* @description 附加数据库(MSSQLSERVER)工具!
* @author brj
* @Creation date: 01-02-2008
* @Email
*/
private String url;
private String serverName;
private String portNumber;
private String userName;
// private String databaseName;
private String password;
private String forName;
private String dataBaseName_Data = null;
private String dataBaseName_Log = null;
private Connection con = null;
private PreparedStatement ps = null;
public static final String JDBC_EXEC_FILE_PATH = "JDBC_EXEC";
private static final String DBFiles_SOURCE_PATH = "DBFiles\\";
private static final String DBFiles_SOURCE_PATH_BAK = "DBFiles_bak\\";
private StringBuffer filePath = new StringBuffer();
private String filePathName = "";
private String dbFilesPath = "D:\\Exec_DBFiles_MSSQLSrver\\";// 默认路径
public ExecSQLServerDBFiles(String dbFilesPath) {
this.dbFilesPath = dbFilesPath;
}
public void setDbFilesPath(String dbFilesPath) {
this.dbFilesPath = dbFilesPath;
}
public ExecSQLServerDBFiles() {
File file = new File(DBFiles_SOURCE_PATH);
filePath.append(file.getAbsolutePath()).append("\\");
filePathName = filePath.toString();
}
public void readProperties() { // 读取 JDBC.properties 配置文件
JOptionPane.showMessageDialog(null, "附加数据库(MSSQLSERVER)工具!\n\n"
+ "作者 : Brj 20080123 \n更多工具请联系 : ",
"ExecSQLServerDBFiles Tool", JOptionPane.INFORMATION_MESSAGE);
ResourceBundle resource = ResourceBundle.getBundle(JDBC_EXEC_FILE_PATH);
System.out
.print("\n--------正在读取 JDBC_EXEC.properties 配置文件 ,请稍等......\n>>>>>>>正在访问数据库,请稍等 ......");
url = resource.getString("url");
forName = resource.getString("forName");
serverName = resource.getString("serverName");
portNumber = resource.getString("portNumber");
// databaseName = resource.getString("databaseName");
userName = resource.getString("userName");
password = resource.getString("password");
// dataBaseName_Data = resource.getString("dataBaseName_Data");
// dataBaseName_Log = resource.getString("dataBaseName_Log");
}
/*
* private String getConnectionUrl() { return url + serverName + ":" +
* portNumber + ";databaseName=" + databaseName + ";"; }
*/
public Connection getConnectionNoDatabaseName() {
try {
Class.forName(forName);
con = java.sql.DriverManager.getConnection(url + serverName + ":"
+ portNumber + ";", userName, password);
if (con != null)
System.out.println(" 访问 SQL Server 成功 !");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : "
+ e.getMessage());
JOptionPane.showMessageDialog(null,
"SQLException: 数据库连接失败 !\n不能加载相关数据,请检查数据库服务器 !\n"
+ e.getMessage(), "数据库连接失败",
JOptionPane.ERROR_MESSAGE);
this.close();
System.exit(0);// 终止当前正在运行的 Java 虚拟机
}
return con;
}
private String getdbname(String data, String log) {
if (data == null || log == null) {
JOptionPane.showMessageDialog(null,
"文件不存在或文件格式错误无法附加数据库文件 , 请检查数据库文件 !",
"ExecSQLServerDBFiles Tool", JOptionPane.ERROR_MESSAGE);
this.close();
System.exit(0);
} else {
String dbname = "";
dbname = data.substring(0, data.lastIndexOf("_"));
String str = log.substring(0, data.lastIndexOf("_"));
if (!dbname.equals(str)) {
JOptionPane.showMessageDialog(null,
"文件格式错误,无法附加数据库文件 , 请检查数据库文件 !",
"ExecSQLServerDBFiles Tool", JOptionPane.ERROR_MESSAGE);
this.close();
System.exit(0);
return null;
} else {
return dbname;
}
}
return null;
}
public void doEXEC() {
JOptionPane.showMessageDialog(null, "正在附加数据库 ,请稍等 。。。",
"ExecSQLServerDBFiles Tool", JOptionPane.INFORMATION_MESSAGE);
String dbName = getdbname(dataBaseName_Data, dataBaseName_Log);
StringBuffer filePathName = new StringBuffer();
filePathName.append(dbFilesPath).append(dbName).append("_db").append(
"\\");
String sql = "EXEC sp_attach_db @dbname = N'" + dbName
+ "',@filename1 = N'" + filePathName + dataBaseName_Data
+ "',@filename2 = N'" + filePathName + dataBaseName_Log + "'";
try {
ps = con.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "附加数据库 " + dbName
+ " 成功 ,\n 请查看数据库 !", "ExecSQLServerDBFiles Tool",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("附加数据库 " + dbName + " 成功 ,\n 请查看数据库 !");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "无法附加数据库文件 ,\n"
+ e.getMessage() + "请检查数据库文件 !",
"ExecSQLServerDBFiles Tool", JOptionPane.ERROR_MESSAGE);
this.close();
}
}
public void getFileNames() {
File[] file = (new File(filePathName)).listFiles();
int mark = 0;
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
String str = file[i].getName();
String fileName = str.substring(str.lastIndexOf("_"));
if (fileName.equalsIgnoreCase("_data.mdf")) {
dataBaseName_Data = file[i].getName();
mark++;
}
if (fileName.equalsIgnoreCase("_log.ldf")) {
dataBaseName_Log = file[i].getName();
}
if (mark > 1) {
JOptionPane.showMessageDialog(null,
"只能上传一组数据库文件 ,请检查数据库文件 !",
"ExecSQLServerDBFiles Tool",
JOptionPane.INFORMATION_MESSAGE);
this.close();
}
} else {
JOptionPane.showMessageDialog(null,
"没有找到数据库文件或文件格式错误 ,请检查数据库文件 !",
"ExecSQLServerDBFiles Tool",
JOptionPane.INFORMATION_MESSAGE);
this.close();
}
}
System.out.println("dataBaseName_Data : " + dataBaseName_Data
+ "\ndataBaseName_Log : " + dataBaseName_Log);
String childFilePathName = this.getdbname(dataBaseName_Data,
dataBaseName_Log);
this.copyFile(DBFiles_SOURCE_PATH_BAK, childFilePathName, filePathName);
this.copyFile(dbFilesPath, childFilePathName, filePathName);
}
public void copyFile(String toFilePathName, String childFilePathName,
String fromFilePathName) {
System.out.println("f :" + toFilePathName + " " + childFilePathName
+ " " + fromFilePathName);
StringBuffer fileName = new StringBuffer();
fileName.append(toFilePathName).append(childFilePathName).append("_db")
.append("\\");
System.out.println(" L ;" + fileName.toString());
File fi = new File(fileName.toString());
if (fi.exists()) {
if (JOptionPane.showConfirmDialog(null, fileName.toString()
+ " 文件夹已存在,是否覆盖 ?", "ExecSQLServerDBFiles Tool",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
fi.mkdirs();
} else {
JOptionPane.showMessageDialog(null,
"ExecSQLServerDBFiles Tool\n 谢谢您的使用 !",
"ExecSQLServerDBFiles Tool",
JOptionPane.INFORMATION_MESSAGE);
this.close();
}
} else {
fi.mkdirs();
}
File[] file = (new File(fromFilePathName)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
FileInputStream input;
FileOutputStream output;
byte[] b = new byte[1024 * 5];
int len;
try {
// 为每一个源File流对象打开一个FileInputStream流对象
input = new FileInputStream(file[i]);
output = new FileOutputStream(fileName.toString()
+ file[i].getName());
// 拷贝文件
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
System.out.println("file folder copy failed!");
}
}
}
}
public void close() {
try {
if (con != null) {
con.close();
}
if (ps != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
System.exit(0);
}
public static void main(String[] args) {
ExecSQLServerDBFiles exec = new ExecSQLServerDBFiles();
exec.readProperties();
exec.getConnectionNoDatabaseName();
exec.getFileNames();
exec.doEXEC();
exec.close();
}
}