Java 需要有关使用数据源连接池连接到数据库的帮助吗
我想创建一个连接到数据库的简单程序。我不知道是什么问题 这是我的密码: Products.java /接球手和接球手*/ } context.xmlJava 需要有关使用数据源连接池连接到数据库的帮助吗,java,jakarta-ee,jdbc,Java,Jakarta Ee,Jdbc,我想创建一个连接到数据库的简单程序。我不知道是什么问题 这是我的密码: Products.java /接球手和接球手*/ } context.xml <?xml version="1.0" encoding="UTF-8"?> <Context path="/grocerific"> <Resource auth="Container" driverClassname="com.mysql.jdbc.Driver" name="/p
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/grocerific">
<Resource
auth="Container"
driverClassname="com.mysql.jdbc.Driver"
name="/pool/grocerific"
maxActive="100"
maxIdle="30"
maxWait="1000"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/grocerific"
username="root"
password="secret"
/>
</Context>
我还配置了来自数据库节点的服务,扩展了数据库节点,并创建了一个新连接。我测试了它,说它成功了。我还在库中添加了库mysql-jdbc-connector.zip。我看到一个context.xml,所以我假设这是在Tomcat上运行的,可以提供非常有用的信息 Tomcat还要求您在web.xml中添加一个数据源条目——Tomcat文档非常清楚地解释了这一切
你好我现在知道了我的错误,可能程序会提示java null异常,当出现错误时,我的try-catch会被触发,提示“error”。但当我检查数据库时,没有null值。。似乎我得到了这个SQL空错误。请帮忙。
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/grocerific">
<Resource
auth="Container"
driverClassname="com.mysql.jdbc.Driver"
name="/pool/grocerific"
maxActive="100"
maxIdle="30"
maxWait="1000"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/grocerific"
username="root"
password="secret"
/>
</Context>
public static Connection getConnection() throws NamingException, SQLException{
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/grocerific");
Connection conn = ds.getConnection();
return conn;
}
GetProductsServlet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NamingException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
ProductsDao productsDao = new ProductsDao();
ArrayList products = productsDao.getAllProducts();
out.println(products);
} finally {
out.println("error!");
}
}