Results 1 to 6 of 6
  1. #1
    eclipse3.4ide is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default how to store the data in data base

    hi,
    i read data from excel file to console. i want to store this data in to db. can anyone please help me...here is the code...
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    public class ControllerServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    try {
    MultipartRequest multi = new MultipartRequest(req, ".",
    5 * 1024 * 1024);
    // Show which files we received
    Enumeration files = multi.getFileNames();
    while (files.hasMoreElements()) {
    String name = (String) files.nextElement();
    String filename = multi.getFilesystemName(name);
    //String type = multi.getContentType(name);
    //File f = multi.getFile(name);
    // read workbook file
    readWorkbook(filename);
    }
    } catch (Exception e) {
    e.printStackTrace(out);
    }
    }
    private static void readWorkbook(String filename) throws Exception {

    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row;
    HSSFCell cell;

    int rows; // No of rows
    rows = sheet.getPhysicalNumberOfRows();
    int cols = 0; // No of columns
    int tmp = 0;
    // This trick ensures that we get the data properly even if it doesn't start from first few rows
    for(int i = 0; i < 10 || i < rows; i++) {
    row = sheet.getRow(i);
    if(row != null) {
    tmp = sheet.getRow(i).getPhysicalNumberOfCells();
    if(tmp > cols) cols = tmp;
    }
    }
    for(int r = 0; r < rows; r++) {
    row = sheet.getRow(r);
    if(row != null) {
    for(int c = 0; c < cols; c++) {
    cell = row.getCell((short)c);
    if(cell != null) {
    //System.out.println(cell);
    printCellValue(cell);
    }
    }
    }
    }
    }
    private static void printCellValue(HSSFCell c) {

    int cellType = c.getCellType();
    if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
    System.out.println(c.getBooleanCellValue());
    } else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
    System.out.println(c.getNumericCellValue());
    } else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
    System.out.println(c.getCellFormula());
    } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
    System.out.println(c.getStringCellValue());
    } else if (cellType == HSSFCell.CELL_TYPE_ERROR) {
    System.out.println(c.getErrorCellValue());
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Search on the Google about Java database. You have to learn few steps about how to do this.

    On what DB are you looking to do this, Excel, SQL, etc....

  3. #3
    eclipse3.4ide is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    i want to store it in mysql.....can you give me the procedure.....

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all you need to connect MySQL server in to your application. JDBC drivers are better.

    You have to execute relevant queries into the database.

    Handle the data-set rerun by the executed query.

    Suns' tutorial has a grate explanation on this.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    In a nutshell, you need to look at JDBC (Java Database Connection). I've written a Java database tutorial, which may also help you (it focusses on MySQL).

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 05:10 PM
  2. Problems with Jtable connected to data base
    By elizabeth in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 05:24 PM
  3. Replies: 1
    Last Post: 08-06-2007, 06:38 PM
  4. 2 threads sharing a data base connection
    By Ed in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 05:41 AM
  5. Connection to data base
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 06-07-2007, 05:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •