Wednesday, 6 November 2019

Swing Save

Hi,
Pls use this code

------------------------------------------------------

package placement;



import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.JOptionPane;

class Skill extends Frame implements ActionListener{

TextField rid,rdate,rdescription;
Button b1,b2;
Skill(String skid,String name,String description)
{
System.out.println(skid+" "+name+" "+description+"')");

     setBackground(Color.getHSBColor(45,92,100));

setLayout(new FlowLayout());
this.setLayout(null);
Label id=new Label("Skid:",Label.CENTER);
Label nam=new Label("Name:",Label.CENTER);
Label des=new Label("Description:",Label.CENTER);

               
               


  rid=new TextField(20);
  rdate=new TextField(20);
                  rdescription=new TextField(20);



      id.setBounds(70,90,90,60);
      nam.setBounds(70,130,90,60);
      des.setBounds(70,170,90,60);
   


        rid.setBounds(200,100,90,20);
        rdate.setBounds(200,140,90,20);
        rdescription.setBounds(200,180,90,20);
   
             

    b1=new Button("Update");
            b1.setBounds(200,240,70,40);
    b2=new Button("close");
    b2.setBounds(280,240,70,40);
    //b3=new Button("close");
    //b3.setBounds(360,240,70,40);

    b1.addActionListener(this);
b2.addActionListener(this);
//b3.addActionListener(this);

add(rid);
add(rdate);
add(rdescription);


add(b1);
add(b2);
//add(b3);
this.add(id);
this.add(nam);
this.add(des);



setSize(700,700);
setLayout(null);
rid.setText(skid);
rdate.setText(name);
                                    rdescription.setText(description);
                                        rid.setEnabled(false);


}
public void autoid()
{
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
                                    String qr="select max(rid) as mid from reminder";
                                    System.out.println(qr);
    ResultSet rs=stmt.executeQuery(qr);
  int flag=0;
                                  int mid=0;
                                   while(rs.next())
                                   {
                                     
                                        mid=rs.getInt("mid");
                                        mid=mid+1;
                                     
                                   }
                                  JOptionPane.showMessageDialog(null,"New id "+mid);
                                  rid.setText(""+mid);
                                  rid.setEnabled(false);
                                con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}

}


 Skill(){
     setBackground(Color.getHSBColor(97,92,247));

setLayout(new FlowLayout());
this.setLayout(null);
                 Label id=new Label("RId:",Label.CENTER);
Label name=new Label("Reminder Date:",Label.CENTER);
Label description=new Label("Description:",Label.CENTER);



          rid=new TextField(20);
  rdate=new TextField(20);
                 rdescription=new TextField(20);


               id.setBounds(70,90,90,60);
      name.setBounds(70,130,90,60);
      description.setBounds(70,170,90,60);
   


        rid.setBounds(200,110,90,20);
        rdate.setBounds(200,150,90,20);
       rdescription.setBounds(200,190,90,20);
     

    b1=new Button("submit");
        b1.setBounds(200,240,70,40);
b2=new Button("close");
    b2.setBounds(280,240,70,40);
    //b3=new Button("close");
    //b3.setBounds(360,240,70,40);

    b1.addActionListener(this);
b2.addActionListener(this);
//b3.addActionListener(this);
               
                        add(rid);
add(rdate);
add(rdescription);

                       

add(b1);
add(b2);
// add(b3);
this.add(id);
this.add(name);
this.add(description);




setSize(500,400);
setLayout(null);
setVisible(true);
                                autoid();
                               
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
              if(str.equals("close"))
{
                    int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?",
             "Exit Confirmation", JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
            new Homepage();
           setVisible(false);
       
    }
}
//setVisible(false);

if(str.equals("submit"))
{
        String id=rid.getText();
                                        String name=rdate.getText();
                                        String description=rdescription.getText();


                                          if(id.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the id");
                                            rid.requestFocusInWindow();
                                          }
                                         else  if(description.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the Description");
                                            rdescription.requestFocusInWindow();
                                         }
                                          else  if(name.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the Date");
                                            rdate.requestFocusInWindow();
                                         }
                                          else  if(name.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the Date");
                                            rdate.requestFocusInWindow();
                                         }
                                         else
                                         {
                               
                                       
                                       
try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
                                    String qr="insert into reminder values('"+id+"','"+name+"','"+description+"')";
                                    System.out.println(qr);
    stmt.executeUpdate(qr);
   JOptionPane.showMessageDialog(null,"Reminder Added");
                                    // ResultSet rs=stmt.executeQuery("select * from tbl_job");
   // while(rs.next())
   // System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
    con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
                                        }

                                if(str.equals("Update"))
{
        String id=rid.getText();
                                        String name=rdate.getText();
                                       

String description=rdescription.getText();
                                          if(id.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the id");
                                            rid.requestFocusInWindow();
                                          }
                                         else  if(name.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the Date");
                                            rdate.requestFocusInWindow();
                                         }
                                          else  if(description.equals(""))
                                        {
                                            JOptionPane.showMessageDialog(null, "Enter the Description");
                                            rdescription.requestFocusInWindow();
                                         }
                                         else
                                         {
                               
                                       
                                       
try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
                                    String qr="update  reminder set rdate='"+name+"',rdescription='"+description+"' where rid='"+id+"'";
                                    System.out.println(qr);
    stmt.executeUpdate(qr);
   JOptionPane.showMessageDialog(null,"Reminder Updated");
                                    // ResultSet rs=stmt.executeQuery("select * from tbl_job");
   // while(rs.next())
   // System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
    con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
                                        }


                                    if(str.equals("aaa"))
{
/*String id=tf1.getText();
String fname=tf2.getText();
String des=tf3.getText();
String amt=tf4.getText();
a String date=tf5.getText();
String actcredited=tf6.getText();

try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/fund","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
    stmt.executeUpdate("update fund set fname='"+tf2.getText()+"',des='"+tf3.getText()+"',amt='"+tf4.getText()+"',date='"+tf5.getText()+"',actcredited='"+tf6.getText()+"' where id='"+tf1.getText()+"'");
   // ResultSet rs=stmt.executeQuery("select * from tbl_job");
   // while(rs.next())
   // System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
    con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
                                                 *
                                                 * */
}
}
public static void main(String args[])
{
new Skill();
}
}






No comments:

Post a Comment

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...