Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Passing variable to public/servlet

Thread view: 
Clive_ - 12 Nov 2007 16:15 GMT
Hi,

Does anyone know an example showing how to pass a database variable to
a public variable??

I would like to run a java file or JSP, passing <%= rs.getInt(1)%> to
a servlet???
The servlet would store the data, which could be retrieved using
getAttribute.

Thanks

Clive

<%
String connectionUrl =("jdbc:jtds:sqlserver://localhost:1032/");
String dbName="Calibration";
String driver="net.sourceforge.jtds.jdbc.Driver";
String user="aaaa";
String password="user";

  // Establish the connection.
  Class.forName(driver).newInstance();
  con = DriverManager.getConnection(connectionUrl
+dbName,user,password);
  stmt = con.createStatement() ;
  rs = stmt.executeQuery(querySQL) ;

 ResultSetMetaData rsmd = rs.getMetaData() ;

 for(int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
%>

<% } %>

</tr>

<%  while(rs.next()) { %>
   <tr>
     <td> <%= rs.getInt(1)%> </td>
     <td> <%= rs.getString(2) %> </td>
   </tr>
   <br>

<% }
 con.close();
 con = null;
%>
Lew - 12 Nov 2007 16:41 GMT
> Hi,
>
> Does anyone know an example showing how to pass a database variable to
> a public variable??

One question mark would suffice.

> I would like to run a java file or JSP, passing <%= rs.getInt(1)%> to
> a servlet???

A JSP is a servlet, so you've already done that.  What do you need the value for?

> The servlet would store the data, which could be retrieved using
> getAttribute.

Do you mean in a request attribute?  Where do you intend to store the data?

You can set request attributes with the request setAttribute() method.  Check
out the Javadocs.

> <%

Do not put Java in JSPs.  It's legal, but foolish.

>  String connectionUrl =("jdbc:jtds:sqlserver://localhost:1032/");
>  String dbName="Calibration";
[quoted text clipped - 4 lines]
>    // Establish the connection.
>    Class.forName(driver).newInstance();

You don't need to load the DB driver class again and again, and you certainly
don't need to create a throwaway instance of it.

>    con = DriverManager.getConnection(connectionUrl
> +dbName,user,password);

What if this fails?  You have no Exception handling at all.

>    stmt = con.createStatement() ;
>    rs = stmt.executeQuery(querySQL) ;

From where do you get the query?

>   ResultSetMetaData rsmd = rs.getMetaData() ;

You assign the value then never refer to it again except to drive an empty
loop.  Why?

>   for(int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
> %>

Empty loop.

> <% } %>
>
> </tr>

Where's the opening <tr> tag?  The enclosing <table>?

> <%  while(rs.next()) { %>
>     <tr>
[quoted text clipped - 6 lines]
>   con.close();
>   con = null;

What in the world do you expect to accomplish with setting con to null?  It
goes out of scope anyway, so it's a useless action.

> %>

There are tags in the JSTL for SQL access that likely would be much easier for
you.  JSPs should do their action through separate server-side classes or tag
libs so that all this Java scriptlet doesn't cruft up your presentation artifacts.

You've already got the value in a servlet, so I am not clear what else you
need to accomplish.

Signature

Lew

Clive_ - 13 Nov 2007 09:50 GMT
Hi Lew,

All my web development, etc was done using VB, eVB & Javascript.

In VB
Private Sub Test_Click()

   'Declare variables
   Dim i As Integer
   Dim X As Integer
   Dim Y As Integer

   Dim ColumnNames As String
   Dim ColumnValues As String

   'Code to Auto Increment the Primary Key
   Dim AutoNumber As Integer
   Dim Identity As ADOCE.Recordset
   Set Identity = CreateObject("ADOCE.Recordset.3.0")
   Identity.Open "SELECT SiteID FROM HealthInspForm ORDER BY SiteID
DESC", CN
   AutoNumber = CInt(Identity(0)) + 1
   Identity.Close

   'check to see if there's any editing in progress
   If RS.EditMode <> adEditNone Then
       MsgBox "There's editing in progress.  Try the operation again
later."
       Exit Sub
   End If

   SiteName = Var2
  X = Var19
  Y = Var20

   'Add the new record
   RS.AddNew
   RS("SiteName") = SiteName
   RS("Easting") = X
   RS("Northing") = Y

   'Ask user if they want the record added
   If MsgBox("Do you wish to add this record?", vbYesNoCancel) =
vbYes Then
       RS.Update
   Else
       RS.CancelUpdate
       MsgBox "No new record added."
   End If

   'Query the database again to refresh the grid
   RS.Requery

   'Remove existing data from grid
   For i = 1 To GridCtrl1.Rows
       GridCtrl1.RemoveItem 0
   Next

   'Set the Grid columns equal to the field count
   GridCtrl1.Cols = RS.Fields.Count

   'Get the column names
   For i = 0 To RS.Fields.Count - 1
       ColumnNames = ColumnNames & RS.Fields(i).name & vbTab
   Next

   'Add the column headers to the grid
   GridCtrl1.AddItem ColumnNames

   'Loop through the recordset
   While Not RS.EOF

       'Get the column values for this row
       For i = 0 To RS.Fields.Count - 1
           ColumnValues = ColumnValues & RS.Fields(i).Value & vbTab
       Next

       'Add the column values to the row
       GridCtrl1.AddItem ColumnValues

       'Set ColumnValues to a zero length string
       'so it can be refilled with the next row
       ColumnValues = ""

       RS.MoveNext
   Wend

End Sub

I am new to Java and would like to find a simple example that shows/
explains the process. There are hundreds of VB examples that explain
the basics. I have not found a simple working example for JSP +
servlets. Once I have a working example then it is easier to
understand the javadocs.

Thanks

clive
Lew - 13 Nov 2007 13:37 GMT
> All my web development, etc was done using VB, eVB & Javascript.
>
[quoted text clipped - 3 lines]
> servlets. Once I have a working example then it is easier to
> understand the javadocs.

<http://java.sun.com/javaee/5/docs/tutorial/doc/>

The Sun tutorials are usually first place to look.

Marty Hall's books on Web development with Java are worthwhile also.
<http://www.coreservlets.com/>

Signature

Lew

Chris ( Val ) - 13 Nov 2007 14:00 GMT
> > All my web development, etc was done using VB, eVB & Javascript.
>
[quoted text clipped - 7 lines]
>
> The Sun tutorials are usually first place to look.

Yes, it is a good place to start.

> Marty Hall's books on Web development with Java are worthwhile also.
> <http://www.coreservlets.com/>

Yes - I like his book a lot, as it is very good
for a beginner to intermediate level.

I have been slowly working my way through his
tutorials, and I'm waiting for volume 2 of his book.

--
Chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.