-2

i want to delete customer's perticular news paper from database instead of full detail of customer?? one customer having multiple paper in their record but i want to delte any paper from multiples papers. i am enter customer id and selecting whch paper i want to delete thanks for the help in advanced...

enter code here:   
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;

public abstract class delete_paper extends JFrame implements ActionListener
{
JTextField textFieldId;
JLabel l1;
JLabel l3;
JLabel l5;
JLabel l6;
    JComboBox combo;
    String course[] = {"Navakal","SandhyaKal","Pudhari",
      "MidDay","Inqlab","BusinessLine","MumbaiSamachar",
       "GujrajSamachar","KarnatakMalla","Vartahar","PunyaNagari"};
JButton b2;
Container c = getContentPane();
delete_paper()
{
    super("Shree DattaDigambar Samarth");
    setBounds(140,250,777,555);
    c.setLayout(null);
    textFieldId = new JTextField();           
    l1 = new JLabel("New Customer Entry");
    l3 = new JLabel("Customer Name");       
    l5 = new JLabel("Paper");
            combo = new JComboBox(course);
    l1.setBounds(10,10,340,20);
    l3.setBounds(110,20,140,70);
    l5.setBounds(400,50,140,20);        
    textFieldId.setBounds(10,70,70,20);
            combo.setBounds(400,70,130,20);              
    b2 = new JButton("Ok");     
    b2.setBounds(10,160,50,20);      
            c.add(combo);       
    c.add(b2);
    c.add(l1);
    c.add(l3);
    c.add(l5);
    c.add(textFieldId);            
            setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);                
            b2.addActionListener(this);
}
    public static void main(String[] args) 
    {
        delete_paper ap=new delete_paper() {};
    }
     public void actionPerformed(ActionEvent e)
        {

            System.out.println("You clicked the button");                
            if(e.getSource()==b2)
            {
                try 
                {
                    Connection con;
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection("jdbc:odbc:devendra");
                    try 
                    {

                        java.sql.Statement st = con.createStatement();
                        PreparedStatement ps = con.prepareStatement("delete 
                        from   Customer where Customer_Id = ? and Paper_Name in (?) ");
                        ps.setString(1,textFieldId.getText());
                        ps.setString(4,combo.getSelectedItem().toString());
                        ps.executeUpdate();
                        JOptionPane.showMessageDialog(null, 
                       "You successfully Enter the Entry");
                    } 
                    catch (SQLException s) 
                    {
                        System.out.println("SQL code does not execute.");
                        JOptionPane.showMessageDialog(null,"Please Enter 
                        the Detail Correctly");
                    }
                } 
                catch (ClassNotFoundException | SQLException ee) 
                {
                    System.out.println("Error:connection not created");
                    JOptionPane.showMessageDialog(null,"Please 
                    Enter the Detail Correctly");
                }
            }
        }
         }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • add stacktrace message. – KhAn SaAb Jun 24 '13 at 05:35
  • 3
    Use java naming convention for classes. Don't use null layout and setBounds. Define correct Layoutmanager. Don't use null parent in dialogs and JOptionPanes. Name variables with some reasonable names not label1, label2. SQLException should be catched just once. Don't prepare statement on each click. Prepare it just once and reuse. Use capital letters in you text to help readers split you text. Post SSCCE and as much details as possible (reasonable). Than ask for help. – StanislavL Jun 24 '13 at 05:41
  • @StanislavL Wow! The 8 (if I counted correctly) point 'great tips for questions' ..as a comment.. Well done. :) – Andrew Thompson Jun 24 '13 at 06:31
  • Please refine your question, as @StanislavL suggests, and comment on the proposed answer; don't just ask a vague, new [question](http://stackoverflow.com/q/17270019/230513). – trashgod Jun 24 '13 at 08:53

1 Answers1

0

You need not to use the index of table column while setting the values in Prepared statement. It is only the index of ? which is set :

Use

                ps.setString(2,combo.getSelectedItem().toString());

instead of

                ps.setString(4,combo.getSelectedItem().toString());
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136