- 论坛徽章:
- 0
|
这个程序可以连接sybase和sql server 须自己下载这个两个JDBC的驱动,在此贴出来,和大家交流,希望大家多多指点。
这个程序的缺点就是当数据量比较大的时候数据比较慢,希望大家给点好的建议,谢谢
我的邮件地址是wangwenhong@9448.com.cn
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;
import java.applet.*;
class DataBackGUI extends Applet
{
TextField TexTable;//表名
TextField TexUserID;//登陆用户名
TextField TexPass;//登陆密码
TextField TexPath;//文件保存路径
Choice ServerType ;//服务器类型
Label TableName;//要备份的表名
Label Pass;
Label UserID;
Label FilePath;
Button ButConn;//连接数据库
Button ButBuck;
TextArea Message;
Connection myConn;
Statement myStmt;
ResultSet myResults;
String SQLText="";
//初始化界面
public void init()
{
setBackground(Color.lightGray);
setLayout(new GridBagLayout());
GridBagConstraints GBC = new GridBagConstraints();
ButConn =new Button("连接" ;
ButBuck =new Button("备份" ;
ButBuck.setEnabled(false);
TexTable =new TextField("",15);
TexUserID =new TextField("",15);
TexPass =new TextField("",15);
TexPath =new TextField("",15);
ServerType=new Choice();
ServerType.addItem("SQL Server" ;
ServerType.addItem("Sybase" ;
TexPass.setEchoChar('*');
TableName =new Label("表名:" ;
Pass =new Label("密码:" ;
UserID =new Label("用户名:" ;
FilePath =new Label("保存路径:" ;
Message =new TextArea(30,80);
Message.setEnabled(false);
GBC.gridwidth= GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(ServerType,GBC);
add(ServerType);
((GridBagLayout)getLayout()).setConstraints(UserID,GBC);
add(UserID);
((GridBagLayout)getLayout()).setConstraints(TexUserID,GBC);
add(TexUserID);
((GridBagLayout)getLayout()).setConstraints(Pass,GBC);
add(Pass);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(TexPass,GBC);
add(TexPass);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(TableName,GBC);
add(TableName);
((GridBagLayout)getLayout()).setConstraints(TableName,GBC);
add(TexTable);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(ButConn,GBC);
add(ButConn);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(FilePath,GBC);
add(FilePath);
((GridBagLayout)getLayout()).setConstraints(TexPath,GBC);
add(TexPath);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(ButBuck,GBC);
add(ButBuck);
GBC.gridwidth=GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
((GridBagLayout)getLayout()).setConstraints(Message,GBC);
add(Message);
}
//连接到数据库并执行查询语句
public boolean conntection()
{
try
{
if (ServerType.getSelectedIndex()==0)
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver" .newInstance();
myConn=DriverManager.getConnection("jdbc:microsoft:sqlserver://VITTY:1433;databasename=XGS_SCJY",TexUserID.getText(),TexPass.getText());
}
if (ServerType.getSelectedIndex()==1)
{
Class.forName("com.sybase.jdbc2.jdbc.SybDriver" .newInstance();
myConn = DriverManager.getConnection("jdbc:sybase:Tds:162.105.167.3:8000/JYGS_JXC_TEST?charset=eucgb&jconnect_version=0",TexUserID.getText(),TexPass.getText());
}
System.out.println(ServerType.getSelectedIndex());
myStmt = myConn.createStatement();
SQLText="select * from "+TexTable.getText();
myResults = myStmt.executeQuery(SQLText);
return true;
}
catch(SQLException es)
{
return false;
}
catch(Exception ee)
{
return false;
}
}
public boolean action(Event evt,Object obj)
{
if (evt.target == ButConn)
{
if (conntection())
{
ButBuck.setEnabled(true);
ButConn.setEnabled(false);
}
else
{
Message.append("连接数据库失败!");
Message.append("\n");
}
return true;
}
if (evt.target==ButBuck)
{
ButBuck.setEnabled(false);
Message.append("正在导出数据,请稍后.....");
Message.append("\n");
if (AppendtoFile())
{
ButConn.setEnabled(true);
Message.append("数据导出成功,保存在:"+TexPath.getText()+TexTable.getText()+".txt");
Message.append("\n");
}
else
{
Message.append("数据导出失败!");
Message.append("\n");
}
return true;
}
return super.action(evt,obj);
}
public boolean AppendtoFile()
{
try
{
int filelength;
int writelength;
String myFilePath=TexPath.getText();
String myFileName=TexTable.getText()+".txt";
String Values;
ResultSetMetaData MetaData=myResults.getMetaData();
int col=MetaData.getColumnCount();
while (myResults.next())
{
for (int i=1;i<=col ;i++ )
{
File f=new File(myFilePath,myFileName);
filelength=(int)f.length();
Values=myResults.getString(i);
if (Values==null)
{
Values="null";
}
writelength=Values.length();
BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
out.write(Values,0,writelength);
out.write(" ",0,3);
if (i==col)
{
out.newLine();
}
out.flush();
out.close();
}
}
}
catch(IOException e)
{
System.out.println(e);
return false;
}
catch(SQLException es)
{
System.out.println(es);
return false;
}
return true;
}
public static void main(String[] args)
{
Frame f=new Frame("数据备份程序");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
DataBackGUI back = new DataBackGUI();
back.init();
f.add(back);
f.pack();
f.setVisible(true);
}
} |
|