0

I am trying use a from a multi-dimensional array that I create in another classes method. Below is my main method:

public class main {

public static void main(String[] args) throws Exception {

    sql test = new sql();
    String[][] test2 = test.getDb();
    System.out.print(test2[0][0]);
}

Now here is the class that returns an multi-dimensional array.

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JTextField;

import com.mysql.jdbc.Statement;

public class sql {

java.sql.Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:8889/deliveryEarn";
String user = "root";
String password = "root";

ArrayList<String> sqlCol1 = new ArrayList<String>();
ArrayList<String> sqlCol2 = new ArrayList<String>();
ArrayList<String> sqlCol3 = new ArrayList<String>();
ArrayList<String> sqlCol4 = new ArrayList<String>();
ArrayList<String> sqlCol5 = new ArrayList<String>();
ArrayList<String> sqlCol6 = new ArrayList<String>();
ArrayList<String> sqlCol7 = new ArrayList<String>();
String sqlArray[][] = new String[7][7];

public sql() {
}

public String[][] getDb() {
    try {
         con = DriverManager.getConnection(url, user, password);
         pst = con.prepareStatement("select * from incomeCalc");
         rs = pst.executeQuery();

    while (rs.next()) {

        sqlCol1.add(rs.getString(1));
        int i1=0;
        for(String s: sqlCol1){
            sqlArray[i1++][0] = s;
        }

        sqlCol2.add(rs.getString(2));
        int i2=0;
        for(String s: sqlCol2){
            sqlArray[i2++][1] = s;
        }

        sqlCol3.add(rs.getString(3));
        int i3=0;
        for(String s: sqlCol3){
            sqlArray[i3++][2] = s;
        }

        sqlCol4.add(rs.getString(4));
        int i4=0;
        for(String s: sqlCol4){
            sqlArray[i4++][3] = s;
        }

        sqlCol5.add(rs.getString(5));
        int i5=0;
        for(String s: sqlCol5){
            sqlArray[i5++][4] = s;
        }

        sqlCol6.add(rs.getString(6));
        int i6=0;
        for(String s: sqlCol6){
            sqlArray[i6++][5] = s;
        }
        sqlCol7.add(rs.getString(7));
        int i7=0;
        for(String s: sqlCol7){
            sqlArray[i7++][6] = s;
        }
    }
    }

catch( Exception E ) { 
    System.out.println( E.getMessage() );   
}
    return sqlArray;
}
}

Here is the screenshot of the MySQL database. enter image description here

Edit: It appears I wasn't clear with my question. I apologize. I am getting a runtime error at this line:

System.out.print(test2[0][0]);

What am I doing wrong? Also, for correct OOP, is it better to use a constructor or a method to pull from or input to a database? THis is my first program so sorry if it seems trivial.

Edit2: Here is the error:

Exception in thread "main" java.lang.NullPointerException at main.main(main.java:17)

2
  • I apologize if I wasn't clear. I am getting a run time error and dont know why. Specifically at this line: System.out.print(test2[0][0]); Commented Aug 18, 2012 at 1:29
  • 1
    If you have a stack trace (the error), please post it. Commented Aug 18, 2012 at 3:10

1 Answer 1

2

As to why you've got an error, it would be nicer to know the error, however...

Personally, I'd drop the contents of the result set into a "Data Object"...

public class Income {
    // Column decelerations...

    private long id;
    private int tips;
    private int hours;
    private int gas;
    private double hourly;
    private double other;
    private double other2;

    public int getGas() {
        return gas;
    }

    public double getHourly() {
        return hourly;
    }

    public int getHours() {
        return hours;
    }

    public long getId() {
        return id;
    }

    public double getOther() {
        return other;
    }

    public double getOther2() {
        return other2;
    }

    public int getTips() {
        return tips;
    }

    public void setGas(int gas) {
        this.gas = gas;
    }

    public void setHourly(double hourly) {
        this.hourly = hourly;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setOther(double other) {
        this.other = other;
    }

    public void setOther2(double other2) {
        this.other2 = other2;
    }

    public void setTips(int tips) {
        this.tips = tips;
    }
}

Then when you load it you could do something like...

public Income[] getIncome() {

    // Call database...

    List<Income> data = new ArrayList<Income>(25);

    while (rs.next()) {

        Income income = new Income();

        income.setID(rs.getInt(1)));
        income.setTips(rs.getInt(2)));
        income.setHours(rs.getInt(3)));
        income.setGas(rs.getInt(4)));
        income.setHourly(rs.getDouble(5)));
        income.setOther(rs.getDouble(6)));
        income.setOther2(rs.getDouble(7)));

        data.add(income);

    }

    return data.toArray(new Income[data.size()]);

}

The you could do things like this...

sql test = new sql();
Income[] incomes = test.getIncome();

System.out.println(incomes[0].getID());

Isn't that easier to read :P

Your attempt to use a Factory is probably the best idea. It comes down to a matter of management as to weather you maintain a single instance (Singlton) or allow multiple instances of this Factory to be created. Personally, I prefer to use Singltons in this case where I can, it allows a centralised place to perform operations (saving changes, creating new objects, listing, deleting) and helps manage the resources involved. IMHO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.