一个Oracle数据库小程序

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 程序说明 这是一个从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);

	}

}		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值