21
Overview Overview SQL Security API Swing

Learning Java 4 – Swing, SQL, and Security API

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Learning Java 4 – Swing, SQL, and Security API

OverviewOverview• SQL• Security API• Swing

Page 2: Learning Java 4 – Swing, SQL, and Security API

SQLSQL

• Load a runtime driver for the database you want to connect to– Class.forName(“com.microsoft.jdbc.sqlserver.SQLServer

Driver”)

– Make sure that the class is in your ClassPath• java.sql.DriverManager.getConnection()

– Pass it a URL, name and password to connect

• Get a Statement object from the connection– Use this to execute queries and updates

• Tons of Exceptions: catch them all

Page 3: Learning Java 4 – Swing, SQL, and Security API

CodeCode

Class.forName(“com.microsoft.jdbc.sqlserver.SQLServerDriver”)

Connection conn = java.sql.DriverManager.getConnection(“jdbc:microsoft:sqlserver://127.0.0.1:1433”, “sa”, “123”);

Statement stmt = conn.createStatement();

Page 4: Learning Java 4 – Swing, SQL, and Security API

Queries / UpdatesQueries / Updates

• Use the Connection methods execQuery(“…”) and execUpdate(“…”)– Return ResultSet object

• Queries – SELECT– rs.next() – moves one row down to the next object and

returns true, or returns false when you are out of rows– rs.getObject(int i) – gets the object in the specified

column number• i starts at 1• Often, columns must be accessed IN ORDER (can’t go back)

• Updates– INSERT, DELETE, UPDATE– Returns number of rows affected

Page 5: Learning Java 4 – Swing, SQL, and Security API

CodeCode

ResultSet rs = stmt.executeQuery(“select name from person”);

while (rs.next()){

System.out.println(rs.getObject(1));}

Page 6: Learning Java 4 – Swing, SQL, and Security API

Security APISecurity API

• Cryptography architecture, provided by Cryptographic Services

• Message digest, digital signatures, key generation and management, encryption, decryption

• java.security, javax.crypto• http://java.sun.com/j2se/1.5.0/docs/guide/security/

CryptoSpec.html

Page 7: Learning Java 4 – Swing, SQL, and Security API

Digest ExampleDigest Example

MessageDigest sha = MessageDigest.getInstance("SHA-1");sha.update(i1); sha.update(i2); sha.update(i3); byte[]

hash = sha.digest();

Page 8: Learning Java 4 – Swing, SQL, and Security API

Digest TypesDigest Types

• MD2• MD5• SHA-1• SHA-256• SHA-384• SHA-512

Page 9: Learning Java 4 – Swing, SQL, and Security API

Cipher ExampleCipher Example

KeyGenerator keygen = KeyGenerator.getInstance("DES");

SecretKey desKey = keygen.generateKey(); Cipher desCipher =

Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, desKey); byte[] cleartext = "This is just an

example".getBytes(); byte[] ciphertext = desCipher.doFinal(cleartext); desCipher.init(Cipher.DECRYPT_MODE, desKey); byte[] cleartext1 = desCipher.doFinal(ciphertext);

Page 10: Learning Java 4 – Swing, SQL, and Security API

Cipher TypesCipher Types• Algorithms

– AES– RC2/RC4/RC5– RSA – actually, PKCS #1– DES– DESede

• Modes– NONE– CBC– CFB– ECB– OFB– PCBC

• Padding– NoPadding– PKCS5Padding

Page 11: Learning Java 4 – Swing, SQL, and Security API

RSARSA

• Will need Bouncy Castle provider

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");keyGen.initialize(1024);

KeyPair key = keyGen.generateKeyPair(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); byte[] cipherText = cipher.doFinal(plainText);

cipher.init(Cipher.DECRYPT_MODE, key.getPrivate()); byte[] newPlainText = cipher.doFinal(cipherText);

Page 12: Learning Java 4 – Swing, SQL, and Security API

SwingSwing

• Swing is a platform-independent GUI-building kit– javax.swing

• Very easy to use– Most constructs are simple

• Clean for most uses• Event-driven• Based on the AWT, the original GUI

package for Java– java.awt

Page 13: Learning Java 4 – Swing, SQL, and Security API

JFrameJFrame

• JFrame is the basic class– Creates a window– Extend it, and override the constructor– Make a new JFrame, add stuff to it– Starts out invisible by default

JFrame f = new JFrame(“Title here”);f.setVisible(true);

Page 14: Learning Java 4 – Swing, SQL, and Security API

Close WindowClose Window

• Closing the window should exit your program, right?– Use “actions” to do this– Also for keystrokes, mouse clicks

• WindowAdapter is a skeleton class to capture Window actions (like, clicking the close button)

• Catches ActionEvents, and figures out what to do with them

• Also InputEvents

Page 15: Learning Java 4 – Swing, SQL, and Security API

ExampleExample

JFrame f = new JFrame(“Test”);f.setVisible(true);f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we){

System.exit(0);}

});

Page 16: Learning Java 4 – Swing, SQL, and Security API

Let’s add stuffLet’s add stuff

• Some basic classes to mess with– Most everything subclasses JComponent

• JLabel– Text (can use simple HTML, like <b>text</b>)

• JTextField– An line of text input

• JButton– A clickable button– Anything implementing AbstractButton is “clickable”– JRadioButton – set a group with ButtonGroup

• JProgressBar

Page 17: Learning Java 4 – Swing, SQL, and Security API

Adding objectsAdding objects

• Pre-Java 1.5– Container c = frame.getContentPane();– c.add(new JLabel(“Text”));

• Now, just frame.add(…)• By default, uses an empty FlowLayout

– Change the Layout via setLayout(…)– BorderLayout, BoxLayout, CardLayout,

FlowLayout, GridBagLayout, GridLayout, SpringLayout

Page 18: Learning Java 4 – Swing, SQL, and Security API

LayoutsLayouts

Page 19: Learning Java 4 – Swing, SQL, and Security API

MenusMenus

• JMenuBar is the bar at the top (setJMenuBar)– Add JMenus, which have JMenuItems– addMenuListener to catch stuff

• menuCanceled• menuDeselected• menuSelected

Page 20: Learning Java 4 – Swing, SQL, and Security API

PanesPanes

• JPanel – create a new container to work with– A sub-frame

• Allow you to do more complicated things– JTabbedPane – tabs

• Most JComponent extended objects can have borders added– setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOW

ERED, Color.GRAY, Color.WHITE))– setBorder(BorderFactory.createTitledBorder(“Stuff"));

Page 21: Learning Java 4 – Swing, SQL, and Security API

Extra tips for SwingExtra tips for Swing

• UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");– Makes things more native looking

• GridBagLayout– Powerful, difficult to use layout