Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

want to copy a column from database into an array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,


Please send a reply as soon as possible.Please note I am a beginer in this field.

I,m working on java servlets to check whether a data is there in a database or not.I've written the code in servlet.The data which is to be checked is send from a HTMl page and is collected by the servlet.Now my problem is in order to check i want to copy the contents of aparticular column into an array.For this i have used the ResultSetMetaData meta = rs.getMetaData(); method.but after copyiung it to an array ,when i displayed the array no problem was there.But When i used the array for comparison with the data to be checked it shown null pointer exception.the array is a string array.The code part used by me to copy the database values into array is shown below.

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String u = request.getParameter("UserID");
String p = request.getParameter("password");

try
{
Statement statement=dbcon.createStatement();
String query="SELECT user_id FROM Employee ";
ResultSet rs=statement.executeQuery(query);
out.println("<TABLE border>");
int j=0;
while(rs.next())
{
ResultSetMetaData meta = rs.getMetaData();
int cols = meta.getColumnCount();
Object[] result = new Object[cols];
Object[] array = new Object[4];
for (int i=0; i <cols; i++)
{
result[i] = rs.getString(i + 1);
array[j] = result[i];
}
j++;

}
Regards
Sam
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite simple really. When you close the database connection (as you should with each request) the resultset goes out of scope and is destroyed making all data in it disappear.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a servlet question, moving to JDBC.

Dave
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my open source project called the FormattedDataSet API I have a couple utility classes you may find useful. The ResultSetConverter converts a ResultSet to an Object[][]. It works by passing a ResultSet to the constructor. DataAccess abstracts jdbc. Note that performance stats are kept on both classes with JAMon

 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic