运行时备份可以成功,可还原不行:出现以下提示:
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();
}
}
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();
}
}
解决方案 »
- Full thread dump
- 这个JOptionPane怎么不用实例化就可以使用他的构造方法?
- 初学struts的小问题,JSP中的代码如何用struts 分离开。。。。 解决马上给分。
- javax.mail.AuthenticationFailedException:EOF on socket
- 高分相送!!!Swing中如何才能一个JPanel面板容器绑定在一个滚动条组件上,使得JPanel里的内容可以在窗口改变大小的时候能出现滚动条
- 为什么编译通不过
- 请问这是什么错误?
- 请高手推荐1-3本学习 JAVA的好书。常用VC,想学一下JAVA.
- ********关于Runtime的问题*********
- 有什么网站有提供Java编程的例子的啊!我是一个初学者,最好是由浅入深!
- 这个程序可以关闭,怎么设置背景颜色???
- 用JAVA 验证邮箱格式是否合法
楼主【haoyanbinok】截止到2008-06-23 21:07:00的历史汇总数据(不包括此帖):
发帖数:19 发帖分:340
结贴数:9 结贴分:140
未结数:10 未结分:200
结贴率:47.37 % 结分率:41.18 %
楼主该结一些帖子了
包括你自己的Tomcat连接也不行。 所以还原一般都是脱机做的。你的帖子是不是该结一些了???