Ниже приведен код, который я использую, и он отлично работает, если запускать его напрямую через основная функция в классе Java.
Код: Выделить всё
private Connection getConnection(String username, String password) { //creates the connection to server for a specific database
Connection conn = null;
String url = "jdbc:postgresql:///DormSystem"; // url of server, postgresql is the server type, IP is the server ip with the port its on and adds on the database specified
try {
// Connect to PostgreSQL database
conn = DriverManager.getConnection(url, username, password); // actually connects using the url above and username and password for admin acc
System.out.println("Connected to the PostgreSQL server successfully."); // console test to prove connection succeded
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
Ниже ошибка отображается на веб-странице сервера при запуске кода.
Код: Выделить всё
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Cannot invoke "java.sql.Connection.prepareStatement(String)" because "conn" is null
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "conn" is null
Login.SQLInterfacing.CreateUser(SQLInterfacing.java:36)
Login.CreateUserServel.doPost(CreateUserServel.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:555)
javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.96
Обратите внимание: я учусь на последнем курсе университета и ненавижу этот модуль, ориентированный на сервис. и облачные вычисления.
Подробнее здесь: https://stackoverflow.com/questions/793 ... on-is-null
Мобильная версия