报错信息:
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "conn" is null
版本:java 8和postgresql 11.5
配置的局部数据源:
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource
name="webstoreDS"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:26000/mydb"
username="xiaoming"
password="12345678a@"
maxWaitMillis="5000"/>
</Context>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>chapter01</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>webstoreDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
dao.java:
package com.demo;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.*;
public interface Dao{
//查找并返回数据源对象
public static DataSource getDataSource() {
DataSource dataSource = null;
try {
Context context = new InitialContext();
dataSource=
(DataSource)context.lookup("java:comp/env/webstoreDS");
}catch(NamingException ne) {
System.out.println("异常:"+ne);
}
return dataSource;
}
//返回连接对象方法
public default Connection getConnection() throws DaoException {
DataSource dataSource = getDataSource();
Connection conn = null ;
try {
conn=dataSource.getConnection();}
catch(SQLException sqle) {System.out.print("异常"+sqle);}
return conn;}
}
插入的servlet:
package com.demo;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/addCustomer.do")
public class AddCustomerServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
CustomerDao dao = new CustomerDaoImpl();
Customer customer = new Customer();
String message = null;
try {
customer.setId(Integer.parseInt(request.getParameter("id")));
customer.setName(new String(request.getParameter("cname").getBytes("iso-8859-1"),"UTF-8"));
customer.setEmail(new String(request.getParameter("email").getBytes("iso-8859-1"),"UTF-8"));
customer.setBalance(
Double.parseDouble(request.getParameter("balance")));
boolean success = dao.addCustomer(customer);
if(success) {
message ="<1i>成功插入一条记录!</1i>";}
else {
message="<1i>插入记录错误!</1i>";
}
}catch(Exception e) {
System.out.println(e);
message="<li>插入记录错误!</1i>"+ e;
}
request.setAttribute("result", message);
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/addCustomer.jsp");
rd.forward (request,response);
}
}
用的是tomact,在tomact里面配置好也不行
错误的意思是连接对象为空,导致空指针异常了。
System.out.println("异常:"+ne);
定位这行catch 中的代码,控制台有输出这行代码对应的异常信息,将异常信息发出来看下
public interface Dao{
//查找并返回数据源对象
public static DataSource getDataSource() {
DataSource dataSource = null;
try {
Context context = new InitialContext();
dataSource=
(DataSource)context.lookup("java:comp/env/webstoreDS");
}catch(NamingException ne) {
System.out.println("异常:"+ne);
}
return dataSource;
}
//返回连接对象方法
public default Connection getConnection() throws DaoException {
DataSource dataSource = getDataSource();
Connection conn = null ;
try {
conn=dataSource.getConnection();}
catch(SQLException sqle) {
System.out.print("异常"+sqle);
}
return conn;
}
}