如何使用java将sql resultset字段存储到单独的数组变量中?
我有表orderinfo的三个字段,数据库中大约有2500行 现在我想将每个字段数据存储在一个数组变量中 当我运行代码时,我得到了存储在数组变量上的空值 下面我提供了我的代码 有谁能帮我做到这一点吗 提前谢谢如何使用java将sql resultset字段存储到单独的数组变量中?,java,arrays,jdbc,resultset,Java,Arrays,Jdbc,Resultset,我有表orderinfo的三个字段,数据库中大约有2500行 现在我想将每个字段数据存储在一个数组变量中 当我运行代码时,我得到了存储在数组变量上的空值 下面我提供了我的代码 有谁能帮我做到这一点吗 提前谢谢 package com; import java.sql.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Scanner; import com.mysql.j
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一次,您正在多次增加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();
“ASHISH。谢谢SIR。早期的代码工作得很好。您应该真正考虑创建和填充对象,并将该对象添加到列表或数组中,而不是为每个列具有单独的数组。