Well i have a Registration form GUI in my netbeans. in my GUI there is a textField that is to write the username and password etc. and a button named "Register". what i wanted is when i put a username and my password etc. in textField then click the button named "Register" will send it to my MySQL Query Browser database.
what code will i do?
this is my existing code from my Registration form GUI.
public class Register extends javax.swing.JFrame {
public Register() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jTextField4 = new javax.swing.JTextField(); jTextField5 = new javax.swing.JTextField(); jTextField6 = new javax.swing.JTextField(); jTextField7 = new javax.swing.JTextField(); jTextField8 = new javax.swing.JTextField(); jPasswordField1 = new javax.swing.JPasswordField(); label1 = new java.awt.Label(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Username"); jLabel2.setText("Password"); jLabel4.setText("E-mail"); jLabel5.setText("Location"); jLabel6.setText("Status"); jLabel7.setText("Occupation"); jLabel8.setText("Zip code"); jButton1.setText("Register"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton1MouseClicked(evt); } }); jTextField1.setText("Username"); jTextField4.setText("E-mail address"); jTextField5.setText("Occupation"); jTextField6.setText("Status"); jTextField7.setText("Location"); jTextField8.setText("Zip code"); jPasswordField1.setText("jPasswordField1"); label1.setText("Register"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(56, 56, 56) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jLabel1) .addComponent(jLabel4) .addComponent(jLabel7) .addComponent(jLabel6) .addComponent(jLabel5) .addComponent(jLabel8)) .addGap(53, 53, 53) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE) .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE) .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE) .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE) .addComponent(jPasswordField1)) .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1))) .addGroup(layout.createSequentialGroup() .addGap(33, 33, 33) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(86, 86, 86)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(29, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { DailyConsumptionForm.jTextField1.setText(getUsername()); DailyConsumptionForm.jPasswordField1.setText(getPassword()); hideGUI(); } public String getUsername() { return jTextField1.getText(); } public String getPassword() { return jPasswordField1.getText(); } public String getEmail() { return jTextField4.getText(); } public String getOccupation() { return jTextField5.getText(); } public String getStatus() { return jTextField6.getText(); } public String getlocation() { return jTextField7.getText(); } public String getZip() { return jTextField8.getText(); } public void showGUI() { setVisible(true); } public void hideGUI() { setVisible(false); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DailyConsumptionRegister().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JPasswordField jPasswordField1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; private javax.swing.JTextField jTextField7; private javax.swing.JTextField jTextField8; private java.awt.Label label1; // End of variables declaration
}
import javax.swing.*; import java.awt.*; import java.sql.*; import java.awt.event.*; class RegistrationForm extends JFrame{ JTextField text1,text2,text3,text4,text5; JPasswordField pass1; JLabel label1,label2,label3,label4,label5,label6; JPanel panel; JButton button; RegistrationForm() { text1=new JTextField(15); text2=new JTextField(15); text3=new JTextField(15); pass1=new JPasswordField(15); text4=new JTextField(15); text5=new JTextField(15); label1=new JLabel("First Name"); label2=new JLabel("Last Name"); label3=new JLabel("User Name"); label4=new JLabel("Password"); label5=new JLabel("Address"); label6=new JLabel("Phone No"); button=new JButton("Register"); panel=new JPanel(new GridLayout(7,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(pass1); panel.add(label5); panel.add(text4); panel.add(label6); panel.add(text5); panel.add(button); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); String value3=text3.getText(); String value4=pass1.getText(); String value5=text4.getText(); String value6=text5.getText(); Connection con = null; String url = "jdbc:mysql://localhost:3306/";; String db = "test"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; String user1=""; String pass1=""; try{ Class.forName(driver); con = DriverManager.getConnection(url+db, user, pass); Statement st = con.createStatement(); int k=st.executeUpdate("insert into login(firstname,lastname,username,password,address,contactno) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"')"); JOptionPane.showMessageDialog(null,"Data is successfully saved"); } catch(Exception e){} } }); add(panel); setSize(300,400); setVisible(true); } }
Ads