0% found this document useful (0 votes)
6 views

ExcelToDatabase Code

Uploaded by

Govind Malpani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ExcelToDatabase Code

Uploaded by

Govind Malpani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java JDBC + Excel to Database Code

SQL Table Creation

CREATE TABLE timetable (


id INT AUTO_INCREMENT PRIMARY KEY,
comp_code VARCHAR(20),
course_no VARCHAR(20),
course_title VARCHAR(100),
sec VARCHAR(10),
instructor VARCHAR(100),
room VARCHAR(50),
monday VARCHAR(100),
tuesday VARCHAR(100),
wednesday VARCHAR(100),
thursday VARCHAR(100),
friday VARCHAR(100),
saturday VARCHAR(100)
);

Java Code to Read Excel and Insert into DB

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.sql.*;

public class ExcelToTimetableDB {


public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";
String excelFilePath = "/mnt/data/TImetable database final.xlsx";

try (Connection connection = DriverManager.getConnection(jdbcURL, username,


password);
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream)) {

Sheet sheet = workbook.getSheetAt(0);


String sql = "INSERT INTO timetable (comp_code, course_no, course_title,
sec, instructor, room, " +
"monday, tuesday, wednesday, thursday, friday, saturday) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);

for (Row row : sheet) {


if (row.getRowNum() == 0) continue; // Skip header row
Java JDBC + Excel to Database Code

statement.setString(1, getCellValue(row.getCell(0)));
statement.setString(2, getCellValue(row.getCell(1)));
statement.setString(3, getCellValue(row.getCell(2)));
statement.setString(4, getCellValue(row.getCell(3)));
statement.setString(5, getCellValue(row.getCell(4)));
statement.setString(6, getCellValue(row.getCell(5)));
statement.setString(7, getCellValue(row.getCell(6)));
statement.setString(8, getCellValue(row.getCell(7)));
statement.setString(9, getCellValue(row.getCell(8)));
statement.setString(10, getCellValue(row.getCell(9)));
statement.setString(11, getCellValue(row.getCell(10)));
statement.setString(12, getCellValue(row.getCell(11)));

statement.executeUpdate();
}

System.out.println("Excel data has been inserted into the database.");


} catch (Exception e) {
e.printStackTrace();
}
}

private static String getCellValue(Cell cell) {


if (cell == null) return "";
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim();
case NUMERIC:
return String.valueOf((int) cell.getNumericCellValue());
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return "";
}
}
}

You might also like