Passing variable to public/servlet

C

Clive_

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;
%>
 
L

Lew

Clive_ said:
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";
String driver="net.sourceforge.jtds.jdbc.Driver";
String user="aaaa";
String password="user";

// 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 said:
<% while(rs.next()) { %>
<tr>
<td> <%= rs.getInt(1)%> </td>
<td> <%= rs.getString(2) %> </td>
</tr>
<br>


<% }
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.
 
C

Clive_

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
 
L

Lew

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

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.

<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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top