import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class importFromExcel {
static String UserName = "newExcel"; // 生成的数据库名字
private static Connection conn = null;
private static String drive = "com.mysql.jdbc.Driver";
private static String DBurl ="jdbc:mysql://192.168.28.24/wm123";
private static String name = "root"; //数据库账号
private static String pwd = "xybNew@24"; //数据库,密码
private static Statement st=null;
public static void main(String[] args) {
readExcel("F:/网盟123/proportion.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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
import-data-from-excel-to-mysql.rar_excel_excel mysql_java excel
共1个文件
txt:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 39 浏览量
2022-09-22
22:05:48
上传
评论
收藏 1KB RAR 举报
温馨提示
从excel中读取数据,然后存储到mysql中
资源推荐
资源详情
资源评论
收起资源包目录
import-data-from-excel-to-mysql.rar (1个子文件)
import data from excel to mysql.txt 4KB
共 1 条
- 1
资源评论
小贝德罗
- 粉丝: 70
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功