I have a project on sports Club Management. In that, I am providing add, delete, update and search facility.
Add command is working simply by : String qry="Insert into sport values('"+s1+"')";
smt.executeUpdate(qry);

This is just an example where I took only s1.

But in delete, I wrote:

String qry="Delete from sport where Mid=' "+smid+" '";
rs=smt.executeQuery(qry);
rs.next();

/* Here mid is Member id. According to the query statement, all the values i.e. the entire row which has smid as Mid should get deleted. But this doesn't happen. At first it showed a SQLException Too few parameters. After making a bit changes it showed No resultset.
Can you pls. correct me.

One more thing I am in the constructor portion, taking smt=con.createStatement();
*/

//When I tried the search part, I provided the query: 
String qry="Select * from sport where Mid='"+smid+"';
rs=smt.executeQuery(qry);
rs.next();

// As the row contains some value which came from Choice. 
// When I added it into access table, I wrote:
bla.getSelectedItem();
//In case of setting text:
bla.select("Badminton");

//and it worked. 

//But in case of retrieving, I am writing:
String s5,s6;
s5=rs.getString("Sports");
s6=rs.getInt("Fees");

tsp.select(s5);
tfee.select(String.valueOf(s6));

//But again this code is not working and gives SQLException "Invalid cursor state."

//Pls. correct me if possible..

I am giving you a code for adding record to a database of student. You can take help from this of how to add record to database and modify it as per your requirement.

package com;

import java.sql.*;

public class Enterdb
{
    public int enter(String fname,String eno,String un,String pw,String stream,String sex,String cnum,String add,String yoa)
    {
        
        PreparedStatement st=null;
        Statement st1=null;
        Connection con=null;
        ResultSet rs=null;
        int n=0,n1=0,flag=0;
        try
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch(ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college2","root",null);
            st1=con.createStatement();
            rs=st1.executeQuery("Select * from login");
            while(rs.next())
            {
                if(un.equalsIgnoreCase(rs.getString("un")) && eno.equals(rs.getString("eno")))
                {
                    flag=1;
                }
            }
            if(flag!=1)
                {
                st=con.prepareStatement("Insert into detail2 values(?,?,?,?,?,?,?,?)");
                st.setString(1,fname);
                st.setString(2,eno);
                st.setString(3,un);
                st.setString(4,stream);
                st.setString(5,sex);
                st.setString(6,cnum);
                st.setString(7,add);
                st.setString(8,yoa);
                
                n=st.executeUpdate();
                
                st=con.prepareStatement("Insert into login values(?,?,?)");
                st.setString(1,un);
                st.setString(2,pw);
                st.setString(3,"3");
                n=st.executeUpdate();
            }


        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(st!=null)
            {
                try
                {
                    st.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            if(con!=null)
            {
                try
                {
                    con.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        return n;

    }


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.