Solved

displaying tables using sql

Posted on 2002-04-04
5
162 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 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
intellij error 2 56
Html Table Looping (part 2) 5 41
Bot application - advice 3 80
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

739 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