[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Oracle JSP (Store in Array)

Posted on 2006-06-05
6
Medium Priority
?
360 Views
Last Modified: 2010-04-01
This JSP works fine, but I have to set the String to a max number....is there anyway that I don't have to set the String to number?? I want to be able to store the outputs of the queries into arrays...

----------------User.jsp----------------------------
<%
Connection connection = null;
String errorcode = "SUCCESS";
int i = 0;
String[] userString = new String[20];

 try {

 String driverName = "oracle.jdbc.driver.OracleDriver";    

      Class.forName(driverName);
   
        // Create a connection to the database
        String serverName = "localhost";
        String portNumber = "1521";
         String sid = "OracleTest";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "SYSTEM";
        String password = "test";
        connection = DriverManager.getConnection(url, username, password);

        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT Username FROM USERS");

        while (rs.next())
      {
            userString [i] = rs.getString("Username");      
                  i++;
      }

%>

<html>
<body>
<%=userString[1]%>
</body>
</html>

----------------------User.jsp---------------------------
0
Comment
Question by:kak39
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 16838680
Hi,

Try this

<%
Connection connection = null;
String errorcode = "SUCCESS";
int i = 0;
String[] userString = null; // set the NULL first

 try {

 String driverName = "oracle.jdbc.driver.OracleDriver";    

     Class.forName(driverName);
   
        // Create a connection to the database
        String serverName = "localhost";
        String portNumber = "1521";
         String sid = "OracleTest";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "SYSTEM";
        String password = "test";
        connection = DriverManager.getConnection(url, username, password);

        Statement stmt = connection.createStatement();
       
        // you need to get the count first
        int counter = 0;
        ResultSet rs = stmt.executeQuery("SELECT count(*) as counter FROM USERS");
        if (rs.next()) {
             counter = rs.getInt("counter");
        }

        userString = new String[counter];

        ResultSet rs = stmt.executeQuery("SELECT Username FROM USERS");

        while (rs.next())
     {
          userString [i] = rs.getString("Username");    
                i++;
     }

%>

David
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16838683
Opppss typo.

There is duplicate of ResultSet rs, it will cause error.

        ResultSet rs = stmt.executeQuery("SELECT count(*) as counter FROM USERS");
        if (rs.next()) {
             counter = rs.getInt("counter");
        }

        userString = new String[counter];

        rs = stmt.executeQuery("SELECT Username FROM USERS");
0
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 16839727
try use ArrayList ? like:


<%@ page import="java.util.*" %>
<%
Connection connection = null;
String errorcode = "SUCCESS";
int i = 0;

//String[] userString = new String[20];

ArrayList userString = new ArrayList();

 try {

 String driverName = "oracle.jdbc.driver.OracleDriver";    

     Class.forName(driverName);
   
        // Create a connection to the database
        String serverName = "localhost";
        String portNumber = "1521";
         String sid = "OracleTest";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "SYSTEM";
        String password = "test";
        connection = DriverManager.getConnection(url, username, password);

        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT Username FROM USERS");

        while (rs.next())
     {
          userString.add(rs.getString("Username"));
     }

%>

<html>
<body>
<%
for(int i=0; i < userString.size(); i++) {
   out.println( userString.get(i).toString() );
 }
%>
</body>
</html>

hope this helps too
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 3

Expert Comment

by:runa_paathak
ID: 16839916
Use ArrayList as suggested by ryancys, that is the best solution. Although the solution suggested by suprapto45 works, it is not efficient in that you have to make to 2 calls to the database: first to find the number of records and then to actually get the records.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16839941
Correct,

Please use the arraylist solution as suggested by ryancys. Nice point ryancys. I was rushed and never thought that ArrayList fit the solution at best ;)

David
0
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 16840280
cheers, we all just try to help here ; )
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2

829 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