Common formatting in JSP includes:
1. String formatting
2. format the string and HTML display content
3. format the date
1. String formatting
String processing is the most frequently used in projects. Java provides two methods to format strings. JDBC also provides a question mark parameter similar to a formatting SQL statement.
1. format the string using the string. Format () method.
Syntax format: public static string format (string format, object... ARGs)
The placeholder (same as the C language) in the string is % d -- integer % f -- decimal % s -- string
During formatting, replace the value of the variable in the placeholder order. If there are duplicate values, you need to write two % d Values and assign two values.
For example:
String
STR = "select top % d * From tb_user ";
Str
= String. Format (STR, 10 );
2. format the string using messageformat.
Class messageformat comes from the java. Text package
Placeholders: {0}, {1}, or {1 [, format type [, format style]}
The formats include number, date, time, and choice.
For details about the format and style, see JDK help.
For example:
Int planet = 7;
String event = "disturbance ";
String result = messageformat. Format (
"At {1, time} on {1, date}, there was {2} on planet {0, number, integer }.",
Planet, new date (), event );
Output:
At
PM on Jul 3, 2053, there was disturbance on planet 7.
3. Question Mark parameters of SQL statements
Use? Is a placeholder. The sequence starts from 1.
Before creating a preparedstatement object to execute an SQL statement, set the parameter values for all question marks.
Statement:
Private void bindparameter (object... Params ){
If (Params
! = NULL) {// variable parameters are used here. If no parameter exists, null is used. Otherwise, it is a parameter array and the type is object.
Int
I = 1;// The parameter number starts from 1.
For (Object
O: Params ){
If (o
= NULL ){
Pre. setnull (I, types. varchar );// Set the database field value to null
} Else
If (O instanceof java. util. Date) {// o if it is a Java. util. date object, true is returned.
Java. util. Date
Dd = (Java. util. Date) O;
Java. SQL. Date
SD = new java. SQL. Date (DD. gettime ());
Pre. setdate (I,
SD );
} Else {
Pre. setobject (I,
O );