Solved

displaying tables using sql

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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

624 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