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=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			//4.    
			resultset=st.executeQuery("select id,name,birthday,money from user where id<10");
			//5.    
			while(resultset.next())
			{
				System.out.println(resultset.getObject("id"));
				System.out.println(resultset.getObject("name"));
				System.out.println(resultset.getObject("birthday"));
				System.out.println(resultset.getObject("money"));
				String name=resultset.getString("name");
				if("wangwu".equals(name))
				{
					resultset.updateFloat("money", 123);
					resultset.updateRow();
				}
			}
			
		} finally
		{
			JdbcUtils.free(resultset, st, conn);
		}
	}
}
-----------------------------------------------------------------------------------------------------------
/*
  :
               
  :20131003
  :    
*/

public class DBMD {
	//       
	public static void main(String[] args) throws SQLException {
		Connection conn=JdbcUtils.getConnection();
		DatabaseMetaData dbmd=conn.getMetaData();
		System.out.println(dbmd.getDatabaseMajorVersion());
		//      
		System.out.println(dbmd.getDatabaseProductName());
		//       
		System.out.println(dbmd.getDatabaseProductVersion());
		//        
		System.out.println(dbmd.supportsTransactions());
		
	}

}