编译代码时java:32:错误:表达式的非法启动 [英] while compiling code java:32: error: illegal start of expression

查看:108
本文介绍了编译代码时java:32:错误:表达式的非法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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()
{
尝试
{
// 注册表jdbc驱动程序
Class.forName( oracle.jdbc.driver.OracleDriver);

// 建立连接
con = DriverManager.getConnection (
jdbc:oracle:thin:@localhost:1521:xe system 系统);

// create jdbc prepaedstatement
ps = con.PreparedStatement (
从$ b中选择
empno,ename,job,sal,
$ b emp wher empno =?
);
}
catch (例外e)
{
e.printStackTrace();
}
} // init

< span class =code-keyword> public
void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
try
{
// 常规设置
PrintWriter pw = res.getWriter();
res.getContentType( text / html);

// 读取表单数据
int no = Integer.ParseInt(req.getParameter( teno));

// 将参数值设置为sql查询
ps。 setInt( 1 ,no);

// 执行sql查询
ResultSet rs = ps .ExecuteQuery();

// 处理结果集
if (rs.next())
{
pw.println( < br>员工没有 + rs.getInt( 1 ));
pw.println( < br>员工Nmae + rs.getString(< span class =code-digit> 2
));
pw.println( < br> Employee desg + rs.getString(< span class =code-digit> 3 ));
pw.println( < br>员工薪资 + rs.getString(< span class =code-digit> 4 ));
} // 如果
else
{
pw.println( < br>没有找到员工);
}
} // 尝试
catch (例外e)
{
e.printStackTrace();
}
} // doGet

< span class =code-keyword> 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();
} // 尝试
catch (例外e)
{
e.printStackTrace();
}

尝试
{
if (con!= null)
con.close();
} // 尝试
catch (例外e)
{
e.printStackTrace();
}
} // destroy()
}
} // class

解决方案

所以我努力查看你的代码。

你使用的是正确的IDE吗?看起来不像。



请使用ECLIPSE或NETBEANS。

两者都是免费的。两个人都会告诉你你正在创造的混乱。



  public   class  dbsrv  extends  HttpServlet { //   open class  
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletExcepton,IOException { // open functionget
Connection con;
PreparedStatement ps;
public void init(){ // 打开另一个函数initINSIDEget???


} // 函数结束init
} // 函数结束get


} // 课程结束


您已在 PreparedStatement 中为 empno 建议了一个变量值,但您还没有添加了一个具有所需实际值的参数。请参阅 http://docs.oracle.com/javase/7/docs/api /java/sql/PreparedStatement.html [ ^ ]。


此代码段位于代码的开头,稍后还有

  public   void  doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletExcepton,IOException
{



我不是Java程序员,但这看起来很奇怪。



您的SQL语句中也有拼写错误。

 emp wher empno =?

应该是

 emp 其中 empno =?


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

解决方案

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.

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


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[^].


This code snippet is in the beginning of the code and also later on

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.

emp wher empno=?

should be

emp where empno=?


这篇关于编译代码时java:32:错误:表达式的非法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