In a ResultSet from either Oracle,MySQl or MSSQL, how can you make the resultset for loop quicker?
while (rs.next()) {
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
rs.getString(i)
}
}
This works great as long as you don't have to many columns, but as soon as you have for example 90 columns and 100 000 rows, then this for loops goes trough 100 000 rows 90 times.
One thing to try would be to just get the column count once:
int colCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 0; i < colCount; i++) {
rs.getString(i)
}
}
Related
I am using JDBC to run a SQL query in Java. I want to take the result of the query and store it in an arraylist so that I can display the data in a graph of some sort. I'm getting the same line printing out the same number of times as columnCount. Here is my code.
ArrayList <String[]> result = new ArrayList<String[]>();
int columnCount = rset.getMetaData().getColumnCount();
if(rset!=null)
{
while(rset.next())
{
found=true;
String[] row = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
row[i] = rset.getString("Date") + " "
+ rset.getString("Hour");
System.out.println(row[i]);
}
result.add(row);
}
Your second row[i] rewrites the value of the column. Just remove it and you'll see your records:
...
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
}
System.out.println(Arrays.toString(row));
result.add(row);
...
I'm currently trying to fill an array list with an object of String[] with a SQL table. I was able to get the headers of my tables using this code.
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
int i = 1;
while (colCount >= i) {
String header = rsmd.getColumnName(i);
field.add(header);
i++;
However, when I'm trying to create my rows to fill the table, the array is not adding to the array list.
while (rs.next()) {
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
}
} catch (SQLException ex) {
}
System.out.println(data.size());
For some reason when I put the "data.add(row);" statement in the for loop, it only shows the same row however many times it loops. But it doesn't work outside of the for (I want to put it after the for loop inside the while). I'm outputting the size of the list to the console and it just returns 0.
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
Dear my friends, why did you decided to write "j <= colCount" meanwhile you had created "j = 0". So your code is going to throw "SqlException". Because index of last element of result set is "colCount", but your loop passed index equals to j + 1. it means equal "colCount + 1".
As you know, you try catch SQLException but you don't do anything. So your code has been failed when first row of resultset and j = colCount + 1. It can't add anything.
//Using MySql Database. creating a search query for mobile phone from IMEI No. //in swing based desktop application
int coulmn_count=rsmd.getColumnCount();
DefaultTableModel dtm=new DefaultTableModel();
Vector column_name=new Vector();
Vector data_rows=new Vector();
for (int i = 1; i <=coulmn_count; i++) {
column_name.addElement(rsmd.getColumnName(i));
}
dtm.setColumnIdentifiers(column_name);
JOptionPane.showMessageDialog(null,+coulmn_count);
while(rs.next())
{
for (int j = 0; j <coulmn_count; j++) {
data_rows=new Vector();
data_rows.addElement(rs.getString(j));
}
dtm.addRow(data_rows);
}
//JOptionPane.showMessageDialog(null,+coulmn_count);
table.setModel(dtm);
The ResultSet is 1 based, not 0 based:
It looks like you got the code correct for the heading names:
for (int i = 1; i <=coulmn_count; i++) {
column_name.addElement(rsmd.getColumnName(i));
}
But then you use 0 based for the column data.
for (int j = 0; j <coulmn_count; j++) {
data_rows=new Vector();
data_rows.addElement(rs.getString(j));
}
Fix the code to start the index from 1.
I have designed a Jtable using Netbeans GUI in Swing.Now as per my requirement i have to display database table values into jtable.
Here is my code ..
Statement statement = (Statement) con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = (ResultSetMetaData) resultSet.getMetaData();
DefaultTableModel dtm = new DefaultTableModel();
int columns = metaData.getColumnCount();
Vector Column_Name = new Vector();
Vector data_rows = new Vector();
for (int i = 1; i < columns; i++) {
Column_Name.addElement(metaData.getColumnName(i));
}
dtm.setColumnIdentifiers(Column_Name);
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
}
dtm.addRow(data_rows);
jTable1.setModel(dtm);
resultSet.close();
Why is this code displaying only one row of my database into jtable when there so many rows in the table.
Please help me to solve this.
Each time you overwrite you data, because you have added the row just one time, which is currently after the while loop
So try to move the adding of row data in the loop like:
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
dtm.addRow(data_rows);
}
I am just curious as to how that is done. I am writing a small program to get a better understanding of two dimensional arrays. I want to know how I can go though each row and then each column separately using for loops.
Lets say I have a 2D array that is made out of different letters. I want to go through each row and each column and check if a certain letter is there. Then I want it to print how many occurrences of this letter happened in each row and then each column.
First index is row and second index is column.
Assuming that the something[][] is an something[] of something-rows (that is something[i] gives us a row, not a column - if it'S the way round, just change the examples):
public static void loopExample (String[][] someTwoDimArray) {
// Looping rows
for (var row = 0 ; row < someTwoDimArray.length ; row++) {
for (var col = 0 ; col < someTwoDimArray[0].length ; col++) {
System.out.println(someTwoDimArray[row][col]);
}
}
// looping columns
for (var col = 0 ; col < someTwoDimArray[0].length ; col++) {
for (var row = 0 ; row < someTwoDimArray.length ; row++) {
System.out.println(someTwoDimArray[row][col]);
}
}
}
I don't know if the first or second index is considered rows or columns, but this is a pretty standard nested loop for iterating over every element of a 2d array.
for(int column = 0; column < array.length(); ++column) {
for(int row = 0; row < array[column].length(); ++row) {
// do stuff to array[column][row]
}
}
Given your update, let's look for the letter 'N', in a 2d char array called myLetters.
int counter = 0;
for(int i = 0; i < myLetters.length(); ++i) {
for(int j = 0; j < myLetters[i].length(); ++j) {
if('N' == myLetters[i][j]) {
++counter;
}
}
}
System.out.println("N occurs " + counter + " times.");
if you have a 2D array if you want to access each cell you will have to use a nested for loop.
eg:
for(int i = 0; i < length1; i++)
for(int j = 0; j<length2; j++){
// do something
to format column first do array[i][j] = //do something
to format row first do array[j][i] = // do something
}
"I tried using a for loop however i dont have a good understanding of for loops and i was wondering how not just go through array in its entirety but small bits like rows and columns"
A for loop is a java control flow statement. It lets you initialize a variable (i and j) it gives you a condition (i
int i = 0
while (i < length1){
//do something
i++
}
if working with arrays for loops are almost always required.