• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need some help

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to be having a problem figuring out how to send in a request as an id to query the database, and give me a few fields in return, either in a single row or in a group of rows.

can someone help me, and help me do it the right way.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import java.sql.*;

public class SomeClass extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String[] someid = request.getParameterValues("someid");
Connection conn = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (ClassNotFoundException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
try
{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "pass");
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(conn)
{
try
{
Statement stmp = conn.createStatement();
ResultSet rs = stmp.executeQuery("SELECT * FROM table WHERE someid="+someid+"");
if (rs !=null) rs.close();
if (conn !=null) conn.close();
if (stmp !=null) stmp.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
doGet(request, response);
}
}
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I updated it too, any thoughts?

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import java.sql.*;

public class SomeClass implements java.io.Serializable {

private int someid=5;
private String var1 ="";
private String var2 ="";

public Category() {}

public void setRequest(HttpServletRequest req) {

Connection conn= null;
try
{
Class.forvar1("org.gjt.mm.mysql.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
}
catch (ClassNotFoundException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(conn)
{
try
{
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT var1,var2 FROM table WHERE someid='"+someid+"'");
setVar1(rs.getString("var1"));
setVar2(rs.getString("var2"));
if (rs !=null) rs.close();
if (conn!=null) conn.close();
if (statement !=null) statement.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public void setvar1(String var1) {
this.var1 = var1;
}

public String getvar1(){
return var1;
}

public void setvar2(String var2) {
this.var2 = var2;
}

public String getvar2(){
return var2;
}
}



JSP
<jsp:useBean id="Some" class="com.fh.db.SomeClass" scope="page" />
<%=Same.getVar1()%>
<%=Same.getVar2()%>
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timy McTipperstan:
Here is what I updated it too, any thoughts?

public class SomeClass implements java.io.Serializable {



First, I don't see where you are returning the data. Do you want to display the data in a JSP? I think your JDBC code may be fine, but you don't appear to be doing anything with the result set you've generated. Perhaps you didn't include that code. Also, is there some reason why you switched from an HttpServlet to the above object, which simply extends Object?
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I think my first post was too hasty. It looks like you began with a servlet and then decided put the data access code in a bean that you are using in a JSP. That's fine, but where does the request originate?

If you want user input for that "someid" field, you need a page with a form that does either a POST or a GET to a servlet or a JSP. In the doPost() or doGet() method of the servlet, you want to get your "someid" parameter from a field in the form (unless you ALWAYS want to use the integer constant 5) with request.getParameter("form_field_name"). Then create your bean using this value, execute the JDBC code, get your result set, perhaps storing it in an array and finally return to the jsp where you will display the data.

Whether or not you want to use a parameter from a form submission, you can create the bean and use it in the jsp, but you still have to process the result set somehow to display the data. How about using something from the Standard Tag Library or writing your own custom tag?
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I left a few pieces out. Someid will be taken from the URL that is passed in from the previous page. somejsp.jsp?someid=5, I put 5 in the sql because I was trying to at least be able to get something out of the rs, ahh no luck with that.

So anyway someid get passed to the jsp, the jsp passes that to the bean, the bean spins it and mixes it and spits back to the jsp the var1 and var2 from the database.

I'm not even sure if this is a good way of doing it. Any thoughts?
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timy McTipperstan:
Someid will be taken from the URL that is passed in from the previous page. somejsp.jsp?someid=5, I put 5 in the sql because I was trying to at least be able to get something out of the rs, ahh no luck with that.

I'm not even sure if this is a good way of doing it. Any thoughts?



Yes, you can do it that way, but you need to pass that value to the bean with the <jsp:setProperty> tag. Putting the parameter in the url is actually a "GET" request, so you still have to process the request object to obtain that value. Your tag should look something like this:

<jsp:useBean id="yourBeanId" scope="page" class="yourPackage.yourClass" />
<jsp:setProperty name="yourBeanId" value="*" />

The above example will work where all the names of the parameters in the query string have corresponding properties with matching names in the bean. Otherwise, you have to set each property explicitly, but the syntax for the value parameter of setProperty differs depending on if you are using the Expression Language from JSP 2.0 or scripting tags from JSP 1.2

To display the data in the JSP, you need to write some display code. You could define a property in the bean that returns a string containing the text, xml or html that you need. Then you would want the use the <jsp:getProperty> tag. Otherwise, you can try the Standard Tag Library or write your own custom tag that makes use of the data in the bean.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still nothing. no output and no errors. I dont get a drop, and I have check the query and there is data for that query.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import java.sql.*;

public class SomeClass implements java.io.Serializable {

private HttpServletRequest request = null;
private String var1="";
private String var2 ="";

public void setRequest(HttpServletRequest req) {

Connection conn = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
}
catch (ClassNotFoundException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(conn)
{
try
{
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT field1,field2 FROM table WHERE someid='5'");
String var1 = rs.getString("field1");
String var2 = rs.getString("field2");
if (rs !=null) rs.close();
if (conn !=null) conn.close();
if (statement !=null) statement.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

public String getVar1(){
return var1;
}

public String getVar2(){
return var2;
}

}


<jsp:useBean id="someclass" scope="page" class="com.fh.db.SomeClass" />

<jsp:getProperty name="someclass" property="var1" /></<br>
<jsp:getProperty name="someclass" property="var2" /><br><br>

and i tried

<%=category.getVar1()%><br>
<%=category.getVar2()%><br><br>
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timy McTipperstan:

<jsp:useBean id="someclass" scope="page" class="com.fh.db.SomeClass" />

<jsp:getProperty name="someclass" property="var1" /></<br>
<jsp:getProperty name="someclass" property="var2" /><br><br>

and i tried

<%=category.getVar1()%><br>
<%=category.getVar2()%><br><br>



If you really are using "someclass" for the id of your bean, your output scripting code needs to use that as well:

<%=someclass.getVar1()%><br>
<%=someclass.getVar2()%><br><br>

But no matter which id/name you use for the bean, and whether you use the getProperty tag or the scripting tags, you must set the value of a property before getting it. After the useBean tag, insert a setProperty tag with the value set to the request parameter.
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I guess my last post didn't make much sense in light of the fact that the properties you are displaying are set in the bean with the results of the query. My apologies.

From your code, it looks like the query never gets run because it's in the setRequest() method. How would this method ever get called? Why don't you call setRequest() from the constructor if you're not going to use a parameter from the query string for the someid value? If you do want to use the query string parameter, then you'll have to set the value of a property with it and then call setRequest() to run the query.
 
H Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, why does your setRequest() method take an HttpServletRequest parameter? It doesn't use this object and you would only be able to call this method from an object that has access to such object. A servlet does have access to the request object and is a good place to create your beans, but I don't think you're creating this bean in a servlet, are you? It's simply being instantiated in the JSP (I know, a JSP is really a servlet and it has access to the request object, but... ). Besides, you'd have to instantiate the bean and then call the setRequest() method with the request object as a parameter. Why not get any value you need from the request object and use that value to set a property in the bean instead of passing the request object itself to the bean?
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont mind me asking how would you change it. I'm pretty new, and I'm trying to do some things that I feel arent that difficult yet I seem to be missing something. I dont want to make it more difficult or bloated then it needs to be.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed some things. No change in the result.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import java.sql.*;

public class SomeClass implements java.io.Serializable {

private HttpServletRequest request = null;
private String var1 ="";
private String var2 ="";

public SomeClass () {}

public String getVar1() {

Connection conn = null;

try {
Class.forName("org.gjt.mm.mysql.Driver");
dtsConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");
}
catch (ClassNotFoundException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(conn)
{
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table WHERE someid='5'");
String var1 = rs.getString("var1");
String var2 = rs.getString("var2");
if (rs !=null) rs.close();
if (conn !=null) conn.close();
if (stmt !=null) stmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return var1;
}

}
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Timy, under the Add Reply button, when posting, there are ten buttons that can be very helpful. One of them is labeled CODE, which will put a CODE tag in your post, then you can post your code in between the tags and it will keep your formatting, meaning indentations, so that your code is more readable. Without indentations code becomes unreadable.

Thanks

Mark
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry didnt notice that.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, I would go back to your previous version. And call the setRequest() from your newly created constructor public SomeClass(), but anyway.

Let me see if I can explain on why I think you are not seeing the desired results.

When your jsp page executes the following code:

All you have done at this point is instantiated the class "com.fh.db.SomeClass" by calling the default no-arg constructor and placed the object in the page scope.

The reference to this object is whatever value you set for the id attribute in your <jsp:usbean> tag. Use this reference to your bean throughout your jsp page. For example the id above is set to "somclass", therefore ...



Also I don't believe you will need the synchronized statement on the connection object, since it is declared locally, but that is another story.

Also I would suggest you take a look at the j2eetutorialAdding JavaBeans

I hope this helps.

And if anybody out there would correct me on any incorrect statments.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, it is always a good thing to verify that data exists based on your query.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are good things to know Thanks

In another question along the same lines, how would this work.

JSP

5 being the int of someid.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two ways to set JavaBeans component properties in a JSP page: with the jsp:setProperty element or with a scriptlet




Where beanName must be the same as that specified for the id attribute in a useBean element.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is getting frustrating, I have never had so much problem, 100% my own making. I'm going to rip up what I have a start over.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake you were asking about the get method.

There are several ways to retrieve JavaBeans component properties. Two of the methods (the jsp:getProperty element and an expression) convert the value of the property into a String and insert the value into the current implicit out object:

<jsp:getProperty name="beanName" property="propName"/>
<%= beanName.getPropName() %>
For both methods, beanName must be the same as that specified for the id attribute in a useBean element, and there must be a getPropName method in the JavaBeans component.

If you need to retrieve the value of a property without converting it and inserting it into the out object, you must use a scriptlet:

<% Object o = beanName.getPropName(); %>


Note the differences between the expression and the scriptlet; the expression has an = after the opening % and does not terminate with a semicolon, as does the scriptlet.



This was taken from the j2eetutorial.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THIS WORKS !!!

java


jsp


THIS DOESNT WORK.

adding the db information in there and ripping out the static if code.



the two ResultSet's are ones that I moved in and out they werent in there at the same time. my thought is that since the first set of code works with me passing in the id from the jsp then since I havent changed that code then it should be fine in the sql statement, and just to verify that I replace the someid var with the actual varable string I am passing in.

Only thing I can see is a Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException.
 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fixed.

had to add while (rs.next()) around the rs.string, not sure why since I was only querying for one row.

had to move the close block outside of the rs.next block.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic