一个分组程序实现

http://www.jdom.org/docs/apidocs/index.html


import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 *  提供分组功能,分成若干组,用List保存每组信息
 *  已知条件:1)WANGYUAN总数 ;2)要分多少组 pages
 *  需要计算每组放多少个网ID 
 *  
 *  计算bujiang要分多少组可以参考下面  getPageCount()方法,bujiang分多少组 也就是把WANGYUAN 分多少组
 * @author hongwei
 *
 */
public class PageHandler {
	
	public static final int DEFAULT_PAGE_SIZE = 1; //WANGYUAN 默认不分组,就只一组
	
	public static final int DEFAULT_COMPONET_SIZE = 10000; //默认不分组,就只一组
	
	private List source; //存要分页的 WANGYUAN ID

	
	private int pageSize = DEFAULT_PAGE_SIZE; //每页存多少个WANGYUAN ID是根据计算得来的
	
	private int componetSize = DEFAULT_COMPONET_SIZE; 
	
	private int page = 0; //0 默认不分组,这里指的是 WANGYUAN 要分的组数,他等于bujiang要分的组数, bujiang要分多少组可以参考下面  getPageCount()方法
	
	/**
	 * Set the source list for this holder.
	 */
	public void setSource(List source) {
		//需要判断List不为空
		this.source = source;
		
	}
	public int getComponentSize(){
		return componetSize;
	}
	
	public List getSource() {
		return this.source;
	}

	
	/**
	 * 返回 WANGYUAN 的总数目
	 */
	public int getNetOfElements() {
		return getSource().size();
	}
    
	public int getComponentOfElements( Test test ){
		int componet_count=test.queryComponetRecord();
		return componet_count;
	}
	
	/**
	 * 设置每组的要存的 WANGYUAN 个数,不像其他分页那样指定,而是由WANGYUAN ID总数/要分的页数计算得来再存进去。
	 * pageSize什么时候初始比较合适
	 */
	public void setPageSize(int pageSize) {
		if (pageSize != this.pageSize) {
			this.pageSize = pageSize;
			
		}
	}
	
	/**
	 * 返回每页的要存的 WANGYUAN 个数 pageSize,
	 * 
	 *  已知条件:1)WANGYUAN 总数 ;2)要分多少组 pages
	 *  pageSize= WANGYUAN 总数/要分多少组 pages
	 *  
	 */
	public int getPageSize() {
		return this.pageSize;
	}
	
	/**
	 * 设置当前组号
	 * Page numbering starts with 0.
	 */
	public void setPage(int page) {
		this.page = page;
		
	}
   
	/**
	 * 返回总共分的组数,getNrOfElements_component bujiang的总数
	 */
	public int getPageCount( ) {
		
		float nrOfPages = (float) getComponentOfElements(new Test()) / getComponentSize(); //bujiang总数 /指定每组bujiang条数
		return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
	}
	/**
	 * 返回 WANGYUAN 分组每组数据大小
	 */
	public int getPageSizeCount( int sumPages) {
		float pageSize = (float)getNetOfElements()/sumPages; 
		
		float nrOfPages = (float) getComponentOfElements(new Test()) / getComponentSize(); //bujiang总数 /指定每组bujiang条数
		
		return (int) ((pageSize > (int) pageSize || pageSize == 0.0) ? pageSize + 1 : pageSize);
	}
	/**
	 * 返回当前组号
	 * Page numbering starts with 0.
	 */
	public int getPage() {
		if (this.page >= getPageCount()) {
			this.page = getPageCount() - 1;
		}
		return this.page;
	}

	/**
	 * Return the element index of the first element on the current page.
	 * Element numbering starts with 0.
	 */
	public int getFirstElementOnPage() {
		return (getPageSize() * getPage());
	}

	/**
	 * Return the element index of the last element on the current page.
	 * Element numbering starts with 0.
	 */
	public int getLastElementOnPage() {
		int endIndex = getPageSize() * (getPage() + 1);
		int size = getNetOfElements();
		return (endIndex > size ? size : endIndex) - 1;
	}

	/**
	 * 返回当前组的List
	 */
	public List getPageList() {
		return getSource().subList(getFirstElementOnPage(), getLastElementOnPage() + 1);
	}
	
	public List getTestTableSource( ){
		
		List list=null;
		return list;
	}
	/**
	 * 
	 * @param source WANGYUAN 总数
	 * @param sumPages 要分多少组
	 * @return
	 */
	public List handlePages(List source, int sumPages){
		
	
        setSource(source);
		//int sumPages=getPageCount();
		List groupList = new ArrayList(); //存放组
		pageSize=getPageSizeCount(sumPages);
		setPageSize(pageSize); //初始化当前组的数据
		//每组存放条数= WANGYUAN总数/要分多少组 pages
		System.out.println("每组存放记录条数:"+ pageSize);
		List  pageList; //存放同组记录 

	    for(int groupId=0; groupId<sumPages; groupId++){
	    	//初始化当前分组信息 setPageSize
	    	//初始化 pageSize(每组存放的网数据),  groupId当前组
	    	
	    	setPage(groupId); //初始化当前组
	    	
	    	pageList= new ArrayList();
	    	pageList = getPageList();//获取当前分组的数据
	    	
	    	groupList.add(pageList);
	    }
		for(int i=0; i<4; i++){
			List categoryList =(List) groupList.get(i);
		    
		    Iterator it = categoryList.iterator();
		    while (it.hasNext()){
		    	System.out.println("the category data:"+it.next());
		    }
		    System.out.println("****************************");
		}
	    
		return groupList;
	}
	
	
	public static void main(String args[]){
		PageHandler handler = new PageHandler();
		Test test = new Test();
		handler.handlePages(test.queryTestTableCategory(), handler.getPageCount());
		System.out.println("the size is:"+ test.queryTestTableCategory().size() );
	}
	
}







