3.21

发布时间 2023-03-22 21:14:29作者: 王者归来蔡徐

今日我编写了地铁的一条线的中间有几条站的代码,

private static int getstationid = 0;

public int getstationid(String station) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection= dbutil.getConnection();
String sql="select stationid from underground where station=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,station);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){

getstationid=resultSet.getInt("stationid");

}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//dbutil.close(resultSet,preparedStatement,connection);
}
return getstationid;
}
}

我们的方法是先根据站名查询出站的号,然后把号进行相减,如果结果小于0就去相反数

输出中间站点号