如何使用 java 将 sql 结果集字段存储到单独的数组变量中?

How to store sql resultset fields in to a separate array variable using java?

我有一个 table orderinfo 的三个字段,在我的数据库中有大约 2500 行。

现在我想将每个字段数据存储在一个数组变量中。

当我 运行 代码时,我将 Null 值存储在数组变量中。

下面我提供了我的代码。

谁能帮我实现这个目标?

提前致谢。

package com;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import com.mysql.jdbc.exceptions.MySQLSyntaxErrorException;



public class getOrderinfo {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://10.10.10.14/opsbank-ii";
    static final String USER = "root";
    static final String PASS = "p@ssw0rd";
    public static int row_count = 0;
    public static int count_for_totalfiles = 0;
    public static String filename_allocated = "";
    public static String dateofcar_allocated = "";
    public String row_data = "";
    public static int orderid = 0;
    public static int[] orderid_sto = new int[5000];
    public static String flow = "";
    public static String[] flow_sto = new String[5000];
    public static Date dateofprocessing;
    public static Date[] dateofprocessing_sto = new Date[5000];

    public static void main(String args[]) {

        //public static void main(String[] args) 
        Connection conn = null;
        Statement stmt = null;
        try {
            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter the Date(Format : 2016-02-22) ");
            String date = scanner.next();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
            Date date2 = null;
            /* try {
             //Parsing the String
             date2 = (Date) dateFormat.parse(date);
             } 
             catch (ParseException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             }*/
            System.out.println("Input Date:" + date2);

            //STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            //STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;

            sql = "select orderid,flow,dateofprocessing from orderinfo where ordertype ='CAR' and dateofprocessing like '%" + date + "%'";
            ResultSet rs = stmt.executeQuery(sql);
            //STEP 5: Extract data from result set
            while (rs.next()) {
                int i = 0;

                orderid = rs.getInt("orderid");
                System.out.println("Order ID get : " + orderid);
                orderid_sto[i++] = orderid;
                flow = rs.getString("flow");
                flow_sto[i++] = flow;
                dateofprocessing = rs.getDate("dateofprocessing");
                dateofprocessing_sto[i++] = dateofprocessing;

                System.out.println("orderid :" + orderid + " || Flow : " + flow + " || date :  " + dateofprocessing);

                i++;
                row_count++;
                count_for_totalfiles++;
                //Display values
                //System.out.print("BOOKISSID: " + BOOKISSID);
                //System.out.print(", ISSN: " + ISSN);
                //System.out.println("\n");

            }
            System.out.println("Total Number of CAR orders found for the date : " + date2 + " = " + row_count);
            System.out.println("The Details after calculation:\n");
            for (int j = 0; j < count_for_totalfiles; j++) {
                System.out.println("I am executed number : " + j);
                System.out.println("orderid :" + orderid_sto[j] + " || Flow : " + flow_sto[j] + " || date: " + dateofprocessing_sto[j]);

            }
            // row_count=0; 
            //STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        } catch (MySQLSyntaxErrorException mysqlerr) {
            System.out.println("date issue");
        } catch (SQLException se) {
            //Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            //Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            //finally block used to close resources
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }//end finally try
        }//end try
    }//end main
}//end FirstExample

如果可能,请帮助任何人。

你应该只增加 i 一次,你正在多次增加它

您的代码:

orderid_sto[i++]=orderid;
flow=rs.getString("flow");
flow_sto[i++]=flow;
dateofprocessing=rs.getDate("dateofprocessing");
dateofprocessing_sto[i++]=dateofprocessing;

i++;

尝试像这样递增 i 一次:

    orderid_sto[i]=rs.getInt("orderid");
   flow_sto[i]=rs.getString("flow");
   dateofprocessing_sto[i]=rs.getDate("dateofprocessing");
   i++;

并删除这些行,因为它们出现在 finally 块中:

   //STEP 6: Clean-up environment
   stmt.close();
   conn.close();