final static private String _driver = "oracle.jdbc.driver.OracleDriver"; final static private String _url = "jdbc:oracle:thin:username/passWord@(description=(address_list=(address=(protocol=tcp)(host=developer)(port=1521)))(source_route=yes)(connect_data=(sid=ansid)))";
public void init(){ setBackground(Color.white); text = new JLabel("Text to save to file:"); clicked = new JLabel("Text retrieved from file:"); button = new JButton("Click Me"); button.addActionListener(this); clickButton = new JButton("Click Again"); clickButton.addActionListener(this); textField = new JTextField(20); setLayout(new BorderLayout()); setBackground(Color.white); add(BorderLayout.NORTH, text); add(BorderLayout.CENTER, textField); add(BorderLayout.SOUTH, button); }
public void start(){ System.out.println("Applet starting."); }
public void stop(){ System.out.println("Applet stopping."); }
public void destroy(){ System.out.println("Destroy method called."); }
public void actionPerformed(ActionEvent event){ try{ Class.forName (_driver); c = DriverManager.getConnection(_url); }catch (java.lang.ClassNotFoundException e){ System.out.println("Cannot find driver"); System.out.println(e.getMessage()); }catch (java.sql.SQLException e){ System.out.println("Cannot get connection"); System.out.println(e.getMessage()); }
Object source = event.getSource(); if(source == button){ if(_clickMeMode){ JTextArea displayText = new JTextArea(); try{ //Write to database String theText = textField.getText(); Statement stmt = c.createStatement(); String updateString = "INSERT INTO dba VALUES ('" + theText + "')"; int count = stmt.executeUpdate(updateString); //Read from database ResultSet results = stmt.executeQuery("SELECT TEXT FROM dba "); while(results.next()){ String s = results.getString("TEXT"); displayText.append(s + "/n"); } stmt.close(); }catch(java.sql.SQLException e){ System.out.println("Cannot create SQL statement"); System.out.println(e.getMessage()); }
//Display text read from database text.setText("Text retrieved from file:"); button.setText("Click Again"); _clickMeMode = false; //Display text read from database } else { text.setText("Text to save to file:"); textField.setText(""); button.setText("Click Me"); _clickMeMode = true; } } } }