详解poi+springmvc+springjdbc导入导出excel实例

工作中常遇到导入导出excel的需求,本獂有一简答实例与大家分享。

废话不多说,

1.所需jar包:

2.前端代码:

ieport.jsp:

 <%@page import="java.util.Date"%>
 <%@ page language="java" contentType="text/html; charset=utf-" pageEncoding="utf-"%>
 <!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://www.w.org/TR/xhtml/DTD/xhtml-transitional.dtd">
 <html xmlns="http://www.w.org//xhtml">
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-" />
   <title>导入\导出页面</title>

   <script type="text/javascript">
     function exportFile(){
       window.location.href = "<%=request.getContextPath()%>/export.go";
     }
   </script>
 </head>
 <body>
   <form action="import.go" method="post" enctype="multipart/form-data">
     文件:<input type="file" name="uploadFile"/>
     <br></br>
     <input type="submit" value="导入"/>
     <input type="button" value="导出" onclick="exportFile()"/>
   </form>
 </body>
 </html>

success.jsp:

<%@ page language="java" contentType="text/html; charset=utf-" pageEncoding="utf-"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 <!DOCTYPE html PUBLIC "-//WC//DTD XHTML . Transitional//EN" "http://www.w.org/TR/xhtml/DTD/xhtml-transitional.dtd">
 <html xmlns="http://www.w.org//xhtml">
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-" />
   <title>成功页面</title>

   <script type="text/javascript">
 //     var secUserList = '${secUserList}';
 //     alert(secUserList);
   </script>
 </head>
 <body>
   <c:if test="${type == 'import'}">
     <div>导入成功!</div>
     <c:forEach items="${secUserList}" var="secUser">
       <div>Id:${secUser.userId} | Name:${secUser.userName} | Password:${secUser.userPassword}</div>
     </c:forEach>
   </c:if>
   <c:if test="${type == 'export'}">
     <div>导出成功!</div>
   </c:if>
 </body>
 </html>

3.后台代码:

controller:

 package com.controller;
 import java.io.File;
 import java.util.List;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.servlet.ModelAndView;
 import com.domain.SecUser;
 import com.service.IEportService;
 @Controller
 public class IEportController {
   @Resource
   private IEportService ieportService;

   @RequestMapping("/import")
   public ModelAndView importFile(@RequestParam(value="uploadFile")MultipartFile mFile, HttpServletRequest request, HttpServletResponse response){
     String rootPath = request.getSession().getServletContext().getRealPath(File.separator);
     List<SecUser> secUserList = ieportService.importFile(mFile, rootPath);

     ModelAndView mv = new ModelAndView();
     mv.addObject("type", "import");
     mv.addObject("secUserList", secUserList);
     mv.setViewName("/success");
     return mv;
   }
   @RequestMapping("/export")
   public ModelAndView exportFile(HttpServletResponse response) {
     ieportService.exportFile(response);

     ModelAndView mv = new ModelAndView();
     mv.addObject("type", "export");
     mv.setViewName("/success");
     return mv;
   }
 }

