java - 如何使用java将sql结果集字段存储到单独的数组变量中?
Posted
技术标签:
【中文标题】java - 如何使用java将sql结果集字段存储到单独的数组变量中?【英文标题】:How to store sql resultset fields in to a separate array variable using java? 【发布时间】:2016-02-23 08:46:30 【问题描述】:我有一个表 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
如果可能,请帮助任何人。
【问题讨论】:
您真的应该考虑创建和填充一个对象,然后将该对象添加到一个列表(或数组)中,而不是为每一列设置单独的数组。 【参考方案1】:尝试像这样增加一次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();
【讨论】:
【参考方案2】:你应该只增加 i 一次,你正在增加它多次
你的代码:
orderid_sto[i++]=orderid;
flow=rs.getString("flow");
flow_sto[i++]=flow;
dateofprocessing=rs.getDate("dateofprocessing");
dateofprocessing_sto[i++]=dateofprocessing;
i++;
【讨论】:
@ashish 。谢谢先生。之前的代码工作正常。以上是关于java - 如何使用java将sql结果集字段存储到单独的数组变量中?的主要内容,如果未能解决你的问题,请参考以下文章
请教高手:java中 SQL查询结果返回为List<Map<String,Object>>结果集顺序问题
java.sql.SQLException:结果集开始之前[重复]