java excel 导入数据库

 java excel 导入数据库

为了把Excel导入数据库写了这个这段程序,大概思路解释一下
:因为导入数据库时字段类型和长度、还有字段数都是未知的,所以导入时用了通用的字段类型,在这里用了text,根据需要可以自行定制字段名,类型。这只是个简单的例子如果常常遇到此类问题的我建议写个导入你所想见的表格属性的配置文件xml或property文件都可以),这样灵活性更强了。

还有一种更为好的办法,把要导入的Excel文件另存为xml文件,
这样就会变成xml to database ,想导入就容易咯

有什么不明白可以参考:全面挖掘Java Excel API 使用方法

从Excel表格导入msyql的例子代码

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import jxl.*;

public class test{
static String createTableSql="";//创建数据库的sql
static String colType="TEXT";//字段类型
static String key="id";//主键
static String charSet="utf8";//表格字符类型
static String ENGINE="InnoDB";//表格类型
static String tableName="tempExcelToMysql";//表名称
static String colName="col";默认字段名
static Connection conn = null;

public static void main(String args[]){
try
{
// 构建Workbook对象, 只读Workbook对象
// 直接从本地文件创建Workbook
// 从输入流创建Workbook

System.out.println("start load file-------------------------");
InputStream is = new FileInputStream("E:/users.xls");//创建输入

jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0); //读取第一个sheet
int colNum=rs.getColumns();//列数
int rowNum=rs.getRows();//行数

System.out.println("colNum rowNum------------------"+rowNum+","+colNum);
System.out.println("start create base-------------------------");

getConntion();

String tableSql=getCreateTableSql(rowNum,colNum);
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.execute(tableSql);
st.close();

System.out.println("create base end -------------------------");


String sql=getColName(rowNum,colNum);
PreparedStatement ps=null;
String strValue="";
ps=conn.prepareStatement(sql);
for(int i=0;i<rowNum;i++){
strValue="";
for(int j=0;j<colNum;j++){
Cell c = rs.getCell(j, i);
strValue=c.getContents();
ps.setString(j+1,strValue);
}
ps.addBatch();
}

ps.executeBatch();
conn.commit();

if(ps!=null){
ps.close();
}

System.out.println(" insert end-------------------------");
close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

static String getCreateTableSql(int rowNum,int colNum)
{
//可以做成可配置文件

createTableSql="create table "+tableName+"( `"+key+"` bigint(12) NOT NULL auto_increment, ";
String temp="";

for(int j=0;j<colNum;j++){
temp=temp+"`"+colName+j+"` "+colType+" DEFAULT NULL,";
}

createTableSql=createTableSql+" "+temp+" PRIMARY KEY (`"+key+"`)" +
") ENGINE="+ENGINE+" DEFAULT CHARSET="+charSet+";";

return createTableSql;
}

static String getColName(int rowNum,int colNum)
{
//可以做成可配置文件
String colSql="";
String colValue="";

for(int j=0;j<colNum;j++){
colSql=colSql+"`"+colName+j+"`,";
colValue=colValue+""+"?,";

}

return "insert into "+tableName+" ("+colSql.substring(0,colSql.lastIndexOf(","))+")values("+colValue.substring(0,colValue.lastIndexOf(","))+")";
}

static void getConntion()
{

try {
String driver_class = "com.mysql.jdbc.Driver";
String connection_url = "jdbc:mysql://localhost:3306/webserve?useUnicode=true&characterEncoding=utf-8";
String user_name = "root";
String db_password = "123";

Class.forName(driver_class);
conn = DriverManager.getConnection(connection_url, user_name,db_password);
}catch(Exception e){
e.printStackTrace();
}
}

static void close()
{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是一个简单的Java代码示例,展示如何将Excel文件中的数据导入数据库中: 1. 首先,你需要按照上面的方法读取Excel文件并获取单元格中的数据。 2. 然后,你需要建立一个连接到数据库的连接对象。 3. 接着,你需要使用PreparedStatement对象将数据插入到数据库中。 4. 最后,关闭连接对象和数据流。 下面是一个简单的Java代码示例,展示如何将Excel文件中的数据导入数据库中: ``` import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("path/to/excel/file.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(file); // 获取第一个工作表 XSSFSheet sheet = workbook.getSheetAt(0); // 建立数据库连接 Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 插入数据 String sql = "INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement statement = conn.prepareStatement(sql); for (Row row : sheet) { Cell cell1 = row.getCell(0); Cell cell2 = row.getCell(1); Cell cell3 = row.getCell(2); statement.setString(1, cell1.getStringCellValue()); statement.setInt(2, (int) cell2.getNumericCellValue()); statement.setDouble(3, cell3.getNumericCellValue()); statement.executeUpdate(); } statement.close(); conn.close(); file.close(); } catch (IOException | ClassNotFoundException | SQLException e) { e.printStackTrace(); } } } ``` 当然,你也可以根据自己的需求对代码进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sdrzths

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值