Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
package com.ds;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class dbsrv  extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)
        throws ServletExcepton,IOException
    {
        Connection con;
        PreparedStatement ps;

        public void init()
        {
            try
            {
                //registry jdbc drivers
                Class.forName("oracle.jdbc.driver.OracleDriver");

                // Establish The Connection
                con = DriverManager.getConnection(
                            "jdbc:oracle:thin:@localhost:1521:xe","system","system");

                //create jdbc prepaedstatement
                ps = con.PreparedStatement("
                select 
                    empno, ename,job,sal, 
                from 
                    emp wher empno=?
                ");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }//init

        public void doGet(HttpServletRequest req,HttpServletResponse res)
            throws ServletException,IOException
        {
            try
            {
                //general setting 
                PrintWriter pw = res.getWriter();
                res.getContentType("text/html");

                //read form data
                int no = Integer.ParseInt(req.getParameter("teno"));

                //set param value to sql query
                ps.setInt(1,no);

                //execute the sql query
                ResultSet rs=ps.ExecuteQuery();
                
                //process the result set
                if(rs.next())
                {
                    pw.println("<br>Employee no"+rs.getInt(1));
                    pw.println("<br>Employee Nmae"+rs.getString(2));
                    pw.println("<br>Employee desg"+rs.getString(3));
                    pw.println("<br>Employee Salary"+rs.getString(4));
                }//if
                else
                {
                    pw.println("<br>NO Employee found");
                }
            }//try
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }//doGet

        public void doPost(HttpServletRequest req,HttpServletResponse res)
            throws ServletExcepton,IOException
        {
            doGet(req,res);
        }//dopost

        public void destroy()
        {
            //close jdbc objs
            try
            {
                if (ps != null)
                    ps.close();
            }//try
            catch(Exception e)
            {
                e.printStackTrace();
            }

            try
            {
                if (con != null)
                    con.close();
            }//try
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }//destroy()
    }
}//class
Posted
Updated 23-Oct-14 21:55pm
v3
Comments
Sergey Alexandrovich Kryukov 24-Oct-14 2:20am    
In what line? Please use "Improve question", format code properly (I added correct "pre" tag for you). Indicate the error message in code comment.
—SA
[no name] 24-Oct-14 2:29am    
every try{} and throws exception total code illegal start of expression
Sergey Alexandrovich Kryukov 24-Oct-14 2:54am    
Did you read my question and suggestion above? If you reply to it, you probably did? But why are you talking about something irrelevant, even to your own post. Your question tells us about compilation error, now you are talking about exceptions. You cannot have exception if you have compilation error.
—SA
[no name] 24-Oct-14 4:06am    
sorry it is compilation error
George Jonsson 24-Oct-14 4:01am    
I have not have had the pleasure to write much Java code, so I have to wonder.
Are spaces not supported in this language?
The reason for asking is that every time anyone posts Java code it is difficult to read.
(Well, at least for me)
Looks like the convention is to decrease the size of the source code file by omitting all white spaces.

So I took the effort of looking through your code.
Are you using a proper IDE? Does not look like.

PLEASE USE ECLIPSE OR NETBEANS.
Both are free. Both would have told you about the mess you're creating.

Java
public class dbsrv  extends HttpServlet{ // open class
   public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletExcepton,IOException{ //open function "get"
   Connection con;
   PreparedStatement ps;
     public void init(){ // open another function "init" INSIDE "get" ??? 


     } // end of function "init"
   }// end of function "get"


} // end of class
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Oct-14 12:20pm    
5ed.
—SA
You have suggested a variable value for empno in your PreparedStatement, but you have not added a parameter with the actual value required. See http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html[^].
 
Share this answer
 
This code snippet is in the beginning of the code and also later on
Java
public void doGet(HttpServletRequest req,HttpServletResponse res)
        throws ServletExcepton,IOException
{

I am no Java programmer, but that looks odd to me.

You also have a spelling error in your SQL statement.
SQL
emp wher empno=?
should be
SQL
emp where empno=?
 
Share this answer
 
Comments
[no name] 24-Oct-14 4:12am    
not working
George Jonsson 25-Oct-14 4:58am    
Did you get it to work now?
Richard MacCutchan 24-Oct-14 4:16am    
I am no Java programmer, but that looks odd to me.
No, perfectly valid Java.
Sergey Alexandrovich Kryukov 24-Oct-14 12:19pm    
Of course...
—SA
George Jonsson 24-Oct-14 22:37pm    
Really? Solution 3 points out the same thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900