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 = "jdbcdbc:*****";
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);
}
}
************************
************************
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 = "jdbcdbc:*****";
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);
}
}
************************