真诚求教!-高手看看这个java恢复SQL数据库的一个小小问题(解决给200分)运行时备份可以成功,可还原不行:出现以下提示:
Error:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServ
er]因为数据库正在使用,所以未能获得对数据库的排它访问权。
我在网上查了一晚也没查出来结果,这个问题很多人遇到,可没有解决的在网上,高手指点啊,我知道是当前的数据库在用,或是因为是有一个相同的数据库,所以不能恢复,可怎么样在代码里解决啊???高手指教,不胜感激!!!! import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.FileFilter;
import java.sql.*;public class DataBackup implements ActionListener
{ JFrame f = null;
JLabel label = null;
JTextArea textarea = null;
JFileChooser fileChooser = null; public DataBackup()
{ f = new JFrame("FileChooser Example");
Container contentPane = f.getContentPane();
textarea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(350,300));
JPanel panel = new JPanel();
JButton b1 = new JButton("恢复数据");
b1.addActionListener(this);
JButton b2 = new JButton("备份数据");
b2.addActionListener(this);
panel.add(b1);
panel.add(b2); label = new JLabel(" ",JLabel.CENTER); contentPane.add(label,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(panel,BorderLayout.SOUTH); f.pack();
f.setVisible(true); f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void actionPerformed(ActionEvent e)
{
//windows效果
try
{
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e1)
{
System.out.println("Look and Feel Exception");
System.exit(0);
}
File file = null;
int result;
fileChooser = new JFileChooser("d:\\");
fileChooser.addChoosableFileFilter(new JAVAFileFilter("bak")); //恢复数据库操作
if (e.getActionCommand().equals("恢复数据"))
{
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(f); textarea.setText(""); if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
}
else if(result == JFileChooser.CANCEL_OPTION)
{
}
/***************执行事件*******************/
//在这里写恢复数据库事件 try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException error)//驱动加载失败
{
System.err.println("驱动加载失败");
}
//连接到数据库
Connection conStudent; try
{
//conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=pubs","sa","");
conStudent = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student1 ","sa",""); Statement cmdStudent = conStudent.createStatement();
cmdStudent.execute("Restore Database student1 from Disk='"+file.getPath()+"'");
JOptionPane.showMessageDialog(null, "祝贺你,还原成功!", "提示",
JOptionPane.WARNING_MESSAGE);
if(conStudent != null)
{//关闭数据库连接
cmdStudent.close();
conStudent.close();
}
}
catch(SQLException sqlerr)
{
System.out.println("Error:"+sqlerr.toString());
}
} //备份数据库操作
if (e.getActionCommand().equals("备份数据"))
{
result = fileChooser.showSaveDialog(f);
file = null;
String fileName; if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
String fileName1 = file.getName();
String filePath = file.getPath();
int index = fileName1.lastIndexOf('.');
if (index > 0)
{
String extension = fileName1.substring(index+1).toLowerCase(); if(!extension.equals("bak"))
{
filePath = filePath + ".bak";
}
}
if (index < 0)
{
filePath = filePath + ".bak";
}
/***************执行事件*******************/
//在这里写备份数据库事件
//装入JDBC驱动
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException error)//驱动加载失败
{
System.err.println("驱动加载失败");
}
//连接到数据库
Connection conStudent; try
{
//conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=pubs","sa","");
conStudent = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student1 ","sa","");
Statement cmdStudent = conStudent.createStatement(); cmdStudent.execute("Backup Database student1 To Disk='"+filePath+"'");
JOptionPane.showMessageDialog(null, "备份成功!", "提示",
JOptionPane.WARNING_MESSAGE);
if(conStudent != null)
{//关闭数据库连接
cmdStudent.close();
conStudent.close();
}
}
catch(SQLException sqlerr)
{
System.out.println("Error:"+sqlerr.toString());
}
}
else if(result == JFileChooser.CANCEL_OPTION)
{
} }
}//过滤文件
class JAVAFileFilter extends FileFilter
{ String ext; public JAVAFileFilter(String ext)
{
this.ext = ext;
} public boolean accept(File file)
{
if (file.isDirectory())
return true; String fileName = file.getName();
int index = fileName.lastIndexOf('.'); if (index > 0 && index < fileName.length()-1) {
String extension = fileName.substring(index+1).toLowerCase();
if (extension.equals(ext))
return true;
}
return false;
} public String getDescription(){
if (ext.equals("bak"))
return "Data Bakeup File (*.bak)";
return "";
}
}
public static void main(String[] args)
{
// new DataBackup();
new DataBackup();
}
}
有人说这个错误原因是:要还原的数据库正在使用中。
我加了
Runtime rt = Runtime.getRuntime();
String cmd="net stop mssqlserver";
try{
Process process = rt.exec(cmd);
process.waitfor( ); }catch(Exception e){
e.printStackTrace();
}
还不行,这到底为什么呢??高手啊,指点一下我吧,谢谢了,明天就答辩了
哪个高手可以解决我一给200分!!谢谢
Error:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServ
er]因为数据库正在使用,所以未能获得对数据库的排它访问权。
我在网上查了一晚也没查出来结果,这个问题很多人遇到,可没有解决的在网上,高手指点啊,我知道是当前的数据库在用,或是因为是有一个相同的数据库,所以不能恢复,可怎么样在代码里解决啊???高手指教,不胜感激!!!! import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.FileFilter;
import java.sql.*;public class DataBackup implements ActionListener
{ JFrame f = null;
JLabel label = null;
JTextArea textarea = null;
JFileChooser fileChooser = null; public DataBackup()
{ f = new JFrame("FileChooser Example");
Container contentPane = f.getContentPane();
textarea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(350,300));
JPanel panel = new JPanel();
JButton b1 = new JButton("恢复数据");
b1.addActionListener(this);
JButton b2 = new JButton("备份数据");
b2.addActionListener(this);
panel.add(b1);
panel.add(b2); label = new JLabel(" ",JLabel.CENTER); contentPane.add(label,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(panel,BorderLayout.SOUTH); f.pack();
f.setVisible(true); f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void actionPerformed(ActionEvent e)
{
//windows效果
try
{
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e1)
{
System.out.println("Look and Feel Exception");
System.exit(0);
}
File file = null;
int result;
fileChooser = new JFileChooser("d:\\");
fileChooser.addChoosableFileFilter(new JAVAFileFilter("bak")); //恢复数据库操作
if (e.getActionCommand().equals("恢复数据"))
{
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(f); textarea.setText(""); if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
}
else if(result == JFileChooser.CANCEL_OPTION)
{
}
/***************执行事件*******************/
//在这里写恢复数据库事件 try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException error)//驱动加载失败
{
System.err.println("驱动加载失败");
}
//连接到数据库
Connection conStudent; try
{
//conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=pubs","sa","");
conStudent = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student1 ","sa",""); Statement cmdStudent = conStudent.createStatement();
cmdStudent.execute("Restore Database student1 from Disk='"+file.getPath()+"'");
JOptionPane.showMessageDialog(null, "祝贺你,还原成功!", "提示",
JOptionPane.WARNING_MESSAGE);
if(conStudent != null)
{//关闭数据库连接
cmdStudent.close();
conStudent.close();
}
}
catch(SQLException sqlerr)
{
System.out.println("Error:"+sqlerr.toString());
}
} //备份数据库操作
if (e.getActionCommand().equals("备份数据"))
{
result = fileChooser.showSaveDialog(f);
file = null;
String fileName; if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
String fileName1 = file.getName();
String filePath = file.getPath();
int index = fileName1.lastIndexOf('.');
if (index > 0)
{
String extension = fileName1.substring(index+1).toLowerCase(); if(!extension.equals("bak"))
{
filePath = filePath + ".bak";
}
}
if (index < 0)
{
filePath = filePath + ".bak";
}
/***************执行事件*******************/
//在这里写备份数据库事件
//装入JDBC驱动
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException error)//驱动加载失败
{
System.err.println("驱动加载失败");
}
//连接到数据库
Connection conStudent; try
{
//conStudent = DriverManager.getConnection("jdbc:sqlserver://home:1433;DatabaseName=pubs","sa","");
conStudent = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student1 ","sa","");
Statement cmdStudent = conStudent.createStatement(); cmdStudent.execute("Backup Database student1 To Disk='"+filePath+"'");
JOptionPane.showMessageDialog(null, "备份成功!", "提示",
JOptionPane.WARNING_MESSAGE);
if(conStudent != null)
{//关闭数据库连接
cmdStudent.close();
conStudent.close();
}
}
catch(SQLException sqlerr)
{
System.out.println("Error:"+sqlerr.toString());
}
}
else if(result == JFileChooser.CANCEL_OPTION)
{
} }
}//过滤文件
class JAVAFileFilter extends FileFilter
{ String ext; public JAVAFileFilter(String ext)
{
this.ext = ext;
} public boolean accept(File file)
{
if (file.isDirectory())
return true; String fileName = file.getName();
int index = fileName.lastIndexOf('.'); if (index > 0 && index < fileName.length()-1) {
String extension = fileName.substring(index+1).toLowerCase();
if (extension.equals(ext))
return true;
}
return false;
} public String getDescription(){
if (ext.equals("bak"))
return "Data Bakeup File (*.bak)";
return "";
}
}
public static void main(String[] args)
{
// new DataBackup();
new DataBackup();
}
}
有人说这个错误原因是:要还原的数据库正在使用中。
我加了
Runtime rt = Runtime.getRuntime();
String cmd="net stop mssqlserver";
try{
Process process = rt.exec(cmd);
process.waitfor( ); }catch(Exception e){
e.printStackTrace();
}
还不行,这到底为什么呢??高手啊,指点一下我吧,谢谢了,明天就答辩了
哪个高手可以解决我一给200分!!谢谢
楼主【haoyanbinok】截止到2008-06-26 09:31:50的历史汇总数据(不包括此帖):
发帖数:22 发帖分:400
结贴数:13 结贴分:220
未结数:9 未结分:180
结贴率:59.09 % 结分率:55.00 %
楼主加油
http://wtnhwbb.javaeye.com/blog/228253