service:

  package com.service;
   import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.URLEncoder;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 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.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFFont;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 import com.dao.IEportDao;
 import com.domain.SecUser;
  @Service
 public class IEportService {
   @Resource
   private IEportDao ieportDao;

   public List<SecUser> importFile(MultipartFile mFile, String rootPath){
     List<SecUser> secUserList = new ArrayList<SecUser>();

     String fileName = mFile.getOriginalFilename();
     String suffix = fileName.substring(fileName.lastIndexOf(".") + , fileName.length());
     String ym = new SimpleDateFormat("yyyy-MM").format(new Date());
     String filePath = "uploadFile/" + ym + fileName;
     try {
       File file = new File(rootPath + filePath);
       if (file.exists()) {
         file.delete();
         file.mkdirs();
       }else {
         file.mkdirs();
       }
       mFile.transferTo(file);
       if ("xls".equals(suffix) || "XLS".equals(suffix)) {
         secUserList = importXls(file);
         ieportDao.importFile(secUserList);
       }else if ("xlsx".equals(suffix) || "XLSX".equals(suffix)) {
         secUserList = importXlsx(file);
         ieportDao.importFile(secUserList);
       }
     } catch (Exception e) {
       e.printStackTrace();
     } 

     return secUserList;
   }

   private List<SecUser> importXls(File file) {
     List<SecUser> secUserList = new ArrayList<SecUser>();

     InputStream is = null;
     HSSFWorkbook hWorkbook = null;
     try {
       is = new FileInputStream(file);
       hWorkbook = new HSSFWorkbook(is);
       HSSFSheet hSheet = hWorkbook.getSheetAt();

       if (null != hSheet){
         for (int i = ; i < hSheet.getPhysicalNumberOfRows(); i++){
           SecUser su = new SecUser();
           HSSFRow hRow = hSheet.getRow(i);

           su.setUserName(hRow.getCell().toString());
           su.setUserPassword(hRow.getCell().toString());

           secUserList.add(su);
         }
       }
     } catch (Exception e) {
       e.printStackTrace();
     }finally {
       if (null != is) {
         try {
           is.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }

       if (null != hWorkbook) {
         try {
           hWorkbook.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     }  

     return secUserList;
   }

   private List<SecUser> importXlsx(File file) {
     List<SecUser> secUserList = new ArrayList<SecUser>();

     InputStream is = null;
     XSSFWorkbook xWorkbook = null;
     try {
       is = new FileInputStream(file);
       xWorkbook = new XSSFWorkbook(is);
       XSSFSheet xSheet = xWorkbook.getSheetAt();

       if (null != xSheet) {
         for (int i = ; i < xSheet.getPhysicalNumberOfRows(); i++) {
           SecUser su = new SecUser();
           XSSFRow xRow = xSheet.getRow(i);

           su.setUserName(xRow.getCell().toString());
           su.setUserPassword(xRow.getCell().toString());

           secUserList.add(su);
         }
       }
     } catch (Exception e) {
       e.printStackTrace();
     }finally {
       if (null != is) {
         try {
           is.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }

       if (null != xWorkbook) {
         try {
           xWorkbook.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     }

     return secUserList;
   }

   public void exportFile(HttpServletResponse response) {
     SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
     OutputStream os = null;
     XSSFWorkbook xWorkbook = null;
     try {
       String fileName = "User" + df.format(new Date()) + ".xlsx";

       os = response.getOutputStream();
       response.reset();

       response.setHeader("Content-disposition", "attachment; filename = " + URLEncoder.encode(fileName, "UTF-"));
       response.setContentType("application/octet-streem");

       xWorkbook = new XSSFWorkbook();
       XSSFSheet xSheet = xWorkbook.createSheet("UserList");

       //set Sheet页头部
       setSheetHeader(xWorkbook, xSheet);

       //set Sheet页内容
       setSheetContent(xWorkbook, xSheet);

       xWorkbook.write(os);
     } catch (Exception e) {
       e.printStackTrace();
     } finally {
       if (null != os) {
         try {
           os.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }

       if (null != xWorkbook) {
         try {
           xWorkbook.close();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     }

   }

   /**
   * set Sheet页头部
   * @param xWorkbook
   * @param xSheet
   */
   private void setSheetHeader(XSSFWorkbook xWorkbook, XSSFSheet xSheet) {
     xSheet.setColumnWidth(, * );
     xSheet.setColumnWidth(, * );
     xSheet.setColumnWidth(, * );

     CellStyle cs = xWorkbook.createCellStyle();
     //设置水平垂直居中
     cs.setAlignment(CellStyle.ALIGN_CENTER);
     cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
     //设置字体
     Font headerFont = xWorkbook.createFont();
     headerFont.setFontHeightInPoints((short) );
     headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
     headerFont.setFontName("宋体");
     cs.setFont(headerFont);
     cs.setWrapText(true);//是否自动换行

     XSSFRow xRow = xSheet.createRow();

     XSSFCell xCell = xRow.createCell();
     xCell.setCellStyle(cs);
     xCell.setCellValue("用户ID");

     XSSFCell xCell = xRow.createCell();
     xCell.setCellStyle(cs);
     xCell.setCellValue("用户名");

     XSSFCell xCell = xRow.createCell();
     xCell.setCellStyle(cs);
     xCell.setCellValue("密码");
   }

   /**
   * set Sheet页内容
   * @param xWorkbook
   * @param xSheet
   */
   private void setSheetContent(XSSFWorkbook xWorkbook, XSSFSheet xSheet) {
     List<SecUser> secUserList = ieportDao.getSecUserList();
     CellStyle cs = xWorkbook.createCellStyle();
     cs.setWrapText(true);

     if (null != secUserList && secUserList.size() > ) {
       for (int i = ; i < secUserList.size(); i++) {
         XSSFRow xRow = xSheet.createRow(i + );
         SecUser secUser = secUserList.get(i);
         for (int j = ; j < ; j++) {
           XSSFCell xCell = xRow.createCell(j);
           xCell.setCellStyle(cs);
           switch (j) {
             case :
               xCell.setCellValue(secUser.getUserId());
               break;
             case :
               xCell.setCellValue(secUser.getUserName());
               break;
             case :
               xCell.setCellValue(secUser.getUserPassword());
               break;
             default:
               break;
           }
         }
       }
     }
   }
 }

dao:

 package com.dao;
  import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Repository;

 import com.domain.SecUser;

 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.RowMapper;

 @Repository
 public class IEportDao {
   @Resource
   private JdbcTemplate jdbcTemplate;

   private RowMapper<SecUser> suRowMapper = null;

   private IEportDao() {
     suRowMapper = new RowMapper<SecUser>() {
       @Override
       public SecUser mapRow(ResultSet rs, int index) throws SQLException {
         SecUser secUser = new SecUser();

         secUser.setUserId(rs.getString("USER_ID"));
         secUser.setUserName(rs.getString("USER_NAME"));
         secUser.setUserPassword(rs.getString("USER_PASSWORD"));

         return secUser;
       }
     };
   }

   public void importFile(List<SecUser> secUserList) {
     try {
       String sql = "INSERT INTO SEC_USER VALUES(UUID(),?,?)";
       List<Object[]> paramsList = new ArrayList<Object[]>();
       for (int i = ; i < secUserList.size(); i++) {
         SecUser secUser = secUserList.get(i);
         Object[] params = new Object[]{secUser.getUserName(),secUser.getUserPassword()};
         paramsList.add(params);
       }

       jdbcTemplate.batchUpdate(sql, paramsList);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }

   public List<SecUser> getSecUserList() {
     List<SecUser> suList = new ArrayList<SecUser>();
     StringBuffer sb = new StringBuffer();
     sb.append("SELECT SU.USER_ID,SU.USER_NAME,SU.USER_PASSWORD FROM SEC_USER SU");

     try {
       suList = jdbcTemplate.query(sb.toString(), suRowMapper);
     } catch (Exception e) {
       e.printStackTrace();
     }

     return suList;
   }
 }

domain:

package com.domain;
  public class SecUser {
   String userId;    //用户ID
   String userName;   //用户名
   String userPassword; //密码

   public String getUserId() {
     return userId;
   }
   public void setUserId(String userId) {
     this.userId = userId;
   }

   public String getUserPassword() {
     return userPassword;
   }
   public void setUserPassword(String userPassword) {
     this.userPassword = userPassword;
   }

   public String getUserName() {
     return userName;
   }
   public void setUserName(String userName) {
     this.userName = userName;
   }
 }

4.配置文件:

 <?xml version="." encoding="UTF-"?>
 <web-app xmlns:xsi="http://www.w.org//XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app__.xsd"
   id="WebApp_ID" version=".">
   <display-name>SpringSpringmvcPoi</display-name>
   <welcome-file-list>
     <welcome-file>ieport.jsp</welcome-file>
   </welcome-file-list>

   <!-- 指定 Spring 配置文件的名称和位置 -->
   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
       classpath:application-context.xml
       classpath:dataSource-context.xml
     </param-value>
   </context-param>

   <!-- 配置启动 Spring 的 Listener -->
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <!-- 配置 SpringMVC 的 DispatcherServlet -->
   <servlet>
     <servlet-name>DispatcherServlet</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 配置 SpringMVC 的配置文件的位置 -->
     <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-mvc.xml</param-value>
     </init-param>
   </servlet>
   <servlet-mapping>
     <servlet-name>DispatcherServlet</servlet-name>
     <url-pattern>*.go</url-pattern>
   </servlet-mapping>

   <!-- 上传文件编码,防止乱码 -->
   <filter>
     <filter-name>CharacterEncodingFilter</filter-name>
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
     <init-param>
       <param-name>encoding</param-name>
       <param-value>utf-</param-value>
     </init-param>
   </filter>
   <filter-mapping>
     <filter-name>CharacterEncodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
 </web-app>
 <?xml version="." encoding="UTF-"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w.org//XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-..xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-..xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-..xsd">

   <!-- 配置自动扫描的包 -->
   <context:component-scan base-package="com.controller"></context:component-scan>

   <!-- 配置SpringMVC的视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/views/"></property>
     <property name="suffix" value=".jsp"></property>
   </bean>

   <!-- 支持上传文件 -->
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

 </beans>
<?xml version="." encoding="UTF-"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w.org//XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-..xsd">

   <context:component-scan base-package="com"></context:component-scan>

 </beans>
 <?xml version="." encoding="UTF-"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w.org//XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">
   <!-- 读取jdbc配置文件 -->
   <context:property-placeholder location="classpath:jdbc.properties" />

   <!-- 配置数据源 -->
   <bean id="dataSource" class="com.mchange.v.cp.ComboPooledDataSource" destroy-method="close">
     <property name="user" value="${jdbc.user}"></property>
     <property name="password" value="${jdbc.password}"></property>
     <property name="driverClass" value="${jdbc.driverClass}"></property>
     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

     <!-- 当连接池中的连接用完时,CP一次性创建新连接的数目 -->
     <property name="acquireIncrement" value=""></property>
     <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
     <property name="initialPoolSize" value=""></property>
     <property name="maxPoolSize" value=""></property>
     <property name="minPoolSize" value=""></property>
     <property name="maxConnectionAge" value=""></property>
     <property name="maxIdleTime" value=""></property>
     <property name="maxIdleTimeExcessConnections" value=""></property>
     <property name="breakAfterAcquireFailure" value="false"></property>
     <property name="testConnectionOnCheckout" value="false"></property>
     <property name="testConnectionOnCheckin" value="false"></property>
     <!-- 每秒检查连接池中的空闲连接 -->
     <property name="idleConnectionTestPeriod" value=""></property>
     <property name="acquireRetryAttempts" value=""></property>
     <property name="acquireRetryDelay" value=""></property>
     <property name="preferredTestQuery" value="SELECT FROM DUAL"></property>
   </bean>

   <!-- 配置Jdbc模板JdbcTemplate -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <constructor-arg ref="dataSource"></constructor-arg>
   </bean>
 </beans>
 jdbc.driverClass=com.mysql.jdbc.Driver
 jdbc.jdbcUrl=jdbc:mysql://localhost:/mydb
 jdbc.user=myuser
 jdbc.password=myuser

5.目录结构:

6.结果演示

导入:

导出:

PS:

1.本獂新手,由于还没清楚怎么添加附件,故将所有代码贴出并加上目录结构,日后了解怎么添加附件,再修改。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • sqlserverdriver配置方法 jdbc连接sqlserver

    一.下载驱动程序. 下载地址:http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe 首先下载到sql2005jdbc驱动sqljdbc_2.0.1008.2_enu.exe(版本号1.1之后都支持sql2005,2.0的支持sql2008) 下载的是一个解压就可以使用的,不需要要安装,你可以把这个包放在任何你想要放的地方,比如说你

  • 基于Java回顾之JDBC的使用详解

    尽管在实际开发过程中,我们一般使用ORM框架来代替传统的JDBC,例如Hibernate或者iBatis,但JDBC是Java用来实现数据访问的基础,掌握它对于我们理解Java的数据操作流程很有帮助. JDBC的全称是Java Database Connectivity. JDBC对数据库进行操作的流程:•连接数据库•发送数据请求,即传统的CRUD指令•返回操作结果集JDBC中常用的对象包括:•ConnectionManager•Connection•Statement•CallableStat

  • jsp+servlet+jdbc实现对数据库的增删改查

    一.JSP和Servlet的简单介绍 1.Servlet和JSP简介: Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP,Servlet是在服务器端执行的Java程序,一个被称为Servlet容器的程序(其实就是服务器) 负责执行Java程序,而JSP(Java Server Page)则是一个页面, 由JSP容器负责执行. 2.Servlet和JSP的区别: Servlet以Java程序为主, 输出HTML代码时需要使用out.println函数,也就是说Java中内

  • Spring 数据库连接池(JDBC)详解

    数据库连接池 对一个简单的数据库应用,由于对数据库的访问不是很频繁,这时可以简单地在需要访问数据库时,就新创建一个连接,就完后就关闭它,这样做也不会带来什么性能上的开销.但是对于一个复杂的数据库应用,情况就完全不同而,频繁的建立.关闭连接,会极大地减低系统的性能,因为对于连接的使用成了系统性能的瓶颈. 通过建立一个数据库连接池以及一套连接使用管理策略,可以达到连接复用的效果,使得一个数据库连接可以得到安全.高效的复用,避免了数据库连接频繁建立.关闭的开销. 数据库连接池的基本原理是在内部对象池中

  • MyEclipse通过JDBC连接MySQL数据库基本介绍

    1.前提是MyEclipse已经能正常开发Java工程 2.安装MySQL 个人使用的是版本是 mysql-5.0.22-win32.zip 网址:http://www.mysql.com/downloads/mysql/#downloads 3.下载JDBC驱动 个人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解压缩之后其中的 mysql-connector-java-5.1.22-bin.jar 网址:http://www.mysql.com/dow

  • JDBC 程序的常见错误及调试方法

    数据库:MySQL 在编写应用的时候,为了调试程序方便,可以在异常处理代码中把异常信息显示出来,这样可以根据错误提示调试代码.异常处理代码通常可以写成这样: try{ - }catch(Exception e){ System.out.println(e.toString()); } 下面是一些常见的错误信息: (1)驱动程序不存在 提示的错误信息如下: java.lang.ClassNotFoundException: com.mysql.jdbc.Drive 后半部分是您在程序中写的驱动程序

  • Java使用JDBC连接数据库的实现方法

    本文实例讲述了Java使用JDBC连接数据库的实现方法,是Java数据库程序设计里非常实用的重要技巧.分享给大家供大家参考.具体如下: JDBC(Java Data Base Connectivity)数据库连接,通常我们在编写web应用或java应用程序要连接数据库时就要使用JDBC.使用JDBC连接数据库一般步骤有: 1.加载驱动程序 Class.forName(driver); 2.创建连接对象 Connection con = DriverManager.getConnection(ur

  • Java开发Oracle数据库连接JDBC Thin Driver 的三种方法

    Oracle的jdbc驱动三种主要分类: 1.JDBC OCI: oci是oracle call interface的缩写,此驱动类似于传统的ODBC 驱动.因为它需要Oracle Call Interface and Net8,所以它需要在运行使用此驱动的JAVA程序的机器上安装客户端软件,其实主要是用到orcale客户端里以dll方式提供的oci和服务器配置. 2.JDBC Thin: thin是for thin client的意思,这种驱动一般用在运行在WEB浏览器中的JAVA程序.它不是

  • java中JDBC实现往MySQL插入百万级数据的实例代码

    想往某个表中插入几百万条数据做下测试,原先的想法,直接写个循环10W次随便插入点数据试试吧,好吧,我真的很天真.... DROP PROCEDURE IF EXISTS proc_initData;--如果存在此存储过程则删掉 DELIMITER $ CREATE PROCEDURE proc_initData() BEGIN DECLARE i INT DEFAULT 1; WHILE i<=100000 DO INSERT INTO text VALUES(i,CONCAT('姓名',i),

  • 详解JDBC使用

    什么是JDBC JDBC(Java Database Connectivity),即Java数据库连接,是一种用于执行SQL语句的Java API,可以为多种关系数据库提供同一访问,它由一组用Java语言编写的类和接口组成.JDBC提供了一种基准,根据这种基准可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序.总而言之,JDBC做了三件事: 1.与数据库建立连接 2.发送操作数据库的语句 3.处理结果 JDBC简单示例 下面的代码演示了如何利用JDBC从数据库中查询若干条符合要求

随机推荐