Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Learning Java 4 – Swing, SQL, and Security API

4,528 views

Published on

Basic introduction to the Java Swing, SQL, and Security APIs.

Published in: Technology

Learning Java 4 – Swing, SQL, and Security API

  1. 1. Learning Java 4: Swing, SQL, Security Christopher Swenson Center for Information Security University of Tulsa 600 S. College Ave Tulsa, OK 74104
  2. 2. Overview <ul><li>SQL </li></ul><ul><li>Security API </li></ul><ul><li>Swing </li></ul>
  3. 3. SQL <ul><li>Load a runtime driver for the database you want to connect to </li></ul><ul><ul><li>Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”) </li></ul></ul><ul><ul><li>Make sure that the class is in your ClassPath </li></ul></ul><ul><li>java.sql.DriverManager.getConnection() </li></ul><ul><ul><li>Pass it a URL, name and password to connect </li></ul></ul><ul><li>Get a Statement object from the connection </li></ul><ul><ul><li>Use this to execute queries and updates </li></ul></ul><ul><li>Tons of Exception s: catch them all </li></ul>
  4. 4. Code <ul><li>Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”) </li></ul><ul><li>Connection conn = java.sql.DriverManager.getConnection(“jdbc:microsoft:sqlserver://127.0.0.1:1433”, “sa”, “123”); </li></ul><ul><li>Statement stmt = conn.createStatement(); </li></ul>
  5. 5. Queries / Updates <ul><li>Use the Connection methods execQuery(“…”) and execUpdate(“…”) </li></ul><ul><ul><li>Return ResultSet object </li></ul></ul><ul><li>Queries – SELECT </li></ul><ul><ul><li>rs.next() – moves one row down to the next object and returns true, or returns false when you are out of rows </li></ul></ul><ul><ul><li>rs.getObject(int i) – gets the object in the specified column number </li></ul></ul><ul><ul><ul><li>i starts at 1 </li></ul></ul></ul><ul><ul><ul><li>Often, columns must be accessed IN ORDER (can’t go back) </li></ul></ul></ul><ul><li>Updates </li></ul><ul><ul><li>INSERT, DELETE, UPDATE </li></ul></ul><ul><ul><li>Returns number of rows affected </li></ul></ul>
  6. 6. Code <ul><li>ResultSet rs = stmt.executeQuery(“select name from person”); </li></ul><ul><li>while (rs.next()) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(rs.getObject(1)); </li></ul><ul><li>} </li></ul>
  7. 7. Security API <ul><li>Cryptography architecture, provided by Cryptographic Services </li></ul><ul><li>Message digest, digital signatures, key generation and management, encryption, decryption </li></ul><ul><li>java.security, javax.crypto </li></ul><ul><li>http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html </li></ul>
  8. 8. Digest Example <ul><li>MessageDigest sha = MessageDigest.getInstance(&quot;SHA-1&quot;); </li></ul><ul><li>sha.update(i1); sha.update(i2); sha.update(i3); byte[] hash = sha.digest(); </li></ul>
  9. 9. Digest Types <ul><li>MD2 </li></ul><ul><li>MD5 </li></ul><ul><li>SHA-1 </li></ul><ul><li>SHA-256 </li></ul><ul><li>SHA-384 </li></ul><ul><li>SHA-512 </li></ul>
  10. 10. Cipher Example <ul><li>KeyGenerator keygen = KeyGenerator.getInstance(&quot;DES&quot;); </li></ul><ul><li>SecretKey desKey = keygen.generateKey(); </li></ul><ul><li>Cipher desCipher = Cipher.getInstance(&quot;DES/ECB/PKCS5Padding&quot;); </li></ul><ul><li>desCipher.init(Cipher.ENCRYPT_MODE, desKey); </li></ul><ul><li>byte[] cleartext = &quot;This is just an example&quot;.getBytes(); </li></ul><ul><li>byte[] ciphertext = desCipher.doFinal(cleartext); </li></ul><ul><li>desCipher.init(Cipher.DECRYPT_MODE, desKey); </li></ul><ul><li>byte[] cleartext1 = desCipher.doFinal(ciphertext); </li></ul>
  11. 11. Cipher Types <ul><li>Algorithms </li></ul><ul><ul><li>AES </li></ul></ul><ul><ul><li>RC2/RC4/RC5 </li></ul></ul><ul><ul><li>RSA – actually, PKCS #1 </li></ul></ul><ul><ul><li>DES </li></ul></ul><ul><ul><li>DESede </li></ul></ul><ul><li>Modes </li></ul><ul><ul><li>NONE </li></ul></ul><ul><ul><li>CBC </li></ul></ul><ul><ul><li>CFB </li></ul></ul><ul><ul><li>ECB </li></ul></ul><ul><ul><li>OFB </li></ul></ul><ul><ul><li>PCBC </li></ul></ul><ul><li>Padding </li></ul><ul><ul><li>NoPadding </li></ul></ul><ul><ul><li>PKCS5Padding </li></ul></ul>
  12. 12. RSA <ul><li>Will need Bouncy Castle provider </li></ul><ul><li>KeyPairGenerator keyGen = KeyPairGenerator.getInstance(&quot;RSA&quot;); </li></ul><ul><li>keyGen.initialize(1024); </li></ul><ul><li>KeyPair key = keyGen.generateKeyPair(); </li></ul><ul><li>Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/PKCS1Padding&quot;); </li></ul><ul><li>cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); </li></ul><ul><li>byte[] cipherText = cipher.doFinal(plainText); </li></ul><ul><li>cipher.init(Cipher.DECRYPT_MODE, key.getPrivate()); </li></ul><ul><li>byte[] newPlainText = cipher.doFinal(cipherText); </li></ul>
  13. 13. Swing <ul><li>Swing is a platform-independent GUI-building kit </li></ul><ul><ul><li>javax.swing </li></ul></ul><ul><li>Very easy to use </li></ul><ul><ul><li>Most constructs are simple </li></ul></ul><ul><li>Clean for most uses </li></ul><ul><li>Event-driven </li></ul><ul><li>Based on the AWT, the original GUI package for Java </li></ul><ul><ul><li>java.awt </li></ul></ul>
  14. 14. JFrame <ul><li>JFrame is the basic class </li></ul><ul><ul><li>Creates a window </li></ul></ul><ul><ul><li>Extend it, and override the constructor </li></ul></ul><ul><ul><li>Make a new JFrame, add stuff to it </li></ul></ul><ul><ul><li>Starts out invisible by default </li></ul></ul><ul><li>JFrame f = new JFrame(“Title here”); </li></ul><ul><li>f.setVisible(true); </li></ul>
  15. 15. Close Window <ul><li>Closing the window should exit your program, right? </li></ul><ul><ul><li>Use “actions” to do this </li></ul></ul><ul><ul><li>Also for keystrokes, mouse clicks </li></ul></ul><ul><li>WindowAdapter is a skeleton class to capture Window actions (like, clicking the close button) </li></ul><ul><li>Catches ActionEvent s, and figures out what to do with them </li></ul><ul><li>Also InputEvent s </li></ul>
  16. 16. Example <ul><li>JFrame f = new JFrame(“Test”); </li></ul><ul><li>f.setVisible(true); </li></ul><ul><li>f.addWindowListener(new WindowAdapter() </li></ul><ul><li>{ </li></ul><ul><li>public void windowClosing(WindowEvent we) </li></ul><ul><li>{ </li></ul><ul><li>System.exit(0); </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>
  17. 17. Let’s add stuff <ul><li>Some basic classes to mess with </li></ul><ul><ul><li>Most everything subclasses JComponent </li></ul></ul><ul><li>JLabel </li></ul><ul><ul><li>Text (can use simple HTML, like <b>text</b>) </li></ul></ul><ul><li>JTextField </li></ul><ul><ul><li>An line of text input </li></ul></ul><ul><li>JButton </li></ul><ul><ul><li>A clickable button </li></ul></ul><ul><ul><li>Anything implementing AbstractButton is “clickable” </li></ul></ul><ul><ul><li>JRadioButton – set a group with ButtonGroup </li></ul></ul><ul><li>JProgressBar </li></ul>
  18. 18. Adding objects <ul><li>Pre-Java 1.5 </li></ul><ul><ul><li>Container c = frame.getContentPane(); </li></ul></ul><ul><ul><li>c.add(new JLabel(“Text”)); </li></ul></ul><ul><li>Now, just frame.add(…) </li></ul><ul><li>By default, uses an empty FlowLayout </li></ul><ul><ul><li>Change the Layout via setLayout(…) </li></ul></ul><ul><ul><li>BorderLayout, BoxLayout, CardLayout, FlowLayout, GridBagLayout, GridLayout, SpringLayout </li></ul></ul>
  19. 19. Layouts
  20. 20. Menus <ul><li>JMenuBar is the bar at the top (setJMenuBar) </li></ul><ul><ul><li>Add JMenus, which have JMenuItems </li></ul></ul><ul><ul><li>addMenuListener to catch stuff </li></ul></ul><ul><ul><ul><li>menuCanceled </li></ul></ul></ul><ul><ul><ul><li>menuDeselected </li></ul></ul></ul><ul><ul><ul><li>menuSelected </li></ul></ul></ul>
  21. 21. Panes <ul><li>JPanel – create a new container to work with </li></ul><ul><ul><li>A sub-frame </li></ul></ul><ul><li>Allow you to do more complicated things </li></ul><ul><ul><li>JTabbedPane – tabs </li></ul></ul><ul><li>Most JComponent extended objects can have borders added </li></ul><ul><ul><li>setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)) </li></ul></ul><ul><ul><li>setBorder(BorderFactory.createTitledBorder(“Stuff&quot;)); </li></ul></ul>
  22. 22. Extra tips for Swing <ul><li>UIManager.setLookAndFeel(&quot;com.sun.java.swing.plaf.windows.WindowsLookAndFeel&quot;); </li></ul><ul><ul><li>Makes things more native looking </li></ul></ul><ul><li>GridBagLayout </li></ul><ul><ul><li>Powerful, difficult to use layout </li></ul></ul>

×