Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.sql.*;
  7.  
  8. public class gui implements ActionListener {
  9.  
  10. private static JButton addButton;
  11. private static JButton dropButton;
  12. private static JButton alterButton;
  13. private static JButton viewButton;
  14. private static JButton exitButton;
  15.  
  16. //private static Connection con;
  17.  
  18. public gui () {
  19. this.addButton = new JButton("Create Tables");
  20. this.dropButton = new JButton("Drop Tables");
  21. this.alterButton = new JButton("Alter Tables");
  22. this.viewButton = new JButton("View Tables");
  23. this.exitButton = new JButton("Exit");
  24.  
  25. this.addButton.addActionListener(this);
  26. this.dropButton.addActionListener(this);
  27. this.alterButton.addActionListener(this);
  28. this.viewButton.addActionListener(this);
  29. this.exitButton.addActionListener(this);
  30. }
  31.  
  32.  
  33. private static class HelloWorldDisplay extends JPanel {
  34. public void paintComponent(Graphics g) {
  35. super.paintComponent(g);
  36. g.drawString("Hello World", 20, 30);
  37. }
  38. }
  39.  
  40. private static class exitHandler implements ActionListener {
  41. public void actionPerformed(ActionEvent e) {
  42. System.exit(0);
  43. }
  44. }
  45.  
  46.  
  47.  
  48. public void actionPerformed(ActionEvent e) {
  49.  
  50. if (e.getSource() == addButton) {
  51. try{
  52. //step1 load the driver class
  53. Class.forName("oracle.jdbc.driver.OracleDriver");
  54.  
  55. //step2 create the connection object
  56. Connection con=DriverManager.getConnection(
  57. "jdbc:oracle:thin:@oracle.scs.ryerson.ca:1521:orcl","etkim","04229574");
  58. Statement stmt = con.createStatement();
  59. ResultSet rs=stmt.executeQuery("");
  60. while(rs.next())
  61. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
  62.  
  63. //step5 close the connection object
  64. con.close();
  65. }catch(Exception error){System.out.println(error);}
  66. }
  67.  
  68. if (e.getSource() == viewButton) {
  69. try{
  70. //step1 load the driver class
  71. Class.forName("oracle.jdbc.driver.OracleDriver");
  72.  
  73. //step2 create the connection object
  74. Connection con=DriverManager.getConnection(
  75. "jdbc:oracle:thin:@oracle.scs.ryerson.ca:1521:orcl","etkim","04229574");
  76. Statement stmt = con.createStatement();
  77. ResultSet rs=stmt.executeQuery("SELECT table_name FROM user_tables");
  78. ResultSetMetaData rsmd = rs.getMetaData();
  79. int columnsNumber = rsmd.getColumnCount();
  80. while(rs.next()) {
  81. for (int i = 1; i <= columnsNumber; i++) {
  82. if (i > 1) System.out.print(", ");
  83. String columnValue = rs.getString(i);
  84. System.out.print(columnValue + " " + rsmd.getColumnName(i));
  85. }
  86. System.out.println("");
  87. }
  88.  
  89.  
  90. //step5 close the connection object
  91. con.close();
  92. }catch(Exception error){System.out.println(error);}
  93. }
  94. }
  95.  
  96.  
  97.  
  98. public static void main(String[] args) {
  99. gui g = new gui();
  100.  
  101. try{
  102. //step1 load the driver class
  103. Class.forName("oracle.jdbc.driver.OracleDriver");
  104.  
  105. //step2 create the connection object
  106. Connection con=DriverManager.getConnection(
  107. "jdbc:oracle:thin:@oracle.scs.ryerson.ca:1521:orcl","etkim","04229574");
  108.  
  109. //step3 create the statement object
  110. Statement stmt=con.createStatement();
  111.  
  112. //step4 execute query
  113. ResultSet rs=stmt.executeQuery("select * from emp");
  114. while(rs.next())
  115. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
  116.  
  117. //step5 close the connection object
  118. con.close();
  119.  
  120. }catch(Exception e){ System.out.println(e);}
  121.  
  122.  
  123. HelloWorldDisplay displayPanel = new HelloWorldDisplay();
  124.  
  125.  
  126.  
  127. JPanel content = new JPanel();
  128. content.setLayout(new GridLayout(5,1));
  129. content.add(addButton);
  130. content.add(dropButton);
  131. content.add(alterButton);
  132. content.add(viewButton);
  133. content.add(exitButton);
  134.  
  135. //SELECT table_name FROM user_tables
  136.  
  137.  
  138. JFrame window = new JFrame("Database GUI");
  139. window.setContentPane(content);
  140. window.setSize(250,400);
  141. window.setLocation(100,100);
  142. window.setVisible(true);
  143.  
  144.  
  145.  
  146.  
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement