Learning Java 4 – Swing, SQL, and Security API
-
Upload
caswenson -
Category
Technology
-
view
3.958 -
download
4
description
Transcript of Learning Java 4 – Swing, SQL, and Security API
OverviewOverview• SQL• Security API• Swing
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
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();
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
CodeCode
ResultSet rs = stmt.executeQuery(“select name from person”);
while (rs.next()){
System.out.println(rs.getObject(1));}
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
Digest ExampleDigest Example
MessageDigest sha = MessageDigest.getInstance("SHA-1");sha.update(i1); sha.update(i2); sha.update(i3); byte[]
hash = sha.digest();
Digest TypesDigest Types
• MD2• MD5• SHA-1• SHA-256• SHA-384• SHA-512
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);
Cipher TypesCipher Types• Algorithms
– AES– RC2/RC4/RC5– RSA – actually, PKCS #1– DES– DESede
• Modes– NONE– CBC– CFB– ECB– OFB– PCBC
• Padding– NoPadding– PKCS5Padding
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);
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
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);
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
ExampleExample
JFrame f = new JFrame(“Test”);f.setVisible(true);f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);}
});
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
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
LayoutsLayouts
MenusMenus
• JMenuBar is the bar at the top (setJMenuBar)– Add JMenus, which have JMenuItems– addMenuListener to catch stuff
• menuCanceled• menuDeselected• menuSelected
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"));
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