public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_value() {
return _value;
}
public void set_value(String _value) {
this._value = _value;
}
}
4.编写主键生成方法
Java代码
1.package com.zzg.util;
2.
3.import java.text.SimpleDateFormat;
11. Class.forName("com.mysql.jdbc.Driver");
12. conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
8.import java.util.ArrayList;
9.import java.util.List;
10.import ermodel.HSSFCell;
11.import ermodel.HSSFRow;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
} catch (Exception e) {
21. private Class<T> type;
22. private String sheetName;
23.
24. public OperExcel(File excelFile, String tableName, Class<T> type,
Connection conn = DbUtils.getConn();
boolean flag=false;
try {
pstmt = conn.prepareStatement(sql);
flag=pstmt.execute();
} catch (SQLException e) {
13. primaryKey +=r.nextInt(100000)+100000;
14. return primaryKey;
15. }
16.}
package com.zzg.util;
import java.text.SimpleDateFormat;
package com.zzg.db;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtils {
private static Connection conn;
static {
try {
16.import com.zzg.util.GenericUtil;
17.
18.public class OperExcel<T extends Serializable> {
19. private HSSFWorkbook workbook;
20. private String tableName;
13. } catch (Exception e) {
14. e.printStackTrace();
15. }
16. }
17.
18. public static Connection getConn() {
Random r = new Random();
primaryKey +=r.nextInt(100000)+100000;
return primaryKey;
}
}
5.编写Excel操作类
Java代码
1.package com.zzg.deployData;
4.import java.util.Date;
5.import java.util.Random;
6.
7.public class GenericUtil {
8. public static String getPrimaryKey()
9. {
10. String primaryKey;
11. primaryKey = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
12. Random r = new Random();
2.
3.import java.io.File;
4.import java.io.FileInputStream;
5.import java.io.FileNotFoundException;
6.import java.io.IOException;
7.import java.io.Serializable;
利用java操作Excel表格(把Excel中的数据导入数据库中)
0.加入依赖的jar文件
引用
*mysql的jar文件
*Spring_HOME/lib/poi/*.jar
1.编写数据库链接类
Java代码
1.package com.zzg.db;
2.
3.import java.sql.Connection;
import java.util.Date;
import java.util.Random;
public class GenericUtil {
public static String getPrimaryKey()
{
String primaryKey;
primaryKey = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
16. e.printStackTrace();
17. }
18. return flag;
19. }
20.}
package com.zzg.db;
import java.sql.Connection;
}
}
2.编写数据库操作类
Java代码
1.package com.zzg.db;
2.
3.import java.sql.Connection;
4.import java.sql.PreparedStatement;
5.import java.sql.SQLException;
12. this._name = _name;
13. }
14.
15. public String get_value() {
16. return _value;
17. }
18.
19. public void set_value(String _value) {
4.import java.sql.DriverManager;
5.
6.public class DbUtils {
7. private static Connection conn;
8.
9. static {
10. try {
e.printStackTrace();
}
}
public static Connection getConn() {
return conn;
}
public static void setConn(Connection con;
11. boolean flag=false;
12. try {
13. pstmt = conn.prepareStatement(sql);
14. flag=pstmt.execute();
15. } catch (SQLException e) {
19. return conn;
20. }
21.
22. public static void setConn(Connection conn) {
23. DbUtils.conn = conn;
24. }
25.}
6.
7.public class ExcuteData {
8. private PreparedStatement pstmt;
9. public boolean ExcuData(String sql) {
10. Connection conn = DbUtils.getConn();
20. this._value = _value;
21. }
22.}
package com.zzg.model;
public class TableCell {
private String _name;
private String _value;
import java.sql.PreparedStatement;