Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to copy a column from database into an array

 
Sam Alex
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
 
Jeroen Wenting
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.
 
David O'Meara
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
 
steve souza
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic