2 Replies - 219 Views - Last Post: 08 October 2018 - 04:05 PM

#1 rulfas2018   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 18

Can't retrieve data from Database.

Posted 07 October 2018 - 01:01 PM

/* The task is to receive input from user, storing input in database, and with a click, retrieve data into web page. I can connect to the database and store data, but I can't retrieve it. Any help is welcome!! */

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1 align="center">Employee Form</h1>
<form action="aJBean.jsp" method="POST">
<table border="1" align="center">
<tbody>
<tr>
<td>Employee ID</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="first" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Department</td>
<td><input type="text" name="dept" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" />
</td>
</tr>

</tbody>
</table>
</form>
</body>
</html>

/* This is the JavaClass where I have getters and setters, connection to database and storing and trying to retrieve data into the web page displayed. */


package org.bean;

//All packages that I considered necessary for the proper connection to the database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//JavaClass that contains "getters" and "setters"
public class AJavaClass {

PreparedStatement ps = null;
ResultSet resultset = null;
Connection con;
Statement statement = null;
private String id;
private String first;
private String last;
private String dept;
ResultSet rs;
String query;
String sqlString;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}


public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}

//This method was succesful on the task of connecting to the database and store the data in it.
public int store() throws ClassNotFoundException, SQLException{

Class.forName("oracle.jdbc.driver.OracleDriver");

con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "matrix", "trinity");
ps = con.prepareStatement("insert into empp(id,first,last,dept)values(?,?,?,?)");
ps.setString(1, id);
ps.setString(2,first);
ps.setString(3, last);
ps.setString(4, dept);

int a = ps.executeUpdate();

if(a==1){
return a;
}else{
return a;
}

}

//This method was created with the intention of retrieving the records from the database but it was not
//succesful.
public ResultSet getAll() throws SQLException{

statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

query = ("SELECT * FROM empp");
resultset = statement.executeQuery(query);

return resultset;
}

}

/* This is the JSP. I am confused in the part where I call the method from the JavaClass. */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*, java.util.*, java.util.Date, java.io.*, javax.servlet.*, javax.servlet.http.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%--jsp is set to the Java Class. In other words the Bean. By decalering the Bean from
the JSP we can get all the attributes user input in Index page with the help
of the "getters & setters"--%>
<h1 align="center">Lloyd Investment Funds</h1>
<jsp:useBean id="myBean" scope="page" class="org.bean.AJavaClass" />
<jsp:setProperty name="myBean" property="id" />
<jsp:setProperty name="myBean" property="first"/>
<jsp:setProperty name="myBean" property="last" />
<jsp:setProperty name="myBean" property="dept" />
<table border="1" align="center" cellpadding="5">
<thead>
<tr bgcolor="#A52A2A">
<h2 align="center">Database Employee Records</h2>
</tr>
</thead>
<tbody>
<tr>
<td><b>ID</b></td>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
<td><b>Department</b></td>
</tr>
<tr><%--This section was my nemesis for this project. I tried using the properties
like the ones above but with the "getProperties"; I tried to call the default
constructor from the Java Class and it didn't work. I tried creating a method
to execute a statement by connecting to Database but I couldn't make it work.
I was able to connect to the database and store the data but not retrieving the
data from it.--%>
<td><%=myBean.getId()%></td>
<td><%=myBean.getFirst()%></td>
<td><%=myBean.getLast()%></td>
<td><%=myBean.getDept()%></td>
</tr>
</tbody>
</table>

<%
int a = myBean.store();
if(a==1){
out.print("Elements Stored");
}else{
out.print("Elements Not Stored");
}
%>


</body>
</html>

Is This A Good Question/Topic? 0
  • +

Replies To: Can't retrieve data from Database.

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3601
  • View blog
  • Posts: 16,467
  • Joined: 20-September 08

Re: Can't retrieve data from Database.

Posted 07 October 2018 - 01:15 PM

Please use code tags as you were asked. andrewsw's Posted Image instructions will show you how to use code-tags. It isn't difficult.
EACH JAVA FILE should have its own code tags
Was This Post Helpful? 1
  • +
  • -

#3 rulfas2018   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 18

Re: Can't retrieve data from Database.

Posted 08 October 2018 - 04:05 PM

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1 align="center">Employee Form</h1>
        <form action="aJBean.jsp" method="POST">
            <table border="1" align="center">
                <tbody>
                    <tr>
                        <td>Employee ID</td>
                        <td><input type="text" name="id" /></td>
                    </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="first" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="last" /></td>
                    </tr>
                    <tr>
                        <td>Department</td>
                        <td><input type="text" name="dept" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="submit" />
                            </td>
                    </tr>
                    
                </tbody>
            </table>
        </form>
    </body>
</html>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*, java.util.*, java.util.Date, java.io.*, javax.servlet.*, javax.servlet.http.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%--jsp is set to the Java Class. In other words the Bean. By decalering the Bean from
        the JSP we can get all the attributes user input in Index page with the help
        of the "getters & setters"--%>
        <h1 align="center">Lloyd Investment Funds</h1>
        <jsp:useBean id="myBean" scope="page" class="org.bean.AJavaClass" />
        <jsp:setProperty name="myBean" property="id" />
        <jsp:setProperty name="myBean" property="first"/>
        <jsp:setProperty name="myBean" property="last" />
        <jsp:setProperty name="myBean" property="dept" />
                <table border="1" align="center" cellpadding="5">
            <thead>
                <tr bgcolor="#A52A2A">
                    <h2 align="center">Database Employee Records</h2>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><b>ID</b></td>
                    <td><b>First Name</b></td>
                    <td><b>Last Name</b></td>
                    <td><b>Department</b></td>
                </tr>
                <tr><%--This section was my nemesis for this project. I tried using the properties
                    like the ones above but with the "getProperties"; I tried to call the default 
                    constructor from the Java Class and it didn't work. I tried creating a method
                    to execute a statement by connecting to Database but I couldn't make it work.
                    I was able to connect to the database and store the data but not retrieving the
                    data from it.--%>
                    <td><%=myBean.getId()%></td>
                    <td><%=myBean.getFirst()%></td>
                    <td><%=myBean.getLast()%></td>
                    <td><%=myBean.getDept()%></td>
                </tr>
            </tbody>
                </table>
                
                    <%
                        int a = myBean.store();
                        if(a==1){
                            out.print("Elements Stored");
                        }else{
                            out.print("Elements Not Stored");
                        }
                    %>
        
                    
    </body>
</html>


package org.bean;

//All packages that I considered necessary for the proper connection to the database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//JavaClass that contains "getters" and "setters"
public class AJavaClass {
    
    PreparedStatement ps = null;
    ResultSet resultset = null;
    Connection con;
    Statement statement = null;
    private String id;
    private String first;
    private String last;
    private String dept;
    ResultSet rs;
    String query;
    String sqlString;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    
    public String getFirst() {
        return first;
    }    
    public void setFirst(String first) {
        this.first = first;
    }
    public String getLast() {
        return last;
    }
    public void setLast(String last) {
        this.last = last;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    
    //This method was succesful on the task of connecting to the database and store the data in it.
    public int store() throws ClassNotFoundException, SQLException{
        
        Class.forName("oracle.jdbc.driver.OracleDriver");
        
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "matrix", "trinity");
        ps = con.prepareStatement("insert into empp(id,first,last,dept)values(?,?,?,?)");
        ps.setString(1, id);
        ps.setString(2,first);
        ps.setString(3, last);
        ps.setString(4, dept);
        
        int a = ps.executeUpdate();
        
        if(a==1){
            return a;
        }else{
            return a;
        }
        
    }
    
    //This method was created with the intention of retrieving the records from the database but it was not 
    //succesful.
    public ResultSet getAll() throws SQLException{
        
            statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            
            query = ("SELECT * FROM empp");
            resultset = statement.executeQuery(query);
            
        return resultset;
    }
    
}



View Postg00se, on 07 October 2018 - 01:15 PM, said:

Please use code tags as you were asked. andrewsw's Posted Image instructions will show you how to use code-tags. It isn't difficult.
EACH JAVA FILE should have its own code tags



Thank you "g00se" for letting me know how to post my code. I am new to Dream in Code is new to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1