Saturday, March 17, 2018

Insert Data Into SQL Server Using Java

From this tutorial you will be able to insert data into SQL Server Database by using Textbox, Combobox, Radiobuttons, Checkbox.


         
Below is the code to insert data into Sql server

Please watch the video because there are some task to be done beside code


private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                     
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:sqlserver://localhost:1433;databaseName=testdb;user=sa;password=orchid";
            Connection con = DriverManager.getConnection(url);
            String query = "insert into users(name, address, gender, knowledge, subject)values(?,?,?,?,?)";
            PreparedStatement pst = con.prepareStatement(query);
            pst.setString(1, name.getText());
            pst.setString(2, address.getText());
            if(male.isSelected()){
                gender="Male";
            }
            if(female.isSelected()){
                gender="Female";
            }
            pst.setString(3, gender);
            String knowledge="";
            if(coreJava.isSelected()){
                knowledge+=coreJava.getText()+" ";
            }
            if(python.isSelected()){
                knowledge+=python.getText()+" ";
            }
            pst.setString(4, knowledge);
            String course;
            course=subject.getSelectedItem().toString();
            pst.setString(5, course);
            pst.executeUpdate();
         
            JOptionPane.showMessageDialog(null, "Inserted Sucessfully!");
        }
         catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
     
    }   

Please don't forget To Like, Share and Subscribe my Youtube video and Channel.

Youtube Channel Subscription Click Here                               

No comments:

Post a Comment