JDBC - 以List的形式检索整个数据

时间:2013-01-31 13:40:10

标签: java database jdbc

我正在尝试构建一个从MySQL表中检索每一行并将其存储到List中的类,但是我的代码没有编译并抛出错误

  

错误:在课程中找不到主要方法   MySQLAccountsDatabankReader,   请将main方法定义为:public static void main(String []   参数)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class MySQLAccountsDatabankReader {

    private static List<String[]> accounts;

    public MySQLAccountsDatabankReader(String[] args) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "mysql";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM test.accounts;");

            if (rs.next()) {
                ArrayList<String[]> accounts = new ArrayList<String[]>();
                rs = st.executeQuery("SELECT * FROM test.accounts;");
                int columnCount = rs.getMetaData().getColumnCount();
                while (rs.next()) {
                    String[] row = new String[columnCount];
                    for (int i = 0; i < columnCount; i++) {
                        row[i] = rs.getString(i + 1);
                    }
                    accounts.add(row);

                            }
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(MySQLAccountsDatabankReader.class
                    .getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(MySQLAccountsDatabankReader.class
                        .getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
        return accounts;

    }
}

修订版0.2 - 我有以下编译错误 - 线程“main”中的异常java.lang.Error:未解决的编译问题:     帐户无法解决     帐户无法解析为变量

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MySQLAccountsDatabankReader {

    public static void main(String[] args) {

        MySQLAccountsDatabankReader reader = new MySQLAccountsDatabankReader();

        List<String[]> accounts = reader.getAccounts();

    }

    public List<String[]> getAccounts() {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "mysql";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM test.accounts;");

            if (rs.next()) {

                rs = st.executeQuery("SELECT * FROM test.accounts;");
                int columnCount = rs.getMetaData().getColumnCount();
                while (rs.next()) {
                    String[] row = new String[columnCount];
                    for (int i = 0; i < columnCount; i++) {
                        row[i] = rs.getString(i + 1);
                    }
                    accounts.add(row);

                }
                return accounts;

            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(MySQLAccountsDatabankReader.class
                    .getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(MySQLAccountsDatabankReader.class
                        .getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }

    }

}

2 个答案:

答案 0 :(得分:1)

编译器错误通常是错误的一个很好的指标 - 您需要main方法。

此外,非声明性语句需要在方法,构造函数或静态初始化器中,而不是在类代码块中。将JDBC代码放在方法中。在这里,您需要一个返回accounts以匹配return语句的方法。您还需要创建List的实例,例如ArrayList返回。

private List<String[]> getAccounts() {
   List<String[]> accounts = new ArrayList<String[]>();
   ...

   return accounts;
}

并添加main方法入口点:

public static void main(String[] args) {

   MySQLAccountsDatabankReader reader = new MySQLAccountsDatabankReader();
   List<String[]> accounts = reader.getAccounts();
   ...
}

答案 1 :(得分:0)

添加主要方法:

public static void main(String [] args) {
  new MySQLAccountsDatabankReader(args);
}