import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Test{


	public static Connection getConnection_mydb(){
		Connection conn=null;
			try{
				conn=DriverManager.getConnection("jdbc:mysql://localhost/mydb?" +
				"user=root&password=12345678");
			}catch(Exception ex){
				ex.printStackTrace();
			}
			
		return conn;
		}
	
	public static Connection getConnection_backuptable(){
		Connection conn=null;
			try{
				conn=DriverManager.getConnection("jdbc:mysql://localhost/backuptable?" +
				"user=root&password=12345678");
			}catch(Exception ex){
				ex.printStackTrace();
			}
			
		return conn;
		}
	
	public  int  queryComponetRecord( ){
		
		ResultSet rs = null;
		Statement stmt=null;
		
		List componet_list = new ArrayList();
		try {
			stmt = Test.getConnection_mydb().createStatement();
			rs = stmt.executeQuery("SELECT id FROM testtable");
			while(rs.next()){
				
				rs.getString("id");
				componet_list.add(rs.getString("id"));
				
			}
			
			
		}catch (SQLException ex){
			ex.printStackTrace();
		}
		int count= componet_list.size();
		return componet_list.size();
	}
	
	
public  List  queryTestTableCategory( ){
		
		ResultSet rs = null;
		Statement stmt=null;
		
		List category_list = new ArrayList();
		try {
			stmt = Test.getConnection_mydb().createStatement();
			rs = stmt.executeQuery("SELECT t.category FROM mydb.testtable t where t.category is not null group by category ");

			while(rs.next()){
				rs.getString("category");
				category_list.add(rs.getString("category"));
				
			}
			
			
		}catch (SQLException ex){
			ex.printStackTrace();
		}
		
		return category_list;
	}
	
	public static void main(String args[]){
		
		
		int flag=0;
		ResultSet rs = null;
		Statement stmt=null;
		
		try {
			stmt = Test.getConnection_mydb().createStatement();
			rs = stmt.executeQuery("SELECT * FROM testtable");
			while(rs.next()){
				System.out.println("the name is:"+rs.getString(2)+"the address is"+rs.getString("address"));
				System.out.println("the result is"+(String)rs.getString(1));
			}
		}catch (SQLException ex){
			//handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		//it is a good idea to release
		//resources in a finally{} block
		//in reverse-order of their creation
		//if they are no-longer needed
		if (rs != null) {
		try {
		rs.close();
		} catch (SQLException sqlEx) { } // ignore
		rs = null;
		}
		if (stmt != null) {
		try {
			stmt.close();
		} catch (SQLException sqlEx) { } // ignore
		stmt = null;
		}	
	}
		
		Test test = new Test();
		flag=2;
		test.batchInsertData(flag);
		
	}
	
	public void batchInsertData(int flag){
		 Connection con=null;
		 PreparedStatement preparedStatement= null;

		   try{
			   if(flag == 1){
				   
				   con=getConnection_mydb();
				   preparedStatement=con.prepareStatement("insert into testtable(id,name,address) values(?,?,?)");	  
			   }
			   else if(flag == 2){
				  
				   con=getConnection_backuptable();
				   preparedStatement=con.prepareStatement("insert into testtable2(b_id,b_name,b_address) values(?,?,?)");  
			   }
			   else{
				   System.out.println("the db without selected");
			   }
			    int size = 100100;

			   for (int i = 100000; i < size; i++ ) {
			   preparedStatement.setInt(1, i);
			   preparedStatement.setString(2, "sheng_xu" + i);
			   preparedStatement.setString(3, "JiangXi_WuHan" + i);
			   preparedStatement.addBatch();
			   }

			   preparedStatement.executeBatch();

			   preparedStatement.close();
			   con.close();
		   }catch(Exception ex){
			   ex.printStackTrace();
			   
		   }
		   

		
	}
	
	}


DROP TABLE IF EXISTS `mydb`.`testtable`;
CREATE TABLE  `mydb`.`testtable` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  `address` varchar(45) NOT NULL,
  `category` varchar(45) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




补充点数据库知识,临时自己参考:
mysql:

alter table mydb.testtable
add( contract int(5), orderNumber varchar(45));
用一个表中的字段更新另一个字段
  update   mydb.testtable t set t.orderNumber = substring( t.name,1,7)


update mydb.testtable set orderNumber ="zixiang" where name

oracle:
用一个表中的字段更新另一个字段
  update   mydb.testtable t set t.orderNumber = substring( t.name,1,7)

alter  table mydb.testtable
add( contract int(5), orderNumber varchar(45));

你可能感兴趣的:(java,oracle,sql,mysql,jdbc)