我想从db获取增量值并显示在输入字段中。当我打开jsp页面时,将自动从db中获取增量值,并显示在jsp的输入值中。我的序列和表格如下所示:
Sequence:
CREATE SEQUENCE stock_register_serial_no_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
Table:
CREATE TABLE stock_requirement_request
(
serial_no serial NOT NULL,
stock_requirement_slip_no character varying(255) NOT NULL,
CONSTRAINT stock_requirement_request_pkey PRIMARY KEY (serial_no, stock_requirement_slip_no)
);
ALTER TABLE stock_requirement_request ALTER COLUMN stock_requirement_slip_no SET DEFAULT TO_CHAR(nextval('stock_register_serial_no_seq'::regclass),'"S"fm000000');
我的jsp是这样的:
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bjspring", "postgres",
"postgres");
statement = connection.createStatement();
String sql = "select max(stock_requirement_slip_no) from stock_requirement_request";
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
String numb = resultSet.getString(1);
Integer number = Integer.valueOf(numb);
%>
<div class="row">
<div class="col-25" style="float: center;">
<label for="stock_requirement_slip_no">Requirement Slip No</label>
</div>
<div class="col-75">
<input type="text" id="stock_requirement_slip_no"
name="stock_requirement_slip_no"
placeholder="Enter stock requirement slip no.."
value="<%=number + 1%>" readonly required>
</div>
</div>
使用这些,我想在打开此jsp(输入屏幕)时显示自动增量值。
当db为null
时,将自动显示S000001
,保存S000001
时将显示S000002
,S000003
,等等。