Solved

displaying tables using sql

Posted on 2002-04-04
5
159 Views
Last Modified: 2013-11-24
i have this java servlet code ,it compiles ok,receives
the valu ok, but do not display the values of the table
whats wrong with the sql statment ???

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Training extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res)
                                     throws ServletException, IOException {
 
         Connection con = null;          
         boolean logged = false;
         res.setContentType("text/html");
         PrintWriter out=res.getWriter();
       
       try {
       
            String strSQL, EmpID=null;
            ResultSet rs=null;
            Statement stmt;
           
            // get parameters passed from html
            EmpID = req.getParameter("userid");
               
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:db1");
            stmt = con.createStatement();
                out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"6\" BORDER=\"1\">");                                                                              
             out.println("<TR><TD>EmpNO.</TD><TD>Course Name</TD><TD>Start Date</TD><TD>End Date</TD><TD>Place</TD></TR>");                      
             
            strSQL = "select * from Training" +
                 "where EMPNO like '"+ EmpID + "'";
            rs = stmt.executeQuery(strSQL);
                 
           
                while(rs.next())                                                      
         {                                                                      
           out.println("<TR>");                                                              
           out.println("<TD>" + rs.getString("EmpNO") + "</TD>");              
            out.println("<TD>" + rs.getString("Course_name") + "</TD>");
             out.println("<TD>" + rs.getString("Start_date") + "</TD>");
           out.println("<TD>" + rs.getString("End_date") + "</TD>");
            out.println("<TD>" + rs.getString("Place") + "</TD>");
           out.println("</TR>");                                              
        }                                    
              rs.close();
               stmt.close();

       } catch (Exception e)
         {
           e.printStackTrace();
       }
         finally
         {
            if (con != null)
               {
                 try
                     {
                      con.close();
                 } catch (SQLException se) {
                      se.printStackTrace(System.err);
                 }
            }
       }
         
  }    

}
0
Comment
Question by:star5
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 6919317
 What's the error message you are getting? It seems to me that you need to leave a space before the WHERE clause:

strSQL = "select * from Training" + "where EMPNO like '"+ EmpID + "'";

  is done:

strSQL = "select * from Training" + " where EMPNO like '"+ EmpID + "'";

  Hope it helps.
0
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6920142
Can u just specify the error if any u are getting
0
 

Author Comment

by:star5
ID: 6920527
i will give u the points qirionis but a small problem
it displays the start date like that 1997-01-12 00:00:00
where those zeros come from and how to delete them???
0
 
LVL 35

Accepted Solution

by:
girionis earned 5 total points
ID: 6921752
 I guess it is a standard format of the DATE column of the database. Most of the columns just expect a standard form of DATE (yyyy/mm/dd hh:mm:ss). If it is not there then they fill it with trailing zeros. I guess that's what's happening to you as well. I am not sure how you can change it. You could either change the format of the database column itself or tokenize the String and get the first token only. If the String that holds the "1997-01-12 00:00:00" is named "date" (i.e. date = "1997-01-12 00:00:00", then you could do something like:

StringTokenizer st = new StringTokenizer(date);
String newDate = st.nextToken();

  Then the "nextDate" variable will hold the 1997-01-12" value. You will have to import the java.util.StringTokenizer class.

  Hope it helps.

  By the way, what was the problem the first time?
0
 
LVL 1

Expert Comment

by:jose_tijo
ID: 6922433
Hi
  Aftree you retrieve the value you can use the DateFormat class to format the returning string.

SimpleDateFormat formatter=new SimpleDateFormat("MM/dd/yyyy");
String formattedDate=formatter.format(<your string>);

Tijo
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
Java Jpanels and Jframe 8 32
Configure a Bean in an XML file 4 31
iterator/ListIterator approach 17 25
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question