连接数据库的工具类
1 package demo; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class DBhepler { 9 /* 10 * String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String 11 * url = "jdbc:sqlserver://127.0.0.1;DatabaseName=javenforexcel"; 12 */ 13 14 String driver = "com.mysql.jdbc.Driver"; 15 String url = "jdbc:mysql://127.0.0.1:3306/javenforexcel"; 16 17 Connection con = null; 18 ResultSet res = null; 19 20 public void DataBase() { 21 try { 22 Class.forName(driver); 23 con = DriverManager.getConnection(url, "root", "root"); 24 } catch (ClassNotFoundException e) { 25 // TODO Auto-generated catch block 26 System.err.println("装载 JDBC/ODBC 驱动程序失败。"); 27 e.printStackTrace(); 28 } catch (SQLException e) { 29 // TODO Auto-generated catch block 30 System.err.println("无法连接数据库"); 31 e.printStackTrace(); 32 } 33 } 34 35 // 查询 36 public ResultSet Search(String sql, String str[]) { 37 DataBase(); 38 try { 39 PreparedStatement pst = con.prepareStatement(sql); 40 if (str != null) { 41 for (int i = 0; i < str.length; i++) { 42 pst.setString(i + 1, str[i]); 43 } 44 } 45 res = pst.executeQuery(); 46 47 } catch (Exception e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 return res; 52 } 53 54 // 增删修改 55 public int AddU(String sql, String str[]) { 56 int a = 0; 57 DataBase(); 58 try { 59 PreparedStatement pst = con.prepareStatement(sql); 60 if (str != null) { 61 for (int i = 0; i < str.length; i++) { 62 pst.setString(i + 1, str[i]); 63 } 64 } 65 a = pst.executeUpdate(); 66 } catch (Exception e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 return a; 71 } 72 73 }
表的实体如下
1 package demo; 2 public class StuEntity { 3 private int id; 4 private String name; 5 private String sex; 6 private int num; 7 8 public StuEntity() { 9 } 10 11 public StuEntity(int id, String name, String sex, int num) { 12 this.id = id; 13 this.name = name; 14 this.sex = sex; 15 this.num = num; 16 } 17 18 @Override 19 public String toString() { 20 return "StuEntity [id=" + id + ", name=" + name + ", sex=" + sex 21 + ", num=" + num + "]"; 22 } 23 24 public int getId() { 25 return id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getSex() { 41 return sex; 42 } 43 44 public void setSex(String sex) { 45 this.sex = sex; 46 } 47 48 public int getNum() { 49 return num; 50 } 51 52 public void setNum(int num) { 53 this.num = num; 54 } 55 56 }
Java实现Excel导入数据核心类 读取Excel表中所有的数据、操作数据(查询、更新)
1 package demo; 2 3 4 5 import java.io.File; 6 7 import java.sql.ResultSet; 8 9 import java.sql.SQLException; 10 11 import java.util.ArrayList; 12 13 import java.util.List; 14 15 16 17 import jxl.Sheet; 18 19 import jxl.Workbook; 20 21 22 23 import com.javen.db.DBhepler; 24 25 import com.javen.entity.StuEntity; 26 27 28 29 public class StuService { 30 31 /** 32 33 * 查询stu表中所有的数据 34 35 * 36 37 * @return 38 39 */ 40 41 public static ListgetAllByDb() { 42 43 List list = new ArrayList (); 44 45 try { 46 47 DBhepler db = new DBhepler(); 48 49 String sql = "select * from stu"; 50 51 ResultSet rs = db.Search(sql, null); 52 53 while (rs.next()) { 54 55 int id = rs.getInt("id"); 56 57 String name = rs.getString("name"); 58 59 String sex = rs.getString("sex"); 60 61 int num = rs.getInt("num"); 62 63 64 65 // System.out.println(id+" "+name+" "+sex+ " "+num); 66 67 list.add(new StuEntity(id, name, sex, num)); 68 69 } 70 71 72 73 } catch (SQLException e) { 74 75 // TODO Auto-generated catch block 76 77 e.printStackTrace(); 78 79 } 80 81 return list; 82 83 } 84 85 86 87 /** 88 89 * 查询指定目录中电子表格中所有的数据 90 91 * 92 93 * @param file 94 95 * 文件完整路径 96 97 * @return 98 99 */ 100 101 public static List getAllByExcel(String file) { 102 103 List list = new ArrayList (); 104 105 try { 106 107 Workbook rwb = Workbook.getWorkbook(new File(file)); 108 109 Sheet rs = rwb.getSheet("Test Shee 1");// 或者rwb.getSheet(0) 110 111 int clos = rs.getColumns();// 得到所有的列 112 113 int rows = rs.getRows();// 得到所有的行 114 115 116 117 System.out.println(clos + " rows:" + rows); 118 119 for (int i = 1; i < rows; i++) { 120 121 for (int j = 0; j < clos; j++) { 122 123 // 第一个是列数,第二个是行数 124 125 String id = rs.getCell(j++, i).getContents();// 默认最左边编号也算一列 126 127 // 所以这里得j++ 128 129 String name = rs.getCell(j++, i).getContents(); 130 131 String sex = rs.getCell(j++, i).getContents(); 132 133 String num = rs.getCell(j++, i).getContents(); 134 135 136 137 System.out.println("id:" + id + " name:" + name + " sex:" 138 139 + sex + " num:" + num); 140 141 list.add(new StuEntity(Integer.parseInt(id), name, sex, 142 143 Integer.parseInt(num))); 144 145 } 146 147 } 148 149 } catch (Exception e) { 150 151 // TODO Auto-generated catch block 152 153 e.printStackTrace(); 154 155 } 156 157 return list; 158 159 160 161 } 162 163 164 165 /** 166 167 * 通过Id判断是否存在 168 169 * 170 171 * @param id 172 173 * @return 174 175 */ 176 177 public static boolean isExist(int id) { 178 179 try { 180 181 DBhepler db = new DBhepler(); 182 183 ResultSet rs = db.Search("select * from stu where id=?", 184 185 new String[] { id + "" }); 186 187 if (rs.next()) { 188 189 return true; 190 191 } 192 193 } catch (SQLException e) { 194 195 // TODO Auto-generated catch block 196 197 e.printStackTrace(); 198 199 } 200 201 return false; 202 203 } 204 205 206 207 public static void main(String[] args) { 208 209 /* 210 211 * List all=getAllByDb(); for (StuEntity stuEntity : all) { 212 213 * System.out.println(stuEntity.toString()); } 214 215 */ 216 217 218 219 System.out.println(isExist(1)); 220 221 222 223 } 224 225 226 227 }
数据的数据导入到Excel表
1 package demo; 2 3 import java.io.File; 4 5 import java.util.List; 6 7 8 9 import com.javen.entity.StuEntity; 10 11 import com.javen.service.StuService; 12 13 14 15 import jxl.Workbook; 16 17 import jxl.write.Label; 18 19 import jxl.write.WritableSheet; 20 21 import jxl.write.WritableWorkbook; 22 23 24 25 public class TestDbToExcel { 26 27 28 29 public static void main(String[] args) { 30 31 try { 32 33 WritableWorkbook wwb = null; 34 35 36 37 // 创建可写入的Excel工作簿 38 39 String fileName = "D://book.xls"; 40 41 File file=new File(fileName); 42 43 if (!file.exists()) { 44 45 file.createNewFile(); 46 47 } 48 49 //以fileName为文件名来创建一个Workbook 50 51 wwb = Workbook.createWorkbook(file); 52 53 54 55 // 创建工作表 56 57 WritableSheet ws = wwb.createSheet("Test Shee 1", 0); 58 59 60 61 //查询数据库中所有的数据 62 63 Listlist= StuService.getAllByDb(); 64 65 //要插入到的Excel表格的行号,默认从0开始 66 67 Label labelId= new Label(0, 0, "编号(id)");//表示第 68 69 Label labelName= new Label(1, 0, "姓名(name)"); 70 71 Label labelSex= new Label(2, 0, "性别(sex)"); 72 73 Label labelNum= new Label(3, 0, "薪水(num)"); 74 75 76 77 ws.addCell(labelId); 78 79 ws.addCell(labelName); 80 81 ws.addCell(labelSex); 82 83 ws.addCell(labelNum); 84 85 for (int i = 0; i < list.size(); i++) { 86 87 88 89 Label labelId_i= new Label(0, i+1, list.get(i).getId()+""); 90 91 Label labelName_i= new Label(1, i+1, list.get(i).getName()); 92 93 Label labelSex_i= new Label(2, i+1, list.get(i).getSex()); 94 95 Label labelNum_i= new Label(3, i+1, list.get(i).getNum()+""); 96 97 ws.addCell(labelId_i); 98 99 ws.addCell(labelName_i); 100 101 ws.addCell(labelSex_i); 102 103 ws.addCell(labelNum_i); 104 105 } 106 107 108 109 //写进文档 110 111 wwb.write(); 112 113 // 关闭Excel工作簿对象 114 115 wwb.close(); 116 117 118 119 } catch (Exception e) { 120 121 // TODO Auto-generated catch block 122 123 e.printStackTrace(); 124 125 } 126 127 } 128 129 }
Excel表中的数据导入到MySql数据库
1 package demo; 2 3 import java.util.List; 4 5 6 7 import com.javen.db.DBhepler; 8 9 import com.javen.entity.StuEntity; 10 11 import com.javen.service.StuService; 12 13 public class TestExcelToDb { 14 15 public static void main(String[] args) { 16 17 //得到表格中所有的数据 18 19 ListlistExcel=StuService.getAllByExcel("d://book.xls"); 20 21 /*//得到数据库表中所有的数据 22 23 List listDb=StuService.getAllByDb(); */ 24 25 26 27 DBhepler db=new DBhepler(); 28 29 30 31 for (StuEntity stuEntity : listExcel) { 32 33 int id=stuEntity.getId(); 34 35 if (!StuService.isExist(id)) { 36 37 //不存在就添加 38 39 String sql="insert into stu (name,sex,num) values(?,?,?)"; 40 41 String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+""}; 42 43 db.AddU(sql, str); 44 45 }else { 46 47 //存在就更新 48 49 String sql="update stu set name=?,sex=?,num=? where id=?"; 50 51 String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+"",id+""}; 52 53 db.AddU(sql, str); 54 55 } 56 57 } 58 59 } 60 61 }