code_practice14
code_practice14

Reputation: 31

JDBC/JFC Connection Null everytime

I have a Login.xhtml that calls a SurveyData.java bean method (toWelcome) I am using another class Connect to create the connection. In the SurveyData.java class, the method getConnection() that toWelcome() calls is creating an object of Connect to create a connection. When I put a main around this connect() method in Connect class, it works fine, I get the connection ID and successful connection. But when I call the method from getConnection() in SurveyData class, the line Statement statement = con.createStatement() throws an exception error. I think a null pointer exception because con is null. If I take the createStatement line out it functions. But obviously there is no connection.

On a side note, I took out the Class.forName() method to load the driver because I was having weird inconsistencies and it was connecting without the driver loading statement. The odd thing is the driver comes from a class that came from IntelliJ's drivers under database properties. And when I try to to load it using Class.forName() I get a class not found. I have set it to the class/driver of the database in properties.

Anyways, heres some code. I'll only include relevant code.

Login.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
-->
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>UHCL Course Evaluation</title>
    <link rel="shortcut icon" type="image/x-icon" href="#{resource['UHCL.png']}"/>
</h:head>

<h:body>
    <h:form>
        <h:panelGrid id = "panel" columns = "2" border = "1"
                     cellpadding = "10" cellspacing = "1">

            <f:facet name = "header">
                <h:outputText value = "Login"/>
            </f:facet>
            <h:outputLabel value = "Username" />
            <h:inputText  value = "${surveyData.user}"/>
            <h:outputLabel value = "Password" />
            <h:inputSecret value = "${surveyData.pass}"/>

            <f:facet name = "footer">
                <h:panelGroup style = "display:block; text-align:center">
                    <h:commandButton id = "submit" value = "Submit" action = "#{surveyData.toWelcome}"/>
                </h:panelGroup>
            </f:facet>
        </h:panelGrid>
    </h:form>
</h:body>

</html>

SurveyData.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
import java.sql.*;
import java.sql.Connection;


@SessionScoped
@ManagedBean (eager = true)
public class SurveyData implements Serializable
{
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    private String user;
    private String pass;
    private String isConnected;

    public String getIsConnected() {
        return isConnected;
    }

    public void setIsConnected(String isConnected) {
        this.isConnected = isConnected;
    }


    public String toWelcome()
    {



        try {
            Connection con = getConnection();
            Statement statement = con.createStatement();

            String createData = "Create table data" + "(course VARCHAR(15), instructor VARCHAR(30), student VARCHAR(30), answer1 VARCHAR(20)," +
                    " answer2 VARCHAR(20), answer3 VARCHAR(20), answer4 VARCHAR(20), answer5 VARCHAR(20), answer6 VARCHAR(20), answer7 VARCHAR(20), " +
                    "answer8 VARCHAR(20), answer9 VARCHAR(20), answer10 VARCHAR(20), answer11 VARCHAR(20), answer12 VARCHAR(20), answer13 VARCHAR(20), " +
                    " answer14 VARCHAR(20), answer15 VARCHAR(20), answer16 VARCHAR(20), answer17 VARCHAR(20), answer18 VARCHAR(20), answer19 VARCHAR(20), " +
                    " answer20 VARCHAR(20), answer21 VARCHAR(20), answer22 VARCHAR(20), answer23 VARCHAR(20), answer24 VARCHAR(20), answer25 VARCHAR(20)," +
                    " answer26 VARCHAR(20), answer27 VARCHAR(20), comment1 VARCHAR(20), comment2 VARCHAR(20), comment3 VARCHAR(20), comment4 VARCHAR(20)," +
                    " comment5 VARCHAR(20), comment6 VARCHAR(20), comment7 VARCHAR(20), comment8 VARCHAR(20), comment9 VARCHAR(20), comment10 VARCHAR(20)";

            return "Loading";
        } catch (Exception exception) {
            System.out.println("User or password not correct");
            return "Login";}


    }

    public Connection getConnection() {
        try {
            Connect connection = new Connect();
            Connection con = connection.connect();
            if (con != null)
                return con;
        }catch(Exception e)
        {e.printStackTrace();}

        return null;
    }

}

Connect.java

package com.project03;
import java.sql.*;

public class Connect
{
    public Connection connect(){


        Connection con = null;
        String url = "jdbc:sqlserver://localhost;instance=SQLEXPRESS; databaseName = surveyDB; user = sa; password = Thomas14";


        try {
            //Class.forName("net.sourceforge.jtds.jdbc.Driver");
            con = DriverManager.getConnection(url);
            System.out.println("Connection completed.");
            System.out.println(con);
        }
        catch (SQLException  ex) {
        System.out.println("error");
        System.out.println(ex.getMessage());
        ex.printStackTrace();

    }


            return con;

    }



}

Upvotes: 0

Views: 159

Answers (1)

code_practice14
code_practice14

Reputation: 31

I think its solved.

  1. Download the driver for your SQL. I downloaded Microsoft SQL 9.2 Driver for JDBC.
  2. Put the corresponding .jar (depending on SDK) in the Glassfish (or whatever server you are running) lib.

Upvotes: 0

Related Questions