从Excel到导入MYSQL数据库

为了把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();
}
}
}
}

猜你喜欢

转载自my520530.iteye.com/blog/1551344