Java 我试图在一个程序中使用两个while循环来迭代两个查询,但第一个while循环没有执行
有两个查询,我尝试使用两个while循环来迭代查询,但对我来说,我总是从数据库中获取第二个循环的数据。其中,由于第一个循环未执行,因此数据不会存储在第一个循环中。(将数据存储在excel表中)…是否有人可以编辑代码,以便为两个循环存储数据 处理第一个while循环后,将第二个executeQuery移动到。您可能正在覆盖Java 我试图在一个程序中使用两个while循环来迭代两个查询,但第一个while循环没有执行,java,jdbc,Java,Jdbc,有两个查询,我尝试使用两个while循环来迭代查询,但对我来说,我总是从数据库中获取第二个循环的数据。其中,由于第一个循环未执行,因此数据不会存储在第一个循环中。(将数据存储在excel表中)…是否有人可以编辑代码,以便为两个循环存储数据 处理第一个while循环后,将第二个executeQuery移动到。您可能正在覆盖 public class JustTest { public static void main(String[] args)
public class JustTest
{
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
String url ="";
String username ="";
String password ="";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c = DriverManager.getConnection(url,username,password);
c.setAutoCommit(false);
System.out.println("Opened database successfully");
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("total");
Row headerRow = sheet.createRow(0);
Cell date = headerRow.createCell(0);
date.setCellValue("date");
Cell byDate = headerRow.createCell(1);
byDate.setCellValue("total au");
Cell byDate1 = headerRow.createCell(1);
byDate1.setCellValue("total au_dt");
Statement stmt = c.createStatement();
//passing two queries rs and rs1
ResultSet rs = stmt.executeQuery("select empname,empno from emp");
ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");
int row = 1;
//first query iterate
while(rs.next()) {
String empname= rs.getString("empname");
int empno = rs.getInt("empno ");
Row dataRow = sheet.createRow(row);
Cell dataNameCell = dataRow.createCell(0);
dataNameCell.setCellValue(dateValue);
Cell dataAddressCell = dataRow.createCell(1);
dataAddressCell.setCellValue(empno );
row = row + 1;
}
//second query iterate
while(rs1.next()) {
String deptno=rs1.getString("deptno");
String deptname =rs1.getString("deptname ");
Row dataRow = sheet.createRow(row1);
Cell dataNameCell1 = dataRow.createCell(2);
dataNameCell1.setCellValue(deptno);
Cell dataNameCell2 = dataRow.createCell(3);
dataNameCell2.setCellValue(deptname );
row1 = row1 + 1;
}
}
catch ( NullPointerException e )
{
System.out.println( " is not updated because cells cannot be left null/empty ");
}
catch ( Exception e )
{
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
try
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\jit\\Desktop\\allSheets\\justtest.xlsx"));
workbook.write(out);
out.close();
System.out.println("sheet.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
java jdbc jdbc-odbc
你自己去整理你的代码,也许?你在重写东西。我刚刚调试过。但是第一个循环总是被跳过。你检查“emp”表中是否有数据?你的查询一定出了问题直接在数据库上执行查询,看看是否有结果现在我只得到存储在excel中的rs查询输出,但我仍然希望存储这两个查询数据..语句stmt=c.createStatement();创建另一个语句stmt2@CocoNess它没有做任何更改。但是第一个rs循环正在执行。我不确定您是否希望第二行集数据跟随同一工作表中的第一行。您在哪里声明了行1变量?我建议把第一排改成第二排
ResultSet rs = stmt.executeQuery("select empname,empno from emp");
int row = 1;
//first query iterate
while(rs.next()) {
String empname= rs.getString("empname");
int empno = rs.getInt("empno ");
Row dataRow = sheet.createRow(row);
Cell dataNameCell = dataRow.createCell(0);
dataNameCell.setCellValue(dateValue);
Cell dataAddressCell = dataRow.createCell(1);
dataAddressCell.setCellValue(empno );
row = row + 1;
}
ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");