Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL query in a java bean

Posted on 2004-03-20
14
Medium Priority
?
353 Views
Last Modified: 2012-05-04
My SQL Query is:
ResultSet RS             = SQLStmt.executeQuery("SELECT * FROM links;");

Question:
If I pass a string into the method (called SQLVar for example), how do I pass this into my SQL statement so i have something like this:

ResultSet RS             = SQLStmt.executeQuery("SELECT * FROM links where description like SQLvar;");

Thanks,

Matt
0
Comment
Question by:Ma7T
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10641827
ResultSet RS           = SQLStmt.executeQuery("SELECT * FROM links where description like '"+SQLvar+"';");
0
 

Author Comment

by:Ma7T
ID: 10641831
Also - if I create a session bean, how do i discard the session - for example is a user logs out?
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 10641832
or use a PreparedStatement:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM links where description like ?");
ps.setString(1, SQLVar);
ResultSet RS = ps.executeQuery();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 10641836
> if I create a session bean, how do i discard the session

session.invalidate();
0
 

Author Comment

by:Ma7T
ID: 10641994
Hello again Objects.
I think your code works - unfortunately when I ran it I started getting an exception, so i put all my code back how it was and I can't seem to get rid of the exception.  Can you cast your expert eye over it - I can't seem to find where the error lies:

try
            {
                  ObjConn = DriverManager.getConnection(url, "user", "password");
                    SQLStmt = ObjConn.createStatement();
                ResultSet RS             = SQLStmt.executeQuery("SELECT * FROM database;");
                ResultSetMetaData OutputMD      = RS.getMetaData();
                  
                iNumColumns             = OutputMD.getColumnCount();
                                                
                        while(RS.next())
                        {
                              Vector Vectortemp = new Vector();
                              
                              for(int i=1;i<=iNumColumns;i++)
                              {
                                    Vectortemp.addElement(RS.getObject(i));
                              }
                              
                              VectorA.addElement(Vectortemp);
                              }
                              
                        SQLStmt.close();
                        RS.close();
                        ObjConn.close();
                   }

Any ideas.

(i've upped the points because you've been so helpful)
0
 
LVL 92

Expert Comment

by:objects
ID: 10642010
what errors u getting?
0
 

Author Comment

by:Ma7T
ID: 10642023
I'm not sure - I've got a crude way of checking:

catch(SQLException ConnectEx)
            {
                  iNumColumns = 32;
                     System.err.println(ConnectEx.getMessage());
                   }

I return iNumColumns to the JSP page and output it to the screen.  

All i know is iNumColumns = 32

0
 
LVL 92

Expert Comment

by:objects
ID: 10642092
>   System.err.println(ConnectEx.getMessage());

what does that line print to log?

> SQLStmt.close();
> RS.close();
> ObjConn.close();

order should be:

RS.close();
SQLStmt.close();
ObjConn.close();

0
 

Author Comment

by:Ma7T
ID: 10642148
If i browse to tomcat/logs where i assume the logs are there are about 5 different log files whih contain loads of data.  So i've moved them into a file called temp so that when i reload the page it creates a new log file.  But this doesn't happen.

How do i find the particular log of this event?
0
 
LVL 92

Expert Comment

by:objects
ID: 10642173
what version of tomcat?

did u change the ordering of the close?
0
 

Author Comment

by:Ma7T
ID: 10642184
Yep, changed the order.
Version of tomcat is 5.0.16

The only thing that i have changed since it was working was the page that the method is called from but i can't see his having any effect on the method throwing an exception.
0
 
LVL 92

Expert Comment

by:objects
ID: 10642209
you could try getting rid of that ; at the end of the sql statement.

not sure what 5.0 does with its logs and it probably depends on your setup anyway.
if it helps the log for my local tobcat webapp in 4.1 is localhost_log.yyyy-mm-dd.txt
0
 

Author Comment

by:Ma7T
ID: 10642246
Fixed it!!!!
dont worry i was being an idiot.

It was further above in the code, i had :
ClassforName("var")
instead of ClassforName(var)

Thanks a lot for your help though, I found the log file and it was that that pointed me to my stupid mistake.

0
 
LVL 92

Expert Comment

by:objects
ID: 10642256
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question