permaculture playing cards*
The moose likes JDBC and the fly likes Populating Drop Down List in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Populating Drop Down List in Servlet " Watch "Populating Drop Down List in Servlet " New topic
Author

Populating Drop Down List in Servlet

Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
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.


Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Mar 14, 2008
Posts: 107
Oops, sorry for the delayed reply Bauke and Thank you for replying!
siva adabala kumar
Greenhorn

Joined: Dec 20, 2008
Posts: 1
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

Joined: Mar 14, 2008
Posts: 107
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 !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating Drop Down List in Servlet
 
Similar Threads
Getting NullPointerException
MySQLSyntaxErrorException
SQLException:Column count doesn't match value count at row 1
jsp insert into database error(java.lang.NumberFormatException: For input string: "")
Facing Problems with DataBase