模仿Oracle SQL Plus Worksheet,oracleworksheet,模仿Oracle SQL
分享于 点击 8754 次 点评:285
模仿Oracle SQL Plus Worksheet,oracleworksheet,模仿Oracle SQL
模仿Oracle SQL Plus Worksheet
Conn.java
import java.sql.*;public class Conn { Connection con = null; Statement stmt = null; LoginBean log = null; Conn(LoginBean log) { this.log = log; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:" + log.getServer(), log.getUsername(), log.getPassword()); stmt = con.createStatement(); System.out.println("connect successful!"); System.out.println(log.getUsername()); } catch (Exception ex) { System.out.println(ex.getMessage()); } }}
FileFliter.java
import javax.swing.filechooser.*;import java.io.File;public class FileFliter extends FileFilter { public String getDescription() { return "*.sql"; } public boolean accept(File file) { String name = file.getName(); return name.toLowerCase().endsWith(".sql"); }}
LoginBean.java
public class LoginBean { private String username; private String password; private String server; private String shenfen; public String getUsername() { return username; } public String getPassword() { return password; } public String getServer() { return server; } public String getShenfen() { return shenfen; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setServer(String server) { this.server = server; } public void setShenfen(String shenfen) { this.shenfen = shenfen; }}
MainFrame.java
import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.util.Date;import java.sql.*;public class MainFrame extends JFrame implements ActionListener { private class Create { String sql = null; Conn conn = null; String result = null; Create(String sql, Conn conn) { this.sql = sql; this.conn = conn; } public String createMethod() { if (sql.substring(7, 12).equals("table")) { try { conn.stmt.executeUpdate(sql); result = "\n" + "表已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(7, 11).toLowerCase().compareTo("user") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "用户已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(7, 11).toLowerCase().compareTo("view") == 0 || sql.substring(7, 22).toLowerCase() .compareTo("or replace view") == 0 || sql.substring(7, 23).toLowerCase() .compareTo("force view") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "视图已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(7, 14).toLowerCase().compareTo("synonym") == 0 || sql.substring(7, 25).toLowerCase() .compareTo("or replace synonym") == 0 || sql.substring(7, 32).toLowerCase() .compareTo("or replace public synonym") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "同义词已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(7, 15).toLowerCase().compareTo("sequence") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "序列已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(7, 12).toLowerCase().compareTo("index") == 0 || sql.substring(7, 19).toLowerCase() .compareTo("unique index") == 0 || sql.substring(7, 19).toLowerCase() .compareTo("bitmap index") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "索引已创建"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } return result; } } private class Drop { String sql = null; Conn conn = null; String result = null; Drop(String sql, Conn conn) { this.sql = sql; this.conn = conn; } public String dropMethod() { if (sql.substring(5, 10).equals("table")) { try { conn.stmt.executeUpdate(sql); result = "\n" + "表已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(5, 9).toLowerCase().compareTo("user") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "用户名已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(5, 9).toLowerCase().compareTo("view") == 0 || sql.substring(5, 19).toLowerCase() .compareTo("or replace view") == 0 || sql.substring(5, 14).toLowerCase() .compareTo("force view") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "视图已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(5, 11).toLowerCase().compareTo("synonym") == 0 || sql.substring(5, 22).toLowerCase() .compareTo("or replace synonym") == 0 || sql.substring(5, 29).toLowerCase() .compareTo("or replace public synonym") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "同义词已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(5, 12).toLowerCase().compareTo("sequence") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "序列已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } else if (sql.substring(5, 9).toLowerCase().compareTo("index") == 0 || sql.substring(5, 16).toLowerCase() .compareTo("unique index") == 0 || sql.substring(5, 16).toLowerCase() .compareTo("bitmap index") == 0) { try { conn.stmt.executeUpdate(sql); result = "\n" + "索引已丢弃"; } catch (Exception ex) { result = "\n" + ex.getMessage(); } } return result; } } private static final long serialVersionUID = 1L; private JMenuBar menu = null; private JMenu file = null; private JMenuItem revomecon = null; private JMenuItem open = null; private JMenuItem insave = null; private JMenuItem outsave = null; private JMenuItem exit = null; private JMenu edit = null; private JMenuItem cut = null; private JMenuItem copy = null; private JMenuItem parse = null; private JMenuItem all = null; private JMenuItem clear = null; private JMenu work = null; private JMenuItem execute = null; private JMenu help = null; private JMenuItem right = null; JToolBar bar = null; private JButton btnremovecon = null; private JButton btnexecute = null; private JButton btninsave = null; private JButton btnoutsave = null; private JButton btnhelp = null; private JTextArea txtinare = null; private JTextArea txtoutare = null; LoginBean log = null; Conn conn = null; public MainFrame(LoginBean log, Conn conn) { this.log = log; this.conn = conn; menu = new JMenuBar(); file = new JMenu("文件(F)"); revomecon = new JMenuItem("改变数据库连接..."); open = new JMenuItem("打开..."); insave = new JMenuItem("将输入另存为..."); outsave = new JMenuItem("将输出另存为..."); exit = new JMenuItem("退出"); revomecon.addActionListener(this); open.addActionListener(this); insave.addActionListener(this); outsave.addActionListener(this); exit.addActionListener(this); file.add(revomecon); file.add(open); file.add(insave); file.add(outsave); file.add(exit); menu.add(file); edit = new JMenu("编辑(E)"); cut = new JMenuItem("剪切"); copy = new JMenuItem("复制"); parse = new JMenuItem("粘贴"); all = new JMenuItem("全选"); clear = new JMenuItem("全部清除"); cut.addActionListener(this); copy.addActionListener(this); parse.addActionListener(this); all.addActionListener(this); clear.addActionListener(this); edit.add(cut); edit.add(copy); edit.add(parse); edit.add(all); edit.add(clear); menu.add(edit); work = new JMenu("WorkSheet"); execute = new JMenuItem("执行"); execute.addActionListener(this); work.add(execute); menu.add(work); help = new JMenu("帮助(H)"); right = new JMenuItem("关于..."); right.addActionListener(this); help.add(right); menu.add(help); bar = new JToolBar(); btnremovecon = new JButton(new ImageIcon("images/connect.gif")); btnremovecon.addActionListener(this); btnexecute = new JButton(new ImageIcon("images/execute.gif")); btnexecute.addActionListener(this); btninsave = new JButton(new ImageIcon("images/previous.gif")); btninsave.addActionListener(this); btnoutsave = new JButton(new ImageIcon("images/next.gif")); btnoutsave.addActionListener(this); btnhelp = new JButton(new ImageIcon("images/help.gif")); btnhelp.addActionListener(this); btnremovecon.setBorder(BorderFactory.createRaisedBevelBorder()); btnexecute.setBorder(BorderFactory.createRaisedBevelBorder()); btninsave.setBorder(BorderFactory.createRaisedBevelBorder()); btnoutsave.setBorder(BorderFactory.createRaisedBevelBorder()); btnhelp.setBorder(BorderFactory.createRaisedBevelBorder()); bar.add(btnremovecon); bar.add(btnexecute); bar.add(btninsave); bar.add(btnoutsave); bar.add(btnhelp); bar.setBorder(BorderFactory.createLineBorder(Color.darkGray)); txtinare = new JTextArea(60, 80); txtinare.setBorder(BorderFactory.createEtchedBorder()); String pass = log.getUsername() + "/"; for (int i = 0; i < log.getPassword().length(); i++) { pass += "*"; } txtinare.setText("connect " + pass); JScrollPane jsp = new JScrollPane(txtinare); jsp.setBorder(BorderFactory.createTitledBorder("输入窗口")); jsp.setFont(new Font("楷体", Font.BOLD + Font.CENTER_BASELINE, 28)); txtoutare = new JTextArea(60, 80); txtoutare.setBorder(BorderFactory.createEtchedBorder()); Date date = new Date(); txtoutare.setEditable(false); String right = "\n" + "SQL*Plus:Release 9.2.0.2.0 - Production on " + date; right += "\n" + "Copyright (C) 1982,2002,Oracle Corporation. All rights reserved."; right += "\n\n\n" + "已连接"; txtoutare.append(right); // txtoutare.append("\n"+"已连接"); JScrollPane jsp1 = new JScrollPane(txtoutare); jsp1.setBorder(BorderFactory.createTitledBorder("输出窗口")); jsp1.setFont(new Font("楷体", Font.BOLD + Font.CENTER_BASELINE, 28)); JPanel p9 = new JPanel(); p9.setLayout(new GridLayout(2, 1)); p9.add(jsp); p9.add(jsp1); Container con = this.getContentPane(); con.add(bar, BorderLayout.NORTH); con.add(p9, BorderLayout.CENTER); this.setJMenuBar(menu); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(1000, 600); this.setTitle("SQL*Plus Worksheet"); ImageIcon image = new ImageIcon("images/vtw16.gif"); this.setIconImage(image.getImage()); // this.pack(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == exit) { System.exit(0); } if (e.getSource() == revomecon || e.getSource() == btnremovecon) { new Login(this); // this.dispose(); } // 保存sql语句 if (e.getSource() == insave || e.getSource() == btninsave) { JFileChooser chooser = new JFileChooser("."); FileFliter fliter = new FileFliter(); // File file=new File(txtinare.getText()); // fliter.accept(file); chooser.addChoosableFileFilter(fliter); // 设置默认的文件管理器 chooser.setFileFilter(fliter); int rs = chooser.showSaveDialog(this); if (rs == JFileChooser.APPROVE_OPTION) { try { BufferedReader br = new BufferedReader(new StringReader( txtinare.getText())); BufferedWriter bw = new BufferedWriter(new FileWriter( chooser.getSelectedFile().getAbsolutePath())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } bw.flush(); bw.close(); JOptionPane.showMessageDialog(null, "sql语句保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE); // txtinare.setText(""); } catch (Exception ex) { System.out.print(ex.getMessage()); } } } // 保存输出结果窗口的文本 if (e.getSource() == outsave || e.getSource() == btnoutsave) { JFileChooser chooser = new JFileChooser("."); FileFliter fliter = new FileFliter(); // File file=new File(txtinare.getText()); // fliter.accept(file); chooser.addChoosableFileFilter(fliter); // 设置默认的文件管理器 chooser.setFileFilter(fliter); int rs = chooser.showSaveDialog(this); if (rs == JFileChooser.APPROVE_OPTION) { try { // BufferedReader br=new BufferedReader(new // StringReader(txtoutare.getText())); BufferedReader br = new BufferedReader(new StringReader( txtoutare.getText())); BufferedWriter bw = new BufferedWriter(new FileWriter( chooser.getSelectedFile().getAbsolutePath())); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } bw.flush(); bw.close(); JOptionPane.showMessageDialog(null, "输出结果保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) { System.out.print(ex.getMessage()); } } } // 打开文件 if (e.getSource() == open) { JFileChooser chooser = new JFileChooser("."); FileFliter fliter = new FileFliter(); chooser.addChoosableFileFilter(fliter); // 设置默认的文件管理器 chooser.setFileFilter(fliter); chooser.showOpenDialog(null); BufferedReader brr = null; String line; String str = ""; try { brr = new BufferedReader(new FileReader(chooser .getSelectedFile().getAbsolutePath())); line = brr.readLine(); str = line; while ((line = brr.readLine()) != null) { str += "\n"; str += line; } brr.close(); txtinare.setText(str); } catch (Exception ex) { System.out.println(ex.getMessage()); } } if (e.getSource() == all) { txtinare.selectAll(); } if (e.getSource() == clear) { txtinare.setText(""); } if (e.getSource() == cut) { txtinare.cut(); } if (e.getSource() == copy) { txtinare.copy(); } if (e.getSource() == parse) { txtinare.paste(); } if (e.getSource() == execute || e.getSource() == btnexecute) { if (!txtinare.getText().equals("")) { String sub = txtinare.getText().substring(0, 6); System.out.println(sub); // 查询数据 if (sub.toLowerCase().compareTo("select") == 0) { System.out.println("aaa"); String result = "\n"; try { ResultSet rs = conn.stmt.executeQuery(txtinare .getText()); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { result += rsmd.getColumnName(i) + "\t"; } result += "\n"; int m = result.length(); for (int i = 0; i < m + m / 2; i++) { result += "—"; } result += "\n"; while (rs.next()) { for (int i = 1; i <= rsmd.getColumnCount(); i++) { result += rs.getString(i) + "\t"; } result += "\n"; } txtoutare.append(result); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } // 创建 else if (txtinare.getText().substring(0, 6).toLowerCase() .compareTo("create") == 0) { Create create = new Create(txtinare.getText(), conn); String result = create.createMethod(); txtoutare.append(result); } // System.out.println(txtinare.getText().substring(0, 4)); // 查询表结构 else if (txtinare.getText().substring(0, 4).toLowerCase() .compareTo("desc") == 0) { int length = txtinare.getText().length(); String result = "\n"; try { ResultSet rs = conn.stmt.executeQuery("select * from " + txtinare.getText().substring(5, length) .toLowerCase()); System.out.println(txtinare.getText().substring(0, 4)); ResultSetMetaData rsmd = rs.getMetaData(); result += "列名" + "\t" + "数据类型" + "\n"; result += "--------------------------------------------------------" + "\n"; for (int i = 1; i <= rsmd.getColumnCount(); i++) { result += rsmd.getColumnName(i) + "\t" + rsmd.getColumnTypeName(i) + "\n"; } result += "\n"; txtoutare.append(result); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } // 删除 else if (txtinare.getText().substring(0, 4).toLowerCase() .compareTo("drop") == 0) { System.out.println(txtinare.getText().substring(0, 4)); Drop drop = new Drop(txtinare.getText(), conn); String result = drop.dropMethod(); txtoutare.append(result); } // 授权 else if (txtinare.getText().substring(0, 5).toLowerCase() .compareTo("grant") == 0) { try { conn.stmt.executeUpdate(txtinare.getText()); txtoutare.append("\n" + "授权成功"); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } // 撤销 else if (txtinare.getText().substring(0, 6).toLowerCase() .compareTo("revoke") == 0) { try { conn.stmt.executeUpdate(txtinare.getText()); txtoutare.append("\n" + "撤销成功"); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } else if (txtinare.getText().substring(0, 4).toLowerCase() .compareTo("show") == 0) { txtoutare.append("\n当前用户" + "'" + log.getUsername() + "'"); } else if (txtinare.getText().substring(0, 5).toLowerCase() .compareTo("alter") == 0) { try { conn.stmt.executeUpdate(txtinare.getText()); txtoutare.append("\n" + "已修改"); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } else { try { conn.stmt.executeUpdate(txtinare.getText()); int row = conn.stmt.getUpdateCount(); txtoutare.append("\n" + row + "行受到影响"); } catch (Exception ex) { txtoutare.append("\n" + ex.getMessage()); } } } } }}
Login.java
import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Login extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField username = null; private JPasswordField password = null; private JTextField servername = null; private JComboBox<String> shenfen = null; JButton ok = null; private JButton cancle = null; MainFrame form; public Login(MainFrame f) { JLabel name = new JLabel("用户名(U)"); username = new JTextField(10); JPanel p4 = new JPanel(); p4.add(name); p4.add(username); JLabel pass = new JLabel("密 码(P)"); password = new JPasswordField(10); JPanel p5 = new JPanel(); p5.add(pass); p5.add(password); JLabel server = new JLabel("服 务"); servername = new JTextField(10); JPanel p6 = new JPanel(); p6.add(server); p6.add(servername); JLabel shen = new JLabel("连 接 身 份(&A)"); String[] fen = { "Normal", "SYSDBA", "SYSOPER" }; shenfen = new JComboBox<String>(fen); JPanel p7 = new JPanel(); p7.add(shen); p7.add(shenfen); ok = new JButton("确定"); cancle = new JButton("取消"); JPanel p8 = new JPanel(); p8.add(ok); p8.add(cancle); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(5, 1)); p1.add(p4); p1.add(p5); p1.add(p6); p1.add(p7); p1.add(p8); ok.addActionListener(this); cancle.addActionListener(this); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(1, 2)); p3.add(p1, BorderLayout.CENTER); Container con = this.getContentPane(); con.add(p3, BorderLayout.CENTER); this.setSize(515, 325); this.setVisible(true); this.setCursor(Cursor.getDefaultCursor()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Oracle SQL Worksheet 登陆"); form = f; } @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { LoginBean log = new LoginBean(); log.setUsername(username.getText()); log.setPassword(password.getText()); log.setServer(servername.getText()); log.setShenfen((String) shenfen.getSelectedItem()); if (e.getSource() == ok) { Conn conn = new Conn(log); if (form == null) { form = new MainFrame(log, conn); form.show(); } else { form.dispose(); form = new MainFrame(log, conn); } this.dispose(); } if (e.getSource() == cancle) { System.exit(0); } } public static void main(String[] args) { new Login(null); }}
用户点评