Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1531542
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 20:28:18

一起学习
程序说明 这是一个从oracle数据库中取函数、存储过程、包的源代码的小程序。 程序采用多线程处理,并且考虑到程序的通用性,连接数据库采用JDBC-ODBC网桥。因此, 在运行程序之前,必须先建立一个ODBC数据源(DSN),该DSN指向一个oracle数据库实例。 程序运行时,先在界面上输入如下必要信息: 1、数据库用户名称(user); 2、数据库用户密码(password); 3、数据源名(dsn); 4、函数、存储过程或包的名称。 然后点Get Code按钮显示源代码、点Save Code按钮把源代码存入文件。 源码如下:


import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.io.*;



public class OraTools

{

	public static void main(String[] args)

	{

		MainWnd mainwnd=new MainWnd();

	}

}



class MainWnd extends JFrame implements ActionListener

{

	JTabbedPane tabpane;

	JTextField proname;

	JTextArea txtarea;

	JButton buttonGetCode;

	JTextField txtuser;

	JPasswordField txtpwd;

	JTextField txtdsn;

	JButton buttonSaveCode;

	Thread threadgetcode;

	Thread threadsavecode;

	

	public MainWnd()

	{

		super("OraTools");

		addWindowListener(new MainWndListener());

		tabpane=new JTabbedPane(JTabbedPane.BOTTOM);

		JPanel getcodepanel=new JPanel(new BorderLayout());

		JPanel buttonspanel=new JPanel();

		proname=new JTextField(20);

		buttonGetCode=new JButton("Get Code");

		buttonSaveCode=new JButton("Save Code");

		buttonGetCode.addActionListener(this);

		buttonSaveCode.addActionListener(this);

		buttonspanel.add(proname);

		buttonspanel.add(buttonGetCode);

		buttonspanel.add(buttonSaveCode);

		JPanel buttonspanel1=new JPanel();

		JLabel labeluser=new JLabel("User:");

		txtuser=new JTextField(10);

		JLabel labelpwd=new JLabel("Password:");

		txtpwd=new JPasswordField(10);

		JLabel labeldsn=new JLabel("dsn:");

		txtdsn=new JTextField(5);

		buttonspanel1.add(labeluser);

		buttonspanel1.add(txtuser);

		buttonspanel1.add(labelpwd);

		buttonspanel1.add(txtpwd);

		buttonspanel1.add(labeldsn);

		buttonspanel1.add(txtdsn);

		getcodepanel.add(buttonspanel1,BorderLayout.NORTH);

		getcodepanel.add(buttonspanel,BorderLayout.SOUTH);

		txtarea=new JTextArea();

		JScrollPane scrollpane=new JScrollPane(txtarea);

		getcodepanel.add(scrollpane,BorderLayout.CENTER);

		tabpane.addTab("Get Code",null,getcodepanel,null);

		tabpane.addChangeListener(new TabbedPaneListener());

		GridLayout gridlayout=new GridLayout(1,1);

		getContentPane().setLayout(gridlayout);

		getContentPane().add(tabpane);

		setSize(640,480);

		show();

	}

	

	public void actionPerformed(ActionEvent ae)

	{

		JButton button=(JButton)(ae.getSource());

		if(button==buttonGetCode)

		{

			if(GetCodeThread.isRunning==false)

			{

				GetCodeThread.isRunning=true;

				GetCodeThread t=new GetCodeThread(this);

				threadgetcode=new Thread(t);

				t.proname=proname;

				t.txtuser=txtuser;

				t.txtpwd=txtpwd;

				t.txtdsn=txtdsn;

				t.txtarea=txtarea;

				threadgetcode.start();

			}

		}

		else if(button==buttonSaveCode)

		{

			if(SaveCodeThread.isRunning==false)

			{

				SaveCodeThread.isRunning=true;

				SaveCodeThread t=new SaveCodeThread(this);

				threadsavecode=new Thread(t);

				t.proname=proname;

				t.txtuser=txtuser;

				t.txtpwd=txtpwd;

				t.txtdsn=txtdsn;

				threadsavecode.start();

			}

		}

	}

}

	

class TabbedPaneListener implements ChangeListener

{

	public void stateChanged(ChangeEvent ce)

	{

	}

}



class GetCodeThread implements Runnable

{

	public static boolean isRunning=false;

	public JTextField proname;

	public JTextField txtuser;

	public JPasswordField txtpwd;

