1个回答
展开全部
看下吧..
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;
}
}
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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询