Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

displaying tables using sql

Posted on 2002-04-04
5
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 20 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.
Suggested Courses

670 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