Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Populating Drop Down List in Servlet

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers
This question might sound weird but I'm not able to find an answer. I'm trying to populate a drop down list in my servlet program but not getting a clue how to populate. And I want to populate it using servlet program itself and not JSP or Javascript or any other. Here's the sample code, have given the Post method only:

If you observe the try block, the first statement "set..." is empty. My question is what should I insert there. As it is a list I cannot insert String(setString) or any other. I have checked the API too, but din't get an answer. Do we have any method in the PreparedStatement API, that allows us to populate a list in the servlet or is there any other option for this?

Any help would be much appreciated.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just do it in a loop for every item? To improve performance, you can use PreparedStatement#addBatch() for this.

That said, your topic title and problem is chosen a bit wrongly and very consufing. I initially understood that you want to populate a dropdown the way which is usually done for display: get from DB, store in List and pass through JSP. But in this case you just wanted to save an array in a DB using JDBC API (which makes the problem completely unrelated to servlets).

Please think a bit more about it the next time.
[ December 19, 2008: Message edited by: Bauke Scholtz ]
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, sorry for the delayed reply Bauke and Thank you for replying!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,

I am not fully clear about your question....according to my understanding , I am giving you the following solution...hope you this will solve your problem....In the following example, i am using Batch updates....we have to iterate the customerNames as shown in the example.If you have any questions, reply me:

Example Code :
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial","root","root");


response.setContentType("text/html");
PrintWriter out = response.getWriter();

String[] customerName = request.getParameterValues("customerName");
String date = request.getParameter("date").trim();
int dcNo = Integer.parseInt(request.getParameter("dcNo").trim());
String componentDetails = request.getParameter("componentDetails").trim();


try{
con.setAutoCommit(false);
String sql = "INSERT into sometable VALUES(?,?,?,?)";
PreparedStatement prest = con.prepareStatement(sql);
for(int i=0;i<customerName.length;i++){
prest.set(1, customerName[i]);
prest.setString(2, date);
prest.setInt(3, dcNo);
prest.setString(4, componentDetails);
}

int count[] = prest.executeBatch();
con.commit();
con.close();
System.out.println("Added Successfully!");
}
catch (BatchUpdateException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}


Thanks,
Siva
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Siva
Thanks a ton ! I don't know a better way than this to Thank you because I was banging my head how to get my task done. The solution you have provided was the one I was exactly looking for.

Again, Thanks a bunch !
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!