2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Dupron
0

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;

    }


}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.