複数のSQL文を実行し、その出力をExcelファイルに書きたいとします。 私はこのためにいくつかのコードを書かれているが、実行している間に、これはエラーにJava複数のSQL文を実行してExcelに書き込む
java.sql.BatchUpdateExceptionをスロー: のexecuteUpdate()またはexecuteLargeUpdate()を経由してSELECTを発行することはできません。 com.mysql.jdbc.SQLError.createBatchUpdateException(SQLError.java:1158)com.mysql.jdbc.StatementImpl.executeBatchで でcom.mysql.jdbc.StatementImpl.executeBatchInternal(StatementImpl.java:1049) (StatementImplで。java:959) at Demo.DatabaseQry(Demo.java:133)at Demo.main(Demo.java:48)原因:によって:java.sql.SQLException:executeUpdate()または を介してSELECTを発行できませんexecuteLargeUpdate )。 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)at com.mysql.jdbc.SQLError.createSQLException(SQLError。 Javaの:885) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)で com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1504) で com.mysql.jdbc.StatementImplで.executeBatchInternal(StatementImpl.java:1023) ... 3もっと
マイコードがある...
public static void DatabaseQry(String ipAdd, String dbNm, String dbUnm, String dbPasswd)
{
String ipAddress, dbName, dbUserId, dbPassword, fileLocation;
Connection conn = null;
Statement stmt = null;
try {
ipAddress = ipAdd;
dbName = dbNm;
dbUserId = dbUnm;
dbPassword = dbPasswd;
BufferedReader brd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Output File location: ");
fileLocation = brd.readLine();
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/" + dbName, dbUserId, dbPassword);
// Execute a query
System.out.println("Working on Query...");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//String TotalWorkflow = "SELECT COUNT(*) AS TOTAL_WORKFLOW FROM M_OBJECT WHERE TYPE_ IN(7)";
String TotalAssets = "SELECT COUNT(*) AS TOTAL_ASSET FROM M_OBJECT WHERE TYPE_ IN(1,2,3)";
String TotalJobs = "SELECT COUNT(*) AS TOTAL_JOBS FROM M_OBJECT WHERE TYPE_ IN(6)";
String TotalTasks = "SELECT COUNT(*) AS TOTAL_TASKS FROM M_OBJECT WHERE TYPE_ IN(5)";
conn.setAutoCommit(false);
//stmt.addBatch(TotalWorkflow);
stmt.addBatch(TotalAssets);
stmt.addBatch(TotalJobs);
stmt.addBatch(TotalTasks);
//Workflow total count
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS TOTAL_WORKFLOW FROM M_OBJECT WHERE TYPE_ IN(7)");
rs.last();
stmt.executeBatch();
conn.commit();
rs.last();
System.out.println("Batch executed");
// Excel file generation code
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Arial");
// font.setColor(IndexedColors.DARK_BLUE.getIndex());
font.setBold(true);
HSSFCellStyle style = workbook.createCellStyle();
style.setBorderTop((short) 6); // double lines border
style.setBorderBottom((short) 2); // single line border
style.setFont(font);
style.setFillPattern(CellStyle.BORDER_DASH_DOT);
sheet.createFreezePane(0, 1); // Freeze 1st Row sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)
// Extract data from result set
for (int i = 1; rs.next(); i++) {
HSSFRow row = sheet.createRow(1);
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.setRowStyle(style);
// For Workflow
rowhead.createCell(8).setCellValue("WORKFLOWS");
row.createCell(8).setCellValue(rs.getString("TOTAL_WORKFLOW"));
// For Assets
rowhead.createCell(9).setCellValue("ASSETS");
row.createCell(9).setCellValue(rs.getString("TOTAL_ASSET"));
// For Jobs
rowhead.createCell(10).setCellValue("JOBS");
row.createCell(10).setCellValue(rs.getString("TOTAL_JOBS"));
// For Tasks
rowhead.createCell(11).setCellValue("TASKS");
row.createCell(11).setCellValue(rs.getString("TOTAL_TASKS"));
//row.createCell(1).setCellValue(rs.getString("EXCEPTION_COUNT_"));
}
// FileOutputStream fileOut = new FileOutputStream(filename);
FileOutputStream fileOut = new FileOutputStream(fileLocation + "\\Readings.xls");
workbook.write(fileOut);
fileOut.close();
System.out.println("Excel file has been generated");
// Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
System.out.println("Unable to make connection with database...!");
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
System.out.println("\n\nPress \"Enter\" to exit...\n");
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
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
System.out.println("Execution Completed");
}
エラーメッセージは非常に明確でなければなりません。 stmt.executeBatch()を削除します。代わりに、すべてのステートメントを単独で実行し、結果セットを覚えておく必要があります。 – Frank
@Frankそれでは、executeBatch()を削除した後、上記の文をどのように実行すればよいですか? – Peter