gpt4 book ai didi

java - 数据源不能为 null Java

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:04 26 4
gpt4 key购买 nike

我的类 ConnectionProvider 返回一个 null DataSource 对象。我不知道为什么。早期在其他程序中它始终有效。

ConnectionProvider.java

public class ConnectionProvider {

private static DataSource dataSource;

public static Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}

public static DataSource getDataSource() {
if (dataSource == null) {
try {
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext
.lookup("java:comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/znalezisko");
} catch (NamingException e) {
e.printStackTrace();
}
}

return dataSource;
}
}

UserDAOImpl.java(返回空数据源):

public class UserDAOImpl implements UserDAO {

private static final String CREATE_USER = "INSERT INTO user(username, email, password, is_active) VALUES(:username, :email, :password, :active);";

private NamedParameterJdbcTemplate template;

public UserDAOImpl() {
template = new NamedParameterJdbcTemplate(ConnectionProvider.getDataSource());
}

@Override
public User create(User user) {
User resultUser = new User(user);
KeyHolder holder = new GeneratedKeyHolder();
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
int update = template.update(CREATE_USER, paramSource, holder);
if(update > 0) {
resultUser.setId((Long)holder.getKey());
setPrivigiles(resultUser);
}
return resultUser;
}

private void setPrivigiles(User user) {
final String userRoleQuery = "INSERT INTO user_role(username) VALUES(:username)";
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
template.update(userRoleQuery, paramSource);
}

这是contex.xml:

<Context>
<Resource
name="jdbc/znalezisko"
auth="Container"
type="javax.sql.DataSource"
initialSize="10"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
username="****"
password="****"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/znalezisko" />

<Realm
className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/znalezisko"
userTable="user"
userNameCol="username"
userCredCol="password"
userRoleTable="user_role"
roleNameCol="role_name"
localDataSource="true" />
</Context>

这是控制台中的所有错误:

javax.naming.NameNotFoundException: Name [jdbc/znalezisko] 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:173) at pl.znalezisko.util.ConnectionProvider.getDataSource(ConnectionProvider.java:25) at pl.znalezisko.dao.UserDAOImpl.(UserDAOImpl.java:21) at pl.znalezisko.dao.MysqlDAOFactory.getUserDAO(MysqlDAOFactory.java:12) at pl.znalezisko.service.UserService.addUser(UserService.java:15) at pl.znalezisko.controller.RegisterController.doPost(RegisterController.java:29) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Unknown Source) mar 10, 2018 12:15:47 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [pl.znalezisko.controller.RegisterController] in context with path [/Znalezisko] threw exception java.lang.IllegalArgumentException: DataSource must not be null at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.(NamedParameterJdbcTemplate.java:91) at pl.znalezisko.dao.UserDAOImpl.(UserDAOImpl.java:21) at pl.znalezisko.dao.MysqlDAOFactory.getUserDAO(MysqlDAOFactory.java:12) at pl.znalezisko.service.UserService.addUser(UserService.java:15) at pl.znalezisko.controller.RegisterController.doPost(RegisterController.java:29) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Unknown Source)

最佳答案

它可能与拼写错误有关:contex.xml而不是context.xml

或者当您的文件的配置不在类路径 (context.xml) 中时,可能会发生这种情况。

关于java - 数据源不能为 null Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49208544/

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