每日日报2021.10.20

完成内容:

1.编写大作业第一阶段

 dbmanage

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//connect or close mysql database autonomously
public class DBManage {


private String DRIVER = "com.mysql.cj.jdbc.Driver";
private String URL = "jdbc:mysql://";
private String USERNAME ;
private String PASSWORD ;
private String DBName;

public DBManage(String IP, String USERNAME, String PASSWORD,String Port,String DBName) {
this.URL = URL+IP+":"+Port+"/"+DBName+"?serverTimezone=UTC";
this.USERNAME = USERNAME;
this.PASSWORD = PASSWORD;
this.DBName=DBName;
}

private static final String SQL = "SELECT * FROM ";// 数据库操作

public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public boolean TCon(){
boolean result=true;
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败");
result=false;
e.printStackTrace();
}
try {
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);


} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//获得所有表名
public List<String> getTableNames() {
List<String> tableNames = new ArrayList<>();
Connection conn = getConnection();
ResultSet rs = null;
try {
//获取数据库的元数据
DatabaseMetaData db = conn.getMetaData();
//从元数据中获取到所有的表名
rs = db.getTables(DBName, null, null, new String[] { "TABLE" });
while(rs.next()) {
tableNames.add(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return tableNames;
}
//获得列名

public List<String> getColumnNames(String tableName) {
List<String> columnNames = new ArrayList<>();
//与数据库的连接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//结果集元数据
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列数
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnNames.add(rsmd.getColumnName(i + 1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return columnNames;
}

//获得字段类型
public List<String> getColumnTypes(String tableName) {
List<String> columnTypes = new ArrayList<>();
//与数据库的连接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//结果集元数据
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列数
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnTypes.add(rsmd.getColumnTypeName(i + 1)+rsmd.getColumnDisplaySize(i+1));

}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return columnTypes;
}

}
readexcel
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//lead
public class ReadExcel {

public List<List<Object>> ExcelToRowList(String fPath) {
Workbook wb = null;
Sheet sheet = null;
Row row = null;
List<List<Object>> sheetList = new ArrayList<>();
String filePath = fPath;
wb = readExcel(filePath);
if (wb != null) {
try {
List<List<List<Object>>> list = new ArrayList<>();

// for
for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
// Specifies the value of the TAB
sheet = wb.getSheetAt(sheetNum);
// Defines a List that holds all the data in a TAB


// for
for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
// Specify row values
row = sheet.getRow(rowNum);
// Define a List that holds a row of data
List<Object> rowList = new ArrayList<>();
// Specify column values
for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
rowList.add(getStringCellValue(cell));
}
//System.out.println(rowList.get(1));
sheetList.add(rowList);
}
list.add(sheetList);
//System.out.println(sheetList.get(2));
}
// System.err.println(list.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
return sheetList;
}


//Judge file format
private static Workbook readExcel(String filePath){
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));

try {
@SuppressWarnings("resource")
InputStream is = new FileInputStream(filePath);
if(".xls".equals(extString)){
return new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return new XSSFWorkbook(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}



@SuppressWarnings("deprecation")
public static String getStringCellValue(Cell cell) {
String cellvalue = "";
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellvalue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = cell.getDateCellValue();
cellvalue = sdf.format(date);
} else {
cellvalue = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
cellvalue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellvalue = "";
break;
default:
cellvalue = "";
break;
}
if (cellvalue == "") {
return "";
}
return cellvalue;
}
}

遇到问题:

目标:继续完成任务

posted @ 2021-10-20 21:08  1905-1雷宇  阅读(38)  评论(0编辑  收藏  举报