Solved

displaying tables using sql

Posted on 2002-04-04
5
156 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
Comment Utility
 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
Comment Utility
Can u just specify the error if any u are getting
0
 

Author Comment

by:star5
Comment Utility
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
Comment Utility
 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now