java 跪求 用jxl 将EXcel中数据 导入 MYSQL的源码~

RT... RT 展开
 我来答
gdsfggdf
推荐于2016-07-11 · TA获得超过840个赞
知道小有建树答主
回答量:219
采纳率:100%
帮助的人:197万
展开全部
看下吧..

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import Bean.DBClass;
import Bean.DBContr;

public class Test {

static String UserName = "newExcel"; // 生成的数据库名字

private static Connection conn = null;

private static String drive = "com.mysql.jdbc.Driver";

private static String DBurl ="jdbc:mysql://localhost:3306/muren";

private static String name = "sa"; //数据库账号

private static String pwd = "sa"; //数据库,密码

private static Statement st=null;

public static void main(String[] args) {
readExcel("D:/newExcel.xls");
UserName = "UserName";
}

public static void readExcel(String url) {
File filename = new File(url);
Workbook wb = null;
String create = "create table " + UserName + "(";
String sql = "insert into " + UserName + "(";
String parameter = "";
String value = "";
String insert = "";
try {
wb = Workbook.getWorkbook(filename);
Sheet s = wb.getSheet(0);// 第1个sheet
Cell c = null;
int row = s.getRows();// 总行数
int col = s.getColumns();// 总列数
for (int i = 0; i < col; i++) {
if (i == col-1) {
create += s.getCell(i, 0).getContents() + " varchar(255)";
parameter += s.getCell(i, 0).getContents();
} else {
create += s.getCell(i, 0).getContents() + " varchar(255),";
parameter += s.getCell(i, 0).getContents() + ",";
}
}
create += ")";
System.out.println("数据库生成表语句---" + create);
try {
Create(create);
} catch (Exception e) {
int a = JOptionPane.showConfirmDialog(null, "数据库已经存在的表,确定删除吗?",
"温馨提示", JOptionPane.YES_NO_OPTION);
if (a == 0) {
try {
Create("drop table " + UserName + "");
Create(create);
} catch (Exception e1) {
return;
}
} else {
int aa = JOptionPane.showConfirmDialog(null, "是否继续添加到原来表单裏面",
"温馨提示", JOptionPane.YES_NO_OPTION);
if (aa == 1) {
return;
} }
}
System.out.println("--------------------------------------");
for (int i = 1; i < row; i++) {
value="";
for (int j = 0; j < col; j++) {
c = s.getCell(j, i);
if (j < col - 1) {
value += "'" + c.getContents() + "',";
} else {
value += "'" + c.getContents() + "'";
}
}
insert = sql + parameter + ") values(" + value + ")";
System.out.println("添加语句-------" + insert);
int a = insert(insert);
if (a > 0) {
System.out.println("成功添加" + i + "次");
} else {
System.out.println("第" + i + "次失败了");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}

public static Connection getConn() {
try {
Class.forName(drive);
conn = DriverManager.getConnection(DBurl, name, pwd);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接错误");
}
return conn;
}

public static void Close() {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static int insert(String sql){
int result=0;
try {
st=getConn().createStatement();
result=st.executeUpdate(sql);
} catch (Exception e) {
System.out.println("添加失败");
}finally{
Close();
}
return result;
}

public static int Create(String sql) throws Exception{
int result=0;
try {
st=getConn().createStatement();
result=st.executeUpdate(sql);
}finally{
Close();
}
return result;
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式