-1

我正在尝试构建一个从 MySQL 表中检索每一行并将其存储到 List 的类,但我的代码无法编译并引发错误

错误:在 MySQLAccountsDatabankReader 类中找不到主方法,请将主方法定义为:public static void main(String[] args)

    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 - 我有以下编译错误 - 线程“主”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);
            }
        }

    }

}
4

2 回答 2

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();
   ...
}
于 2013-01-31T13:44:00.743 回答
0

添加主要方法:

public static void main(String [] args) {
  new MySQLAccountsDatabankReader(args);
}
于 2013-01-31T13:46:57.900 回答