Sorry again
public class MemberCtrl{
// Define variables
Connection con=null;
PreparedStatement ps = null;
ResultSetters = null;
public MemberCtrl (Connection con) {
This.con=con;
}
public Member[] executeSelect()throwsSQLException{
// statement generation
Statement stmt = con.createStatement();
// run SQL
String sql = "SELECT* FROM T_MEMBER";
ResultSetters=stmt.executeQuery(sql);
*// Loop result rows
while(rs.next()){
// Record Value
int id = rs.getInt("id");
String name = rs.getString("name");
String email=rs.getString("email");
// View Results
System.out.println("id:"+id+"+"name:"+name+"+"email:"+email);
}
return;* Wrong
}
And output the results in another class
public class Toi1_db{
/**
* @paramargs
* @throws SQLException
*/
public static void main(String[]args)throws SQLException {
Connection con=(new DBC Connection()) .getConnection();
MemberCtrl memctrl = new MemberCtrl(con);
Member[] mem=memctrl.excuteSelect();
for(inti=0;i<mem.length;i++){
// Below is output processing
}
}
}
How do I run SQL and return it as a result array Member[]?
Returns the first code result as an array
Perform output processing in the second class
Who knows?
First, the public Member[]exuteSelect() method is declared to return an array of member classes.
The main() method of the Toi1_db class also states that the execution result of executeSelect of MemberCtrl is received, so there seems to be no problem with the call.
The problem is that the result of executing the SELECT statement on the database is in the ResultSet, so should I loop it as many times as I get it and put it in the Member class?
Since you have taken a line of items from ResultSet, such as name and email, you need to put them in a member and return the results that you took out repeatedly in the member array Member[].
Each member must be placed in an array, and the number of previously acquired arrays must be prepared, so if possible, it would be better to use java.util.ArrayList, which can store values dynamically.
Therefore, it would be better to use Member[] as a list first.
By the way, does the member class have id, name, and email fields?
I hope it will be helpful.
© 2024 OneMinuteCode. All rights reserved.