Example 1: The application has the I/O buffer function class
Import java. Io .*;
Public class iotest
{
Public static void main (string ARGs [])
{
Try
{
Filereader Fr = new filereader (ARGs [0]);
Bufferedreader BR = new bufferedreader (FR );
While (Br. Readline ()! = NULL)
{
System. Out. println ("the file content are:" + Br. Readline ());
}
FCM. Close ();
}
Catch (ioexception IOE)
{
System. Out. println ("the I/O exception is" + IOE );
}
}
}
In the above example, the program uses a class with the buffer function, which greatly improves the read Speed of disk I/O. Bufferedreader is a Java class that replaces datainputstream and improves read/write speed. In the new Java version, datainputstream is not recommended because its read and write operations are based on characters.
Example 2: String operation
Public class stringoperation
{
Public static void main (string ARGs [])
{
String sqlquery = NULL;
String sqlcondition = "conditionc = conditiond ");
Stringbuffer sb = new stringbuffer ();
SB. append ("select * from database table where ");
SB. append ("conditiona = conditionb and ");
If (! Sqlcondition. Equals (null)
{
SB. append (sqlcondition );
}
Else
{
SB. append ("Conditione = conditionf ");
}
Sqlquery = sb. tostring ();
// Then connect to the database then excute the database query
//.......
}
}
In the above example, stingbuffer class is used to create a database query, avoiding the "+ =" Operation of string class to reduce JVM creation of objects in memory and occupying resources, this increases the JVM Resource Recycling burden. You can use the Java proflier function to compare different string operations. The JVM needs to collect resources and run time. Therefore, it is very expensive to directly perform "+ =" on strings in JVM.
Example 3: process expensive database Initialization
At present, many websites can query databases through Web servers. How to improve the database query speed has become a concern of many programmers. Java servlets or JSP can be implemented through Init () or jspinit (). Below is a specific Java Servlet and database dialog instance.
Import java. Io .*;
Import java. SQL .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class databaseservlet extends httpservlet
{
Public void Init (servletconfig conf) throws servletexception
{
Super. INIT (CONF );
Connection conn = NULL;
Try
{
Class. forname ("Sun. JDBC. ODBC. jdcodbcdriver ");
Conn = drivermanager. getconnection ("JDBC: ODBC: yourdsn ,"","");
}
Catch (sqlexception sqle)
{
System. Err. println ("your error exception is" + sqle );
}
Catch (classnotfoundexception cnfe)
{
System. Err. println ("your error exception is" + cnfe );
}
}
Public void doget (httpservletrequest req, httpservletresponse res) throws
Servletexception, ioexception
{
Res. setcontenttype ("text/html ");
Servletoutputstream out = NULL;
// Your HTML formatter
Out. println ("your HTML ");
Try
{
Statement stmt = conn. creatstatement ();
Resultset rs = stmt. excutequery ("select * From yourdatabasetable ");
While (Rs. Next ())
{
// Processing your data
}
}
Catch (sqlexception sqle)
{
Out. println ("the SQL error is" + sqle );
}
// Output your processing result to HTML page
Out. println ("your HTML ");
Rs. Close ();
Stmt. Close ();
}
Public void destroy ()
{
Try
{
Conn. Close ();
}
Catch (sqlexception sqle)
{
System. Err. println ("Your SQL error is" + sqle );
}
}
}
In the above example, due to the characteristics of the Java Servlet operating mechanism, the expensive database initialization operation is completed in the init () called only once during the entire servlet operation, to reduce unnecessary repetitive database operations. The reader can move the statement and resultset parts of the database to Init () based on the actual situation of the application, or call preparedstatement and callablestatement to optimize database operations. At the same time, the closure of the database connection is completed by destroy () at one time.