Introduction
This article’s main content is showing about how to solve a problem by the appearance of an error message when submitting a JSF form. It appears in the output tab of the NetBeans IDE. Actually, the error message exist as in the title of the article as follows :
13:16:01,334 SEVERE [com.mycompany.web.db.DBConnection] (default task-1) null: java.sql.SQLException: No suitable driver found for postgresql://localhost:5432/employee
Furthermore, the JSF form where triggering the error exist as part of the Java Web Application running using Primefaces framework. The Java Web Application is following the setting and configuration in this link, this link and also this link. It is an article with the title of ‘How to Create maven-based Web Application in NetBeans IDE’, ‘How to Add JSF Library to a maven-based Web Application in NetBeans IDE’, and ‘How to Add Primefaces Library to a maven-based Java Web Application in NetBeans IDE’ respectively. Before going on to the detail of the solving problem, the following is the content of the JSF form in a file with the name of ‘basic-input-view-db.xhtml’ :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <head> <title>TODO supply a title</title> <meta name="viewport" content="width-device-width, initial-scale=1.0"> </head> <body> <div>TODO content</div> <div class="card"> <h:form> <h5>Form User with only Basic InputText</h5> <h5>Hello, <p:outputLabel for="username" value="#{basicInputTextDBView.username}"></p:outputLabel></h5> <p:inputText id="username" value="#{basicInputTextDBView.username}"/> <p:commandButton actionListener="#{basicInputTextDBView.submit()}" value="Submit" /> </h:form> </div> <div class="card"> <p:panel header="User List"> <p:dataTable var="user" value="#{userBean.userList}"> <p:column> <f:facet name="header"> <h:outputText value="Username" /> </f:facet> <h:outputText value="#{user.username}" /> </p:column> </p:dataTable> </p> </div> </body> </html>
On the other hand, there is a Java file with the name of ‘BasicInputTextDBView.java’ which is acting as a backend bean for the ‘basic-input-view-db.xhtml’ JSF file. As for the content of that ‘BasicInputTextDBView’ exist below :
package com.mycompany.web.view; import com.mycompany.web.bean.UserBean; import com.mycompany.web.db.DBConnection; import com.mycompany.web.model.User; import java.io.Serializable; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.view.ViewScoped; import javax.inject.Named; /** * * @author Mark Spectre */ @Named(value = "basicInputTextDBView") @ViewScoped public class BasicInputTextDBView implements Serializable { private Connection connection; private Statement statement; private ResultSet resultSet; private final String TABLE_NAME = "employee"; private User user; private List userList; private String name; public BasicInputTextDBView() { connection = DBConnection.getConnection(); user = new User(); userList = user.getUserList(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public void submit() { try { String query = "INSERT INTO " + TABLE_NAME + "(name) " + " values(?)"; System.out.println(query); System.out.println("Name : "+name); connection = DBConnection.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query); preparedStatement.execute(); preparedStatement.close(); preparedStatement.setString(1, name); //getName()); User user = new User(); user.setUsername(name); userList.add(user); } catch (SQLException ex) { Logger.getLogger(BasicInputTextDBView.class.getName()).log(Level.SEVERE, null, ex); } } }
Solving the Error Message
Using the information exist as in the previous part, the following is the description for solving the problem. First of all, this article has a connection with the previous article. It is an article in this link with the title of ‘How to Solve Error Message Caused by: java.lang.NullPointerException: Cannot invoke “java.sql.Connection.prepareStatement(String)” because “this.connection” is null when executing Java Web Application using Primefaces Framework solve by edit URL Connection’. The title of the article is pointing out the detail of the error message which is pointing out for the solution also. The primary cause exist in the line of the Java file with the name of ‘BasicInputTextDBView.java’ specifically in the following line :
connection = DBConnection.getConnection();
The above line is getting a connection object from a method with the name of ‘getConnection()’ which exist in DBConnection class. Moreover, the DBConnection class itself exist in a file with the name of ‘DBConnection.java’. Below is the content of the ‘DBConnection.java’ file :
package com.mycompany.web.db; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Mark Spectre */ public class DBConnection { private static String url, user, password; static void init() { url = "postgresql://localhost:5432/employee"; user = "postgres"; password = "password"; } public static Connection getConnection() { init(); try { return DriverManager.getConnection(url, user, password); } catch (SQLException ex) { System.out.println("Error Connection"); Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex); ex.printStackTrace(); return null; } } }
As expected, the URL with the value of ‘postgresql://localhost:5432/employee’ is not recognizable. In the end, it will cause null value for the connection to be retrieved by the command of ‘connection = DBConnection.getConnection();’ So, the solution for getting the object connection will be exist is by changing the URL of the database connection into a right format. Change it as in the previous article before as follows :
static void init() { url = "jdbc:postgresql://localhost:5432/employee"; user = "postgres"; password = "password"; }
Just add the keyword ‘jdbc’ in front of the URL database connection definition. Just check in this link for a further reference.