Location: PHPKode > scripts > pdbc > pdbc/test.php
<?php
	function getmicrotime(){
		list($usec, $sec) = explode(" ",microtime());
		return ((float)$usec + (float)$sec);
	}
	$start = getmicrotime();

	require_once ('pdbc/DriverManager.class.php');

	# ÓÃÀ´Á¬½Ómysql4.1ÒÔÏ°汾µÄurl
	#$url = 'pdbc:mysql://localhost:3306/shinestb?dbuser=root&dbpass=shinestb';
	$url = $GLOBALS['url'];
	# ÓÃÀ´Á¬½Ómysql4.1ÒÔÉÏ°æ±¾µÄurl
	#$url = 'pdbc:mysqli://localhost:3306/shinestb?dbuser=root&dbpass=';
	# ÓÃÀ´Á¬½ÓoracleµÄurl
	#$url = 'pdbc:oracle://localhost:3306/shinestb?dbuser=root&dbpass=';
	# ÓÃÀ´Á¬½Ósql serverµÄurl
	#$url = 'pdbc:mssql://localhost:3306/shinestb?dbuser=root&dbpass=';

	# ´´½¨Connection¶ÔÏ󣬻ñÈ¡Ò»¸öÊý¾Ý¿âÁ¬½Ó
	$conn = DriverManager::getConnection($url);
	# ÉèÖÃÊý¾Ý¿âΪ×Ô¶¯¸üÐÂģʽ
	$conn->setAutoCommit(true);

	# ´òÓ¡pdbc°æ±¾
	printf ('PDBC  Version: %s<br/>' ,$conn->getVersion());
	# ´òÓ¡mysql°æ±¾
	printf ('MySQL Version: %d(%s)<br/>', $conn->getServerVersionInt(), $conn->getServerVersionString());
	
	$sql = 'SELECT id, name, add_date FROM program ORDER BY id ASC LIMIT 3';
	
	/*
	# preparedStatementģʽ
	$pstmt = $conn->prepareStatement($sql);
	$rs = $pstmt->executeQuery();
	*/

	# Statementģʽ
	$stmt = $conn->createStatement();

	# ÉèÖòéѯ½á¹û¼¯µÄ´óС
	$stmt->setFetchSize("2.5");
	# »ñÈ¡²éѯ½á¹û¼¯µÄ´óС
	$stmt->pGgetFetchSize();
	# Ö´ÐÐSQL²éѯ
	$stmt->executeQuery($sql);
	# »ñÈ¡±¾´Î²éѯµÄ½á¹û¼¯
	$rs = $stmt->getResultSet();

	# ´òÓ¡ÊÕÓ°ÏìµÄ¼Ç¼Êý(Ö»ÏÞÓÚDELETE,INSERT,UPDATEÓï¾ä)
	printf ('Update Count: %s<br/>', $stmt->getUpdateCount());

	# ´òÓ¡±¾´Î²éѯµÄ×Ö¶ÎÐÅÏ¢
	var_dump($rs->pGetFetchFieldInfo());
	print '<br/>';

	# »ñÈ¡±¾´Î²éѯµÄÔªÊý¾Ý
	#$metadata = $rs->getMetaData();
	#print_r($metadata);
	#print_r($rs->getMetaStructure());exit;
	print '<br/>';

	# ±éÀú½á¹û¼¯
	while ($rs->next()) {
		# $rs->pGetCursor() ·µ»Øµ±Ç°Óαê
		# $rs->getString('name') »ñÈ¡ÀàÐÍΪStringµÄ×Ö¶ÎÃûΪnameµÄÖµ
		printf ('%s $rs->getString(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	}

	# ½«ÓαêÒƶ¯ÖÁ½á¹û¼¯ÖвÎÊýÖ¸¶¨Î»ÖÃ[$rs->absolute(1)]
	if ($rs->absolute(1))
		printf ('%s $rs->absolute(1): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	if ($rs->absolute(2))
		printf ('%s $rs->absolute(2): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	
	# ½«ÓαêÒƶ¯ÖÁ½á¹û¼¯ÖÐ×îºóһλ[$rs->absolute(1)]
	if ($rs->last())
		printf ('%s $rs->last(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	# Åжϵ±Ç°ÓαêÊÇ·ñΪ½á¹û¼¯ÖÐ×îºóÒ»Ìõ
	if ($rs->isLast())
		printf ('%s $rs->isLast(): %s<br/>', $rs->pGetCursor(), 'isLast');

	# ½«ÓαêÒƶ¯ÖÁµ±Ç°ÓαêµÄǰһλ
	if ($rs->previous())
		printf ('%s $rs->previous(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	if ($rs->previous())
		printf ('%s $rs->previous(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));

	while ($rs->next()) {
		printf ('%s $rs->getString(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	}

	# ½«ÓαêÒƶ¯ÖÁ½á¹û¼¯ÖÐÊ×λ
	if ($rs->first())
		printf ('%s $rs->first(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	# Åжϵ±Ç°ÓαêÊÇ·ñΪ½á¹û¼¯ÖÐÊ×Ìõ
	if ($rs->isFirst())
		printf ('%s $rs->isFirst(): %s<br/>', $rs->pGetCursor(), 'isFirst');

	$rs->close();

	# Ìí¼ÓÅúÁ¿Ö´ÐеÄSQLÓï¾ä
	print '<br/>¿ªÊ¼ÅúÁ¿Ö´ÐÐSQL(Statement->addBatch && Statement->executeBatch)<br/>';
	$sql1 = 'SELECT * FROM program ORDER BY id DESC LIMIT 4';
	$sql2 = 'SELECT * FROM room ORDER BY id DESC LIMIT 4';
	$sql3 = 'INSERT INTO vodfee2 (RoomNumber, FeeDatetime, fee, flag) VALUES("1204", NOW(), "124", "1")';
	$stmt->addBatch($sql1);
	$stmt->addBatch($sql2);
	$stmt->addBatch($sql3);
	$batch = array($sql1, $sql2, $sql3);
	$stmt->addBatch($batch);
	#print_r($stmt->getBatchSQL());
	$rss = $stmt->executeBatch();
	#var_dump($stmt->resultBatch);exit;
	$stmt->getMoreResults();
	$rs = $stmt->getResultSet();
	#var_dump($rs);
	#print_r($rs->getMetaStructure());
	while ($rs->next()) {
		printf ('%s $rs->getString(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	}
	$rs->close();
	
	$stmt->getMoreResults();
	$rs = $stmt->getResultSet();
	#var_dump($rs);
	while ($rs->next()) {
		printf ('%s $rs->getString(): %s<br/>', $rs->pGetCursor(), $rs->getString('stbserial'));
	}
	#print_r($rss);
	
	# ¹Ø±ÕPreparedStatement¶ÔÏó
	#$pstmt->close();
	# ¹Ø±ÕStatement¶ÔÏó
	$stmt->close();
	# ¹Ø±ÕResultSet¶ÔÏ󣬲¢ÊͷŽá¹û¼¯
	$rs->close();
	
	# ÉèÖõ±Ç°Connection¶ÔÏóΪֻ¶ÁÊôÐÔ
	$conn->setReadOnly(false);
	# Åжϵ±Ç°Connection¶ÔÏóÊÇ·ñΪֻ¶ÁÊôÐÔ
	if ($conn->isReadOnly())
		printf ('%s', 'readonly<br/>');

	$sql = 'UPDATE program SET last_modify = NOW() WHERE id < 80';
	$stmt = $conn->createStatement();
	$stmt->executeUpdate($sql);
	
	printf ('Update Count: %s<br/>', $stmt->getUpdateCount());
	
	$stmt->close();

	$conn->setReadOnly();
	if ($conn->isReadOnly())
		printf ('%s', 'set connection readonly<br/>');

	$sql = 'SELECT id, name FROM program WHERE id > ? ORDER BY id ASC LIMIT ?';
	# ´´½¨PreparedStatement¶ÔÏó
	$pstmt = $conn->prepareStatement($sql);
	# ÉèÖÃÔ¤²éѯÓï¾äµÄµÚ1¸ö²ÎÊý(?)
	$pstmt->setInt(1, 100);
	# ÉèÖÃÔ¤²éѯÓï¾äµÄµÚ2¸ö²ÎÊý(?)
	$pstmt->setInt(2, 5);
	# Ö´Ðвéѯ(¶ÔÓÚSELECT²Ù×÷ʹÓø÷½·¨)
	$rs = $pstmt->executeQuery();

	while ($rs->next()) {
		printf ('%s $rs->getString(): %s<br/>', $rs->pGetCursor(), $rs->getString('name'));
	}

	# ¹Ø±ÕPreparedStatement¶ÔÏó
	$pstmt->close();
	$rs->close();

	print '<br/>¿ªÊ¼ÅúÁ¿Ö´ÐÐSQL(PreparedStatement->addBatch && PreparedStatement->executeBatch)<br/>';
	$sql = 'INSERT INTO news_type (name) VALUES(?)';
	#print $sql.'<br/>';
	$pstmt = $conn->prepareStatement($sql);
	$pstmt->setString(1, 'test');
	$pstmt->addBatch();

	#$sql = 'INSERT INTO news_type (name) VALUES(?)';
	$sql = 'SELECT * FROM program';
	#print $sql.'<br/>';
	$pstmt = $conn->prepareStatement($sql);
	$pstmt->setString(1, 'chenxi');
	$pstmt->addBatch();
	#$rss = $pstmt->executeBatch();
	#print_r($pstmt->sqlBatch);
	#print_r($rss);

	/*
	# Ö´ÐÐSQLÓï¾ä(¶ÔÓÚINSERT, UPDATE, DELETE²Ù×÷ʹÓø÷½·¨)
	$ret = $pstmt->executeUpdate();
	var_dump($ret);
	printf ('<br/>Update Count: %s<br/>', $pstmt->getUpdateCount());
	*/

	$pstmt->close();
	# ¹Ø±ÕConnection¶ÔÏ󣬶ϿªÊý¾Ý¿âÁ¬½Ó
	$conn->close();

	$end = getmicrotime();
	#print '<br/>'.(($end - $start)*1000).' ºÁÃë';
	print '<br/>'.($end - $start).' Ãë';
	exit();
?>
Return current item: pdbc