This code works and it get data from databasae. Take a look at the output and you will see the issue? How can I format this data so cols are nicly display inline. I dont want to use complex table or dataset to output my result. I just want to put "\t" between each cols.
here is the output:
ID FIRSTNAME LASTNAME USERNAME AGE
2 Dave Bill DaveB 30
3 Jane Mark JaneM 37
4 Voo Gorge VooG 50
public class ex02 {
public static void main(String[] args) {
String url = "jdbc:ucanaccess:Database11.accdb";
Connection con;
Statement stmt;
String query = "Select * from user";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
for (int i = 1; i <= columns; i++) {
System.out.print(rsmd.getColumnLabel(i) + "\t");
}
System.out.println();
// Display columns data from database
while (rs.next()) {
for (int i = 1; i <= columns; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println();
}
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}