我正在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&characterEncoding=utf8&"
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)
有人可以帮助我吗?
答案 0 :(得分:0)
@Atletica使用此链接会有很大帮助,如果答案还可以,请投票https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
答案 1 :(得分:0)
假设您将应用程序部署为war文件,可以将名为application.xml的XML文件添加到以下目录中:
$ CATALINA_BASE / CONF /卡塔利娜/本地主机/ application.xml中
在此文件中,您可以按如下方式配置数据库:
<?xml version="1.0" encoding="UTF-8"?><Context path="/application"><Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/database?autoReconnect=true" username="root" password="test" maxActive="20" maxIdle="10" maxWait="-1" /> </Context>
仅使用此配置,application.war就可以使用此池。如果您希望在服务器范围内使用该池,则应在server.xml中对其进行配置。
答案 2 :(得分:0)
也许这是你的问题:
在$ CATALINA_BASE / conf / [enginename] / [hostname] /目录中的单个文件(扩展名为“.xml”)中。上下文路径和版本将从文件的基本名称(文件名减去.xml扩展名)派生。 此文件将始终优先于Web应用程序的META-INF目录中打包的任何context.xml文件。