How to add elements dynamically in database using jdbc ?


You are giving static input to prepared statemnt.What if you dont know who will use how many user are entered.


import java.sql.*;
import java.util.Scanner;

public class Main
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","password-1");



            Scanner sc=new Scanner(System.in);
    PreparedStatement ps=con.prepareStatement("insert into sandeep values(?)");

            System.out.println("Enter how many details you want to enter");
    int n=sc.nextInt();

    for(int i=1;i<5;i++)
    {
   String s=sc.next();
   ps.setString(1,s);
   ps.execute();
    }


}




#How to add elements dynamically in database using jdbc

Comments

Popular posts from this blog

NumPy Codes and import

Spring container and sequential steps

Deploy your web app to tomcat in war format