Re: Passing variable to public/servlet
Clive_ wrote:
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 <tr> tag? The enclosing <table>?
<% 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.
--
Lew