Database-related operations: such as connection, query, addition, deletion, modification, and paging display

Source: Internet
Author: User

Database-related operations: such as connection, query, addition, deletion, modification, and paging display
 
Package study. database;

/**
* <P> Title: jsp (preferred for SUN Enterprise Applications) model learning </p>
* <P> Description: database related operations, such as connection, query, addition, deletion, and modification. </p>
* <P> Copyright: Copyright (c) 2004 </p>
* <P> Company: </p>
* @ Author Li Yansheng
* @ Version 1.0
*/
Import java. SQL .*;
Import java. io .*;
Import java. util .*;

Public class Operation {
// Database driver
Private String strDriver = "";
// Database connection string
Private String strURL = "";
// Database username
Private String username = "";
// Database Password
Private String password = "";

Private Connection conn = null;
Private Statement stmt = null;
ResultSet rs = null;

/** Read database configuration information
*/
Private void loadProp (){
InputStream is = getClass (). getResourceAsStream ("/setup.txt ");
Properties props = new Properties ();

Try {
Props. load (is );
} Catch (Exception e ){
System. err. println ("the configuration file cannot be read. Ensure that setup.txt is in the path specified by classes ");
}

Enumeration propNames = props. propertyNames ();
While (propNames. hasMoreElements ()){
String name = (String) propNames. nextElement ();
If (name. endsWith (". driver ")){
String poolName = name. substring (0, name. lastIndexOf ("."));
StrDriver = props. getProperty (poolName + ". driver ");
StrURL = props. getProperty (poolName + ". url ");
Username = props. getProperty (poolName + ". user ");
Password = props. getProperty (poolName + ". password ");
}
}
}

/** Connect to the database when creating an Operation object
*/
Public Operation (){
// Read database configuration information
LoadProp ();

Try {
Class. forName (strDriver );
} Catch (java. lang. ClassNotFoundException e ){
System. err. println ("database connection error:" + e. getMessage ());
}

Try {
Conn = DriverManager. getConnection (strURL, username, password );
} Catch (SQLException ex ){
System. err. println ("database connection error:" + ex. getMessage ());
}
}

/** Database Query
* SQL: SQL query statement
*/
Public ResultSet query (String SQL ){
Try {
Stmt = conn. createStatement ();
Rs1_stmt.exe cuteQuery (SQL );
} Catch (SQLException ex ){
System. err. println ("database query error:" + ex. getMessage ());
}

Return rs;
}

/** Add, modify, and delete Databases
* SQL: SQL statement
*/
Public void update (String SQL ){
Try {
Stmt = conn. createStatement ();
Stmt.exe cuteUpdate (SQL );
} Catch (SQLException ex ){
System. err. println ("database update error:" + ex. getMessage ());
}
}

/** Get the total number of records in the query results
* Rs: query result set
*/
Public int totalRecord (ResultSet rs) throws Exception {
Int total = 0;
// Move the pointer to the last record
Rs. last ();
Total = rs. getRow ();
Rs. first ();

Return total;
}

/** Display by PAGE
* CurrPage: Current page number
* PageSize: page size
* PageCount: Total number of pages
* Filename: the name of the file that uses pagination (which must be followed ?, Multiple parameters are separated by commas (,), and parameters are ended with commas)
* Http://www.knowsky.com/return string: <a href = "filename? Page = Current page number ">
*/
Public String showPages (int currPage, int pageSize, int pageCount, String filename ){
String addr;

Addr = "<table width = 100% border = 0 align = center cellpadding = 0 cellspacing = 0> <form method = Post name = pageform> <tr> <td> <div align = right> current <strong> <font color = red> "+ currPage +" </font> </strong> page "+
"Total <strong> <font color = red>" + pageCount + "</font> </strong> pages <strong> <font color = red>" + pageSize + "</font> </strong> entries ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.