	public JTextField txtdsn;

	public JTextArea txtarea;

	public MainWnd mainwnd;

	

	public GetCodeThread(MainWnd wnd)

	{

		mainwnd=wnd;

	}

	

	void ShowCode()

	{

		txtarea.setText("");

		String strpwd=new String(txtpwd.getPassword());

		if(proname.getText().equals("")||txtuser.getText().equals("")||strpwd.equals("")||txtdsn.getText().equals(""))

		{

			JOptionPane.showMessageDialog(mainwnd,"Please enter necessary data!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

		try

		{

			System.setProperty("jdbc.drivers","sun.jdbc.odbc.JdbcOdbcDriver");

			Connection con=DriverManager.getConnection("jdbc:odbc:" txtdsn.getText(),txtuser.getText(),strpwd);

			Statement s=con.createStatement();

			String strsql="select text from user_source where name='" proname.getText().toUpperCase() "'";

			ResultSet rs=s.executeQuery(strsql);

			while(rs.next())

			{

				String str=rs.getString("text");

			//	str=str "\15" "\12";

				txtarea.append(str);

			}

			rs.close();

			con.close();

		}

		catch(SQLException e)

		{

			JOptionPane.showMessageDialog(mainwnd,"Database exception found!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

	}

	

	public void run()

	{

		ShowCode();

		JOptionPane.showMessageDialog(mainwnd,"Operation Complete!","Note",JOptionPane.INFORMATION_MESSAGE);

		isRunning=false;

	}

}



class SaveCodeThread implements Runnable

{

	public static boolean isRunning=false;

	public JTextField proname;

	public JTextField txtuser;

	public JPasswordField txtpwd;

	public JTextField txtdsn;

	public MainWnd mainwnd;

	

	public SaveCodeThread(MainWnd wnd)

	{

		mainwnd=wnd;

	}

	

	void SaveCode(String filename)

	{

		String strpwd=new String(txtpwd.getPassword());

		if(proname.getText().equals("")||txtuser.getText().equals("")||strpwd.equals("")||txtdsn.getText().equals(""))

		{

			JOptionPane.showMessageDialog(mainwnd,"Please enter necessary data!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

		OutputStreamWriter fo;

		try

		{

			fo=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(filename)));

		}

		catch(IOException e)

		{

			JOptionPane.showMessageDialog(mainwnd,"Can't open file!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

		try

		{

			System.setProperty("jdbc.drivers","sun.jdbc.odbc.JdbcOdbcDriver");

			Connection con=DriverManager.getConnection("jdbc:odbc:" txtdsn.getText(),txtuser.getText(),strpwd);

			Statement s=con.createStatement();

			String strsql="select text from user_source where name='" proname.getText().toUpperCase() "'";

			ResultSet rs=s.executeQuery(strsql);

			while(rs.next())

			{

				String str=rs.getString("text");

				str=str "\15" "\12";

				fo.write(str,0,str.length());

			}

			fo.close();

			rs.close();

			con.close();

		}

		catch(SQLException e)

		{

			JOptionPane.showMessageDialog(mainwnd,"Database exception found!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

		catch(IOException e)

		{

			JOptionPane.showMessageDialog(mainwnd,"File exception found!","Note",JOptionPane.INFORMATION_MESSAGE);

			return;

		}

	}

	

	public void run()

	{

		JFileChooser filechooser=new JFileChooser();

		int selected=filechooser.showSaveDialog(mainwnd);

		File selectedfile;//=new File("Untitled.sql");

	//	filechooser.setSelectedFile(selectedfile);

		if(selected==JFileChooser.APPROVE_OPTION) 

		{

		       	selectedfile=filechooser.getSelectedFile();

		       	SaveCode(selectedfile.getPath());

		       	JOptionPane.showMessageDialog(mainwnd,"Operation Complete!","Note",JOptionPane.INFORMATION_MESSAGE);

		}

		isRunning=false;

	}

}



class MainWndListener extends WindowAdapter

{

	public void windowClosing(WindowEvent we)

	{

	//	if(GetCodeThread.isRunning==true||SaveCodeThread.isRunning==true)

	//	{

	//		JOptionPane.showMessageDialog(null,"Operation not completed!","Note",JOptionPane.INFORMATION_MESSAGE);

	//		return;

	//	}

		System.exit(0);

	}

}		
下载本文示例代码


一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序一个Oracle数据库小程序
阅读(282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~