繁体   English   中英

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

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

我有一个表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

如果可以的话,请帮助任何人。 db的屏幕截图

您应该只递增一次,而多次递增

您的代码:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM