gpt4 book ai didi

java - 为什么控制台给我 "Unable to find jdbc"

转载 作者:行者123 更新时间:2023-11-29 19:54:01 27 4
gpt4 key购买 nike

我正在 Eclipse 中开发一个应用程序。那是maven项目。我想使用 phpMyAdmin MySqlDatabase。我在 src-main-resources 中创建了一个 META-INF 文件夹,并在该文件夹中创建了 context.xml 文件。这是我的 context.xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/database?useUnicode=true&amp;characterEncoding=utf8&amp;"
username="root" password="" maxActive="20" maxIdle="10" maxWait="-1" />
</Context>

在 src-main-java 中,我制作了一个包,在其中制作了 DAO 类,这是我的 DAO 类:

public class OsobaDAO {
private static String INSERTOSOBA = "INSERT INTO osoba (ime, prezime, JMBG) VALUES (?, ?, ?)";
private static String DELETEOSOBA = "DELETE FROM osoba WHERE idosoba = ?";
private static String SELECTBYID = "SELECT * FROM osoba WHERE idosoba = ?"; private static String GETLASTNOSOBA = "SELECT * FROM osoba ORDER BY idosoba DESC LIMIT ? ";
private DataSource ds;

public OsobaDAO(){
try {
InitialContext cxt = new InitialContext();
if ( cxt == null ) {
}
ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/mysql" );
if ( ds == null ) {

}
} catch (NamingException e) {
e.printStackTrace();
}
}

public void insertOsoba(Osoba osoba){
Connection con = null;
java.sql.PreparedStatement pstm = null;
try {
con = ds.getConnection();
pstm = con.prepareStatement(INSERTOSOBA);
pstm.setString(1, osoba.getIme());
pstm.setString(2, osoba.getPrezime());
pstm.setString(3, osoba.getJMBG());
pstm.execute();
} catch (SQLException e) {
System.out.println("Error connecting!");
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

此外,我有一个 jsp 文件,我正在其中测试从 DAO 插入的此方法:

<% OsobaDAO od = new OsobaDAO();

Osoba o = new Osoba("Pera", "Peric", "1111111111111");

// poziv metode
od.insertOsoba(o);
%>

当我运行程序时出现此错误:

INFO: Server startup in 5862 ms
javax.naming.NameNotFoundException: Name [jdbc/mysql] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at dao.OsobaDAO.<init>(OsobaDAO.java:25)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:123)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:745)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Nov 22, 2016 2:00:13 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/DaoWithMaven] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at dao.OsobaDAO.insertOsoba(OsobaDAO.java:38)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:128)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:745)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

有人可以帮助我吗?

最佳答案

@Atletica 使用此链接将会有很大帮助,如果答案可以,请投票 https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

关于java - 为什么控制台给我 "Unable to find jdbc",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742686/

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