gpt4 book ai didi

java - getConnection() 和 ResultSet 中的错误

转载 作者:行者123 更新时间:2023-12-01 13:46:23 24 4
gpt4 key购买 nike

我从 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 (?,?,?,?)";

最佳答案

您导入了错误的类

javax.activation.DataSource vs javax.sql.DataSource

java.beans.Statement vs java.sql.Statement

关于java - getConnection() 和 ResultSet 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com