做了一个excel转数据库的东西,找不到url,(系统找不到指定的路径。),换了全路径以后也不行
public class PoiRead {
public static void main(String[] args) throws Exception {
ArrayList<String> cars = new ArrayList<String>();
String PATH = "C:\\Users\\李晓彬\\Desktop\\汽车信息表.xlsx";
SqlSession sqlSession = Mybatis.getSqlSession();
//CarDao mapper = sqlSession.getMapper(CarDao.class);
FileInputStream inputStream = new FileInputStream(PATH);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
//总行数
int rowNum=sheet.getPhysicalNumberOfRows();
//总列数
int cellNum = sheet.getRow(0).getPhysicalNumberOfCells();
for (int i = 0; i < rowNum; i++) {
XSSFRow row = sheet.getRow(i);
Car car = new Car();
for (int cellNumber = 0; cellNumber < cellNum; cellNumber++) {
XSSFCell cell = row.getCell(cellNumber);
if (cell != null) {
CellType cellType = cell.getCellType();
if(i==0){
cars.add(cell.getStringCellValue());
continue;
}
if(cars.get(cellNumber).equals("序号")){
car.setCarID(cell.getStringCellValue());
continue;
}
if(cars.get(cellNumber).equals("汽车名字")){
car.setName(cell.getStringCellValue());
continue;
}
if(cars.get(cellNumber).equals("核载人数")){
car.setPeople(cell.getStringCellValue());
continue;
}
if(cars.get(cellNumber).equals("厂家")){
car.setFrom(cell.getStringCellValue());
continue;
}
if(cars.get(cellNumber).equals("发售日期")){
car.setDate(cell.getStringCellValue());
continue;
}
}
}
if(i!=0){
insertUser(car);
}
}
}
public static int insertUser(Car car) {
String carID= car.getCarID();
String name=car.getName();
String people=car.getPeople();
String from=car.getFrom();
String date=car.getDate();
Connection connection = null;
PreparedStatement ps = null;
int yxhs = 0;
int i = 0;
try {
connection = JdbcUtils.getConnection();
String sql = "INSERT INTO car_information (car_id,car_name,car_people,car_room,car_day) VALUE (?,?,?,?,?)";
ps = connection.prepareStatement(sql);
ps.setString(++i, carID);
ps.setString(++i, name);
ps.setString(++i, people);
ps.setString(++i, from);
ps.setString(++i, date);
yxhs = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(null, ps, connection);
}
return yxhs;
}}
```java
public class JdbcUtils {
//数据库地址
private static String Url;
//用户名
private static String UserName;
//密码
private static String PassWord;
//数据库驱动
private static String Driver;
static {
try {
Properties pro = new Properties();
ClassLoader classLoader = JdbcUtils.class.getClassLoader();
URL res = classLoader.getResource("db.properties");
String path = res.getPath();
pro.load(new FileReader(path));
Url = pro.getProperty("url");
UserName = pro.getProperty("username");
PassWord = pro.getProperty("password");
Driver = pro.getProperty("driver");
Class.forName(Driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return Connection 连接对象
* @description 创建连接
* @author zyh
* @date 2022/1/4 16:10
* @version 1.0
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(Url, UserName, PassWord);
}
/**
* @param resultSet 结果集对象
* @param statement PreparedStatement预编译对象
* @param connection jdbc连接对象
* @description 关闭连接
* @author zyh
* @date 2022/1/4 16:11
* @version 1.0
*/
public static void close(ResultSet resultSet, PreparedStatement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/930479545446168.png "#left")
```