Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declare dynamic variable names

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
How do I declare dynamic variable names so I can use the people class multiple times. Is there a better approach with Java to what I am doing. Thanks.

************************
person person0 = new person(rs.getString(1));
person person1 = new person(rs.getString(1));
person person3 = new person(rs.getString(1));
....
person person[n] = new person(rs.getString(1));
************************
This is how it is done in actionscript, I just concat "person" and the variable myNumber:

["person"+myNumber] = "somedata"
*************************

//declare the person class
public class person{
String lastName;

public person(String lastName){
this.lastName = lastName;

}
}

//Then I get the data from sql and put it into the class:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int myNumber = 0;
String dbURL = "jdbc:eek:dbc:*****";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (ClassNotFoundException e) {
System.out.println("Database access failed " + e);
}
try {
Connection conn = DriverManager.getConnection(dbURL,"*****","****");
Statement stmt=conn.createStatement();
//ResultSet rs=stmt.executeQuery("select firstName from GetConReg where lastName='"+jTextField1.getText()+"'");
ResultSet rs=stmt.executeQuery("select firstName from GetConReg where lastName != ''");
while(rs.next()){
person ["person"+myNumber] = new person(rs.getString(1));
System.out.println(["person"+myNumber].lastName);
myNumber++;
}
}catch (SQLException e) {
System.out.println("Database access failed " + e);
}
}
************************
 
I can't see that compiling ...
(especially this line :person ["person"+myNumber] = new person(rs.getString(1));
)

Try adding your person objects into a class variable ArraList ...

Code:
import java.util.*;

public class Whatever {
  private ArrayList people  = new ArrayList();

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int myNumber = 0;
        String dbURL = "jdbc:odbc:*****";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch (ClassNotFoundException e) {
            System.out.println("Database access failed " + e);
        }
        try {
            Connection conn = DriverManager.getConnection(dbURL,"*****","****");
            Statement stmt=conn.createStatement();
            //ResultSet rs=stmt.executeQuery("select firstName from GetConReg where lastName='"+jTextField1.getText()+"'");
            ResultSet rs=stmt.executeQuery("select firstName from GetConReg where lastName != ''");
            while(rs.next()){
               people.add(new person(rs.getString(1)));
        System.out.println(["person"+myNumber].lastName);
                myNumber++;
            }
        }catch (SQLException e) {
            System.out.println("Database access failed " + e);
        }
    }
}
 
Here is the final code but I am getting this message:

Code:
Database access failed java.sql.SQLException: No data found
JFrame_1$person@76fba0

I know this is not a database error because when I take this line out:
Code:
people.add(myNumber, new person(rs.getString(1)));
, I do not get the error. There is no problem with databse or the data within it.

thanks

Code:
public class person{
    String lastName;
    
    public person(String lastName){
        this.lastName = lastName;
    }
}

public ArrayList people  = new ArrayList();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int myNumber = 0;
        String dbURL = "jdbc:odbc:****";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch (ClassNotFoundException e) {
            System.out.println("Database access failed " + e);
        }
        try {
            Connection conn = DriverManager.getConnection(dbURL,"******","***");
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select firstName,lastName from GetConReg where lastName like '"+jTextField1.getText()+"%'");
            while(rs.next()){
                people.add(myNumber, new person(rs.getString(1)));
                myNumber++;
            }
            
        }catch (SQLException e) {
            System.out.println("Database access failed " + e);
        }
        System.out.println(people.get(0));
      
    }
 
It is a database problem ... run this code, and you'll see :

Code:
public class person{
    String lastName;
    
    public person(String lastName){
        this.lastName = lastName;
    }
}

public ArrayList people  = new ArrayList();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int myNumber = 0;
        String dbURL = "jdbc:odbc:****";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch (ClassNotFoundException e) {
            System.out.println("Database access failed " + e);
        }
        try {
            Connection conn = DriverManager.getConnection(dbURL,"******","***");
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select firstName,lastName from GetConReg where lastName like '"+jTextField1.getText()+"%'");
            while(rs.next()){
               System.out.println("about to request data ... " +myNumber);
                String qqq = rs.getString(1);
                System.out.println("Data - " +myNumber +" : " +qqq);
                people.add(myNumber, new person(qqq));
                myNumber++;
            }
            
        }catch (SQLException e) {
            System.out.println("Database access failed " + e);
        }
        System.out.println(people.get(0));
      
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top