日期:2014-05-16  浏览次数:20458 次

JDBC数据库的API对照实例学习
/*
功能:
      实现数据库对数据的批处理,比如下面要输入一千条数据,不能每次都要创建连接等操作之后插入一条再断开再建立插入、、、、这样的话很显然是十分的浪费时间的。
      当然了,批处理并不一定能到达很高的效率但是这是一种解决问题的方式。

时间:20131003
作者:烟大阳仔
*/
public class PiChuLi {

	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		create();
	}
	static void create() throws SQLException
	{
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet resultset=null;
		
		try {
			//2.建立连接
			conn=JdbcUtils.getConnection();
			
			//3.创建语句
			String sql="insert into user(name,birthday,money) values(?,?,?)";
			ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			for(int i=0;i<1000;i++)
			{
				ps.setString(1, "sdmf"+i);
				ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));
				ps.setFloat(3, 345+i);
				ps.addBatch();
			}
			ps.executeBatch();
			
		} finally
		{
			JdbcUtils.free(resultset, ps, conn);
		}
	}
}
----------------------------------------------------------------------
/*
功能:
      拿到刚插入的信息的主键,这是API中的一个用于学习该方法

时间:20131003
作者:烟大阳仔
*/
public class OtherApi {

	public static void main(String[] args) throws SQLException {
		int id=create();
		System.out.println(id);
	}
	//拿到刚插入的信息的主键
	static int create() throws SQLException
	{
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet resultset=null;
		
		try {
			//2.建立连接
			conn=JdbcUtils.getConnection();
			
			//3.创建语句
			String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";
			ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			ps.executeUpdate();
			resultset=ps.getGeneratedKeys();
			int id=0;
			if(resultset.next())
			{
				id=resultset.getInt(1);
			}
			return id;
		} finally
		{
			JdbcUtils.free(resultset, ps, conn);
		}
	}

}
-------------------------------------------------------------------------
/*
功能:
      可滚动的结果集实例
      分页查询
时间:20131003
作者:烟大阳仔
*/

public class ScrollAPIDemo {

	public static void main(String[] args) throws SQLException {
		scroll();
	}
	//可滚动的结果集实例
	//分页查询
	static void scroll() throws SQLException
	{
		Connection conn=null;
		Statement st=null;
		ResultSet resultset=null;
		
		try {
			//2.建立连接
			conn=JdbcUtils.getConnection();
			//3.创建语句
			st=conn.createStatement();
			//4.执行语句
			String sql="select id,name,birthday,money from user";
			resultset=st.executeQuery(sql);
			resultset.absolute(8);
			System.out.println();
			if(resultset.previous())
			{
				System.out.println(
				resultset.getObject("id")+"\t"+
				resultset.getObject("name")+"\t"+
				resultset.getObject("birthday")+"\t"+
				resultset.getObject("money")+"\t"
				
				);
			}
			//分页的一种方式不过效率比较低MySQL本身支持分页
			//对MySQL来说设置分页的话直接在sql语句中写为:
			//select id,name,birthday,money from user limit 100,10
			//也就是定位到第一百条数据显示十条数据
			resultset.absolute(100);
			int i=0;
			while(resultset.next()&&i<10)
			{
				i++;
				System.out.println(
						resultset.getObject("id")+"\t"+
						resultset.getObject("name")+"\t"+
						resultset.getObject("birthday")+"\t"+
						resultset.getObject("money")+"\t"
						);
			}
		} finally
		{
			JdbcUtils.free(resultset, st, conn);
		}
	}
}
-------------------------------------------------------------------------
/*
功能:
      可更新的结果集
时间:20131003
作者:烟大阳仔
*/

public class UpdateKeGengXin {

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		read();
	}
	//可更新的结果集,查询出来之后进行更新,不过这个了解就可以了
	static void read() throws SQLException
	{
		Connection conn=null;
		Statement st=null;
		ResultSet resultset=null;
		
		try {
			//2.建立连接
			conn=JdbcUtils.getConnection();
			
			//3.创建语句
			st=