我正在编写一个应用程序,我在显示数据库中的最后一个ID时遇到了一些问题(递增+1),我想在JSP中输入字段有一个默认值,它将是数据库中的最后一个ID。我尝试了一些选项,如value="id" or value "${patient.id}"
,但它从未奏效。我该怎么做呢?这是一个代码:
RegistrationPatient.jsp:
<p style="font-size: 26">
ID<input style="font-family: Century Gothic; font-size: 26"
type="text" value="id" name="id" disabled="disabled" />
控制器:
@RequestMapping(value="/registration.html", method = RequestMethod.GET)
public ModelAndView registrationPatient(@ModelAttribute("patient") Patient patient){
setAppContext();
clinicService.getNextId(patient);
ModelAndView reg = new ModelAndView("RegistrationPatient");
return reg;
}
ClinicService:
public void getNextId(Patient patient){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
patientDAO = ctx.getBean("patientDAO", PatientDAOImpl.class);
patient.setId(patient.getId());
patientDAO.getNextId(patient);
}
PatientDAOImpl:
public void getNextId(Patient patient) {
String query = "SELECT idpatient FROM virtualclinic.patient ORDER BY idpatient DESC LIMIT 1";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = dataSource.getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
while (rs.next()){
int PID;
PID = Integer.parseInt(rs.getString(1));
int nextPID = PID + 1;
patient.setId(String.valueOf(nextPID));
}
patient.getId();
}catch(SQLException e){
e.printStackTrace();
}
答案 0 :(得分:0)
使用jdbc保存记录时,可以执行以下操作:
//assuming connection is setup
PreparedStatement ps = connection.prepareStatement(QUERY,
new String[] { "patient_id" });
Long patientID= null;
if (ps.executeUpdate() > 0) {
ResultSet generatedKeys = ps.getGeneratedKeys();
if (null != generatedKeys && generatedKeys.next()) {
patientID = generatedKeys.getLong(1);
}
注意第二个参数传递给prepareStatement()方法传递主键列的名称,在这种情况下,patient_id是从序列生成的
现在,patientID具有最后一个患者记录插入的id。你可以将它推进1,然后在jsp中显示它。 我希望这很有用。