title: java insert record to database in button click event
body: <p>Can i get a sample of a java mysql database connectivity and inserting records in a button click event?  this is the code i use in the jframe but it does not work it has a error.</p>
  
  <pre><code> private void btnlogActionPerformed(java.awt.event.ActionEvent evt) {                                       
  
       user=txtuser.getText();
        char[] pass=jPasswordField1.getPassword();
               String passString=new String(pass);
              try{                
                  **Connection con =createConnection();**             
  
  
  java.sql.PreparedStatement statement= con.prepareStatement ("INSERT INTO login(username,Password) VALUES ('" + user + "','" + passString + "')");
  
  statement.setString(1,user);
      statement.setString(2,passString);
  statement.execute();
              }
              catch(Exception e){
                  JOptionPane.showMessageDialog(null,"Exception: "+ e.toString());
              }
  
  public static void main(String args[]) {
   try {
              Class.forName("com.mysql.jdbc.Driver");
              String connectionUrl = "jdbc:mysql://localhost/Stock?"+
                                     "user=root&amp;password=";
              Connection con = DriverManager.getConnection(connectionUrl);
          } catch (SQLException e) {
              JOptionPane.showMessageDialog(null,"SQL Exception: "+ e.toString());
          } catch (ClassNotFoundException cE) {
              JOptionPane.showMessageDialog(null,"Class Not Found Exception: "+ cE.toString());
          }
  </code></pre>