溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

從Excel到導(dǎo)入MYSQL數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2020-08-13 08:27:55 來源:ITPUB博客 閱讀:233 作者:okone96 欄目:MySQL數(shù)據(jù)庫(kù)

為了把Excel導(dǎo)入數(shù)據(jù)庫(kù)寫了這個(gè)這段程序,大概思路解釋一下
:因?yàn)閷?dǎo)入數(shù)據(jù)庫(kù)時(shí)字段類型和長(zhǎng)度、還有字段數(shù)都是未知的,所以導(dǎo)入時(shí)用了通用的字段類型,在這里用了text,根據(jù)需要可以自行定制字段名,類型。這只是個(gè)簡(jiǎn)單的例子 如果常常遇到此類問題的我建議寫個(gè)導(dǎo)入你所想見的表格屬性的配置文件(xml或property文件都可以),這樣靈活性更強(qiáng)了。

還有一種更為好的辦法,把要導(dǎo)入的Excel文件另存為xml文件,
這樣就會(huì)變成xml to database ,想導(dǎo)入就容易咯

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

從Excel表格導(dǎo)入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="";//創(chuàng)建數(shù)據(jù)庫(kù)的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";默認(rèn)字段名
static Connection conn = null;

public static void main(String args[]){
try
{
// 構(gòu)建Workbook對(duì)象, 只讀Workbook對(duì)象
// 直接從本地文件創(chuàng)建Workbook
// 從輸入流創(chuàng)建Workbook

System.out.println("start load file-------------------------");
InputStream is = new FileInputStream("E:/users.xls");//創(chuàng)建輸入

jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0); //讀取第一個(gè)sheet
int colNum=rs.getColumns();//列數(shù)
int rowNum=rs.getRows();//行數(shù)

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;istrValue="";
for(int j=0;jCell 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;jtemp=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;jcolSql=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();
}
}
}

}

[@more@]
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI