从excel表格读取数据用Java代码实现批量上传写入数据库

从excel表格读取数据用Java代码实现批量上传写入数据库,第1张

package comttgisframeworkutil;

import javaioFile;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javalangreflectField;

import javalangreflectInvocationTargetException;

import javalangreflectMethod;

import javanetURLEncoder;

import javautilArrayList;

import javautilHashMap;

import javautilIterator;

import javautilList;

import javautilMap;

import javautilMapEntry;

import javaxservletServletOutputStream;

import javaxservlet>

1提取单条记录

//importjavasql;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

con=(url,%%2,%%3);

stmt=conn();

stmt(%%4);

rs=stmt(%%5);

}catch(Exceptione){

e();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(e){

e();

}

}

3显示表格

/

importjavaawt;

importjavaxswing;

importjavasql;

importjavaxswingtable;

String[]colHeads=%%4;

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

ClassforName("sunjdbcodbc");

Stringurl="jdbc:odbc:%%1";

conn=(url,%%2,%%3);

stmt=conn();

rs=stmt("SELECTcount()asau_countfrom"%%5);

rsnext();

intiCount=rsgetInt("au_count");

Object[][]data=newObject[iCount][];

inti=0;

rs=stmt("SELECTfrom"%%5);

while(rsnext()){

data[i]=newObject[iCount];

data[i][0]=rsgetString("au_fname");

data[i][1]=rsgetString("Phone");

data[i][2]=rsgetString("City");

i;

}

JTabletable=newJTable(data,colHeads);

JScrollPanejsp=newJScrollPane(table);

()add(jsp);

}catch(Exceptione){

eprintStackTrace();

}

finally{

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

6关闭时关闭连接

//importjavasql;

addWindowListener(newWindowAdapter{

publicvoidwindowClosing(WindowEventwevent){

if(stmt!=null){

try{

if(rs!=null)

rsclose();

if(stmt!=null)

stmtclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

eprintStackTrace();

}

}

7执行命令

//importjavasql;

Connectionconn=null;

PreparedStatementpst=null;

try{

conn=DriverManagergetConnection(url);

pst=connprepareStatement("InsertIntograde(%%1)Values()");

pstsetInt(1,%%2);

//pstsetString(2,%%2);

pstaddBatch();

pstexecuteBatch();

}catch(SQLExceptione){

eprintStackTrace();

}

finally{

try{

if(pst!=null)

pstclose();

if(conn!=null)

connclose();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

eprintStackTrace();

}

}

import javaio;

import javatextSimpleDateFormat;

import javautil;

import javasql;

import javautilDate;

import javautilHashMap;

import javautilMap;

import jxl;

public class SimUpdate {

 private String fileName;

 public ZfzSimUpdate(String fileName){

  thisfileName = fileName;

 }

 static Map tNames;

 static{

  tNames = new HashMap();

 }

 /

   用于产生 数据库的 ID 值,组成 [年月日时分秒(100-999)] 总共 17 位数

   根据不同的表名,可保证同一秒内产生的 ID 号不重复

  /  

 private static String getDtime() {

  String rid;

  Date nd = new Date();

  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

  rid = sdfformat(nd);

  return rid;

 }

 

 public String getSeqNumber(String tableName) {

  if(tableName == null || ""equals(tableName))

   tableName = "GENERY";

  Integer it;

  // noinspection SynchronizeOnNonFinalField

  synchronized(tNames){

   it = (Integer)tNamesget(tableName);

   if(it == null){

    it = new Integer(100);

    tNamesput(tableName, it);

   }else{

    if(itintValue() > 998)

     it = new Integer(100);

    else

     it = new Integer(1 + itintValue());

    tNamesput(tableName, it);

   }

  }

  return getDtime() + StringvalueOf(it);

 }

 

 private void updateDb(){

  try{

   Connection conn = DbPoolconnectDB();

   if(conn != null){

    Statement stmt = conncreateStatement();

    //

    jxlWorkbook rwb = null;

    try{

     //构建Workbook对象 只读Workbook对象

     //直接从本地文件创建Workbook

     //从输入流创建Workbook

     InputStream is = new FileInputStream(fileName);

     rwb = WorkbookgetWorkbook(is);

     //Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中

     //Sheet的下标是从0开始的

     //获取第一张Sheet表

     Sheet rs = rwbgetSheet(0);

     //获取Sheet表中所包含的总列数

     int rsColumns = rsgetColumns();

     //获取Sheet表中所包含的总行数

     int rsRows = rsgetRows();

     //获取指这下单元格的对象引用

     

     String simNumber = "",termSeqId = "";

     //指定SIM卡号及序列号

     for(int i=0;i<rsRows;i++){

      for(int j=0;j<rsColumns;j++){

       Cell cell = rsgetCell(j,i);

       if(j==0){

        simNumber = cellgetContents();

       }  

       termSeqId = "633"+simNumber;

      } 

      String sql = "查询SQL";

      int isOk = stmtexecuteUpdate(sql);

      if(isOk == 0 && !simNumberequals("")){

       String termId = getSeqNumber("termInf");

       String insertSql = "自定义INSERT";

       int isAdd = stmtexecuteUpdate(insertSql);

       if(isAdd > 0){

        Systemoutprintln("成功插入第"+i+"条数据");

       }

        

      }

      //Systemoutprintln("SIM卡号:"+simNumber+",序列号:"+termSeqId);

     }

     

     //以下代码为写入新的EXCEL,这里不使用,所以注释

     /

     

     //利用已经创建的Excel工作薄创建新的可写入的Excel工作薄

     jxlwriteWritableWorkbook wwb = WorkbookcreateWorkbook(new File("D://Book2xls"),rwb);

     //读取第一张工作表

     jxlwriteWritableSheet ws = wwbgetSheet(0);

   

     //获取第一个单元格对象

     jxlwriteWritableCell wc = wsgetWritableCell(0, 0);

     //决断单元格的类型,做出相应的转化

     if (wcgetType() == CellTypeLABEL) {

      Label l = (Label) wc;

      lsetString("The value has been modified");

     }

     //写入Excel对象

     wwbwrite();

     wwbclose();

      /

    }catch(Exception e){

     eprintStackTrace();

    }

    finally{

     // *** 作完成时,关闭对象,翻译占用的内存空间

     rwbclose();

   

    }

    //

}

  }catch(Exception e){

   eprintStackTrace();

  }

 }

 public static void main(String args[]){

  DbPool dbPool = new DbPool("dbConncfg");//连接数据库

  SimUpdate simUpdate = new SimUpdate("zfz_simxls");

  simUpdateupdateDb();

  

 }

  

}

我只用了读取XLS,写入没试,应该没问题吧,你把注释了的拿 来试一下吧

以上就是关于从excel表格读取数据用Java代码实现批量上传写入数据库全部的内容,包括:从excel表格读取数据用Java代码实现批量上传写入数据库、如何用java实现将execl表格导入到数据库中、求一个java程序,excel表格读取表格内容存入到数据库,再从数据库读取数据存放到excel文件中。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10206607.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存