getConnection()和ResultSet出错

时间:2013-12-03 04:01:06

标签: java

我从Netbeans那里得到了错误:

Cannot find symbol.  Symbol: method getConnection(), 
location: interface.javax.activation.DataSource.

另一个错误:

Cannot find sybmol.  Symbol: method executeQuery(java.lang.String), 
location: class.java.beans.Statement.

有人在那里指出我哪里出错了?

 public class Servlet2 extends HttpServlet {


DataSource pool;  // Database connection pool

 @Override
 public void init( ) throws ServletException {
  try {
     // Create a JNDI Initial context to be able to lookup the DataSource
     InitialContext ctx = new InitialContext();
     // Lookup the DataSource, which will be backed by a pool
     //   that the application server provides.
     pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
     if (pool == null)
        throw new ServletException("Unknown DataSource 'jdbc/TestDB'");
    } catch (NamingException ex) {
        Logger.getLogger(Servlet2.class.getName()).log(Level.SEVERE, null, ex);
    }
   }

   @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

     PrintWriter out = response.getWriter();
      Connection conn = null;
    Statement stmt = null;
     try {
        conn = pool.getConnection();
        stmt = (Statement) conn.createStatement();
        ResultSet rset = stmt.executeQuery(" INSERT PartNumber,ModelDescription,  Cost,  Qyt INTO books") + "VALUES (?,?,?,?)";

1 个答案:

答案 0 :(得分:1)

您导入了错误的类

javax.activation.DataSource vs javax.sql.DataSource

java.beans.Statement vs java.sql.Statement