Results 1 to 5 of 5
  1. #1
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Cannot save data in mysql database

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author SAIM
     */
    public class UserInfo extends javax.swing.JInternalFrame {
    
       String gender="";
      static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://localhost/user";
       
       static final String USER = "root";
       static final String PASS = "";
       
       public static Connection myConnection=null;
       public static Statement dsatement = null;  
        
        
        
        
        
        
    
        
        
        
        
        
        public UserInfo() {
         try {
               Class.forName("com.mysql.jdbc.Driver");
               myConnection = DriverManager.getConnection(DB_URL,USER,PASS);
               dsatement = myConnection.createStatement();
            }
            catch(Exception ex)
            {
                System.out.println(ex.getMessage());
            }   
            
           initComponents();
            }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            txtname = new javax.swing.JTextField();
            divcombo = new javax.swing.JComboBox<>();
            btnmale = new javax.swing.JRadioButton();
            btnfemale = new javax.swing.JRadioButton();
            jLabel5 = new javax.swing.JLabel();
            userdivcombo = new javax.swing.JComboBox<>();
            jPanel2 = new javax.swing.JPanel();
            btnsearch = new javax.swing.JButton();
            btnsave = new javax.swing.JButton();
            btnupdate = new javax.swing.JButton();
            btndelete = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
    
            setBackground(new java.awt.Color(255, 255, 255));
            setBorder(null);
    
            jPanel1.setBackground(new java.awt.Color(153, 255, 153));
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("UserInfo"));
    
            jLabel2.setText("Name:");
    
            jLabel3.setText("Gender:");
    
            jLabel4.setText("Division:");
    
            divcombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Dhaka", "Khulna", "Rajshahi", "Comilla" }));
    
            btnmale.setText("Male");
            btnmale.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnmaleActionPerformed(evt);
                }
            });
    
            btnfemale.setText("Female");
            btnfemale.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnfemaleActionPerformed(evt);
                }
            });
    
            jLabel5.setText("UserLevel:");
    
            userdivcombo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "admin", "user", "member" }));
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(107, 107, 107)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(36, 36, 36)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(divcombo, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(btnmale)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnfemale))
                                            .addComponent(userdivcombo, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(96, 96, 96)
                            .addComponent(jLabel5)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(29, 29, 29)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(btnmale)
                        .addComponent(btnfemale))
                    .addGap(25, 25, 25)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(divcombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(29, 29, 29)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(userdivcombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(53, Short.MAX_VALUE))
            );
    
            jPanel2.setBackground(new java.awt.Color(255, 255, 153));
            jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
            btnsearch.setText("search");
    
            btnsave.setText("save");
            btnsave.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnsaveActionPerformed(evt);
                }
            });
    
            btnupdate.setText("update");
    
            btndelete.setText("delete");
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(btnsearch)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                    .addComponent(btnsave)
                    .addGap(36, 36, 36)
                    .addComponent(btnupdate)
                    .addGap(43, 43, 43)
                    .addComponent(btndelete)
                    .addGap(36, 36, 36))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnsearch)
                        .addComponent(btnsave)
                        .addComponent(btnupdate)
                        .addComponent(btndelete))
                    .addContainerGap(26, Short.MAX_VALUE))
            );
    
            jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
            ));
            jScrollPane1.setViewportView(jTable1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
             try {
                // TODO add your handling code here:
                String name = txtname.getText();
                String division =divcombo.getSelectedItem().toString();
                String userlevel =userdivcombo.getSelectedItem().toString();
               
                String sql = "insert into  userinfo (name,gender,division,userlevel) values ('"+name+"',"+gender+","+division+",'"+userlevel+"')";
                dsatement.executeUpdate(sql);
                
                
                
            } catch (SQLException ex) {
                System.out.println("Can not Save Data");
            }
        }                                       
    
        private void btnmaleActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
             btnfemale.setSelected(false);
             gender="Male";
        }                                       
    
        private void btnfemaleActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
           btnmale.setSelected(false);
           gender="Female";
        }                                         
    
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton btndelete;
        private javax.swing.JRadioButton btnfemale;
        private javax.swing.JRadioButton btnmale;
        private javax.swing.JButton btnsave;
        private javax.swing.JButton btnsearch;
        private javax.swing.JButton btnupdate;
        private javax.swing.JComboBox<String> divcombo;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        private javax.swing.JTextField txtname;
        private javax.swing.JComboBox<String> userdivcombo;
        // End of variables declaration                   
    }
    Attached Thumbnails Attached Thumbnails Cannot save data in mysql database-1.png  
    Last edited by Tolls; 11-09-2016 at 03:50 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Cannot save data in mysql database

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Cannot save data in mysql database

    During development, you need more than System.out.println("Can not Save Data"); At least log the exception somewhere so you can see what is going on. Besides that, you need PreparedStatements to avoid 1) SQL injection attacks and 2) prevent quoting issues.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Cannot save data in mysql database

    Please edit your post and replace the BOLD tags with a code tags.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Cannot save data in mysql database

    I've fixed your tags.
    I'll also echo Surfman, you need to print the stack trace at a minimum in that catch block so you know what exactly has gone wrong.

    That is assuming that is causing issues, as you haven't actually told us what happens when you run this code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 05-23-2014, 10:07 AM
  2. Replies: 1
    Last Post: 05-29-2013, 08:11 AM
  3. Replies: 3
    Last Post: 03-12-2012, 02:03 PM
  4. Dispaly data from mysql database into JSP.
    By kritijain in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-16-2011, 08:17 AM
  5. Displaying data from mysql database.
    By Haresh gurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-09-2008, 02:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •