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);
}
}
|