This document provides an example of how to read and import data from a DB2 database in Excel. The example has two functions: reading Excel from POI, and connecting to a DB2 database and executing SQL. For your reference, I hope to enlighten you.
Have you ever encountered the need to read data from Excel? Import data to the database and save it. Finally, you can use the report to read the database and display it, which is relatively simple. I checked the information about the two, and it didn't matter. The required functions are relatively simple. As long as I can read the Excel Data, I decided to use POI and trust apache. It was really easy to use, I found a source code on the Internet, and copied it and changed it slightly to use it.
The following code is a Demo with two functions: POI reading Excel, and DB2 database connection and SQL Execution.
Import Java. io. FileInputStream; import
Java. io. IOException; import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import org. apache. poi. hssf. usermodel. HSSFCell;
Import org. apache. poi. hssf. usermodel. HSSFRow;
Import org. apache. poi. hssf. usermodel. HSSFSheet;
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
Public class POITest {private static Connection conn = null;
Private static Statement stmt = null;
Private static boolean connectDB2 ()
{String url = "";
String username = "username ";
String password = "password ";
// Load the driver to connect to the database try
{// Add the class library driver package db2jcc. jar and db2jcc_license_cu.jarClass.forName
("Com. ibm. db2.jcc. DB2Driver ");
Url = "JDBC: db2: // 192.168.0.1: 50000/dbname